Jump to content

Game of Life

namarino

Hey guys, I have to make this game of life project for my computer science class. I am NOT asking for the answer, but if someone could just explain to me what these methods are supposed to do it would be a big help. The professor essentially gives us a skeleton program containing classes, instance variables and methods and you have to fill it in, but the problem is that you really don't know what role they are supposed to play in the program. You essentially have to read in the initial board from a file which looks like this. The . are blank and the * are alive.Again I'm not asking for the code at all, I'm just trying to get an understanding of how this is going to be working. Thanks so much for any help you can give me.

 

10 10                 the tens are the rows and columns 
..........
..........
..........
..........
....***...
....*.....
.....*....
..........
..........
..........

public class GameOfLifeBoard extends Object{	private boolean[][] current;	private boolean [][] next;		public GameOfLifeBoard(String file_name){			}			public int rows(){			}		public int columns(){			}			private boolean isBetween(int low, int value, int high){				return true;	}		public void toggle(int row, int column){			}		public boolean isAlive(int row, int column){				return true;	}		public int neighbours(int row, int column){				return 0;		}		public boolean lives(boolean occupied, int neightbours){				return true;	}		private void nextGeneration(){			}		public void printNeighbours(){			}		public String toString(){				return null;	}	}
Link to comment
Share on other sites

Link to post
Share on other sites

From your explanation I can't really understand what's supposed to happen. so yeah

|CPU: Intel i7-5960X @ 4.4ghz|MoBo: Asus Rampage V|RAM: 64GB Corsair Dominator Platinum|GPU:2-way SLI Gigabyte G1 Gaming GTX 980's|SSD:512GB Samsung 850 pro|HDD: 2TB WD Black|PSU: Corsair AX1200i|COOLING: NZXT Kraken x61|SOUNDCARD: Creative SBX ZxR|  ^_^  Planned Bedroom Build: Red Phantom [quadro is stuck in customs, still trying to find a cheaper way to buy a highend xeon]

Link to comment
Share on other sites

Link to post
Share on other sites

Looking at the methods, it looks like an implementation of Conway's Game of Life. Its rules are:

Any live cell with fewer than two live neighbours dies, as if caused by under-population.

Any live cell with two or three live neighbours lives on to the next generation.

Any live cell with more than three live neighbours dies, as if by overcrowding.

Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.I remember doing something like this a along time ago.

http://en.wikipedia.org/wiki/Conway's_Game_of_Life

The toggle method would probably switch the cell from alive to dead or vice-versa.

The lives method would return whether that cell would be alive for the next generation.

Neighbors would return how many live cells are adjacent to a cell.

Link to comment
Share on other sites

Link to post
Share on other sites

I've put what I think each method should do. Let me know if you need more help. It would also help to be able to see the object class.

public class GameOfLifeBoard extends Object{	private boolean[][] current;	private boolean [][] next;		public GameOfLifeBoard(String file_name){		// In here you should read in the file.	}			public int rows(){		// Return the amount of rows.	}		public int columns(){		// Return the amount of colunms.	}			private boolean isBetween(int low, int value, int high){		// No idea.		return true;	}		public void toggle(int row, int column){		// I'm going to assume that if it's alive then change it to dead and visa versa.	}		public boolean isAlive(int row, int column){		// Return whether or not the object in this specific location is alive or not.		return true;	}		public int neighbours(int row, int column){		// Return the cells around the current one.		return 0;		}		public boolean lives(boolean occupied, int neightbours){		// Not sure		return true;	}		private void nextGeneration(){		// No idea.	}		public void printNeighbours(){		// Pretty obvious from the name, print the surrounding neighbours.	}		public String toString(){		// Again pretty self explanatory, convert something to a string.		return null;	}	}
Link to comment
Share on other sites

Link to post
Share on other sites

 

I've put what I think each method should do. Let me know if you need more help. It would also help to be able to see the object class.

public class GameOfLifeBoard extends Object{	private boolean[][] current;	private boolean [][] next;		public GameOfLifeBoard(String file_name){		// In here you should read in the file.	}			public int rows(){		// Return the amount of rows.	}		public int columns(){		// Return the amount of colunms.	}			private boolean isBetween(int low, int value, int high){		// No idea.		return true;	}		public void toggle(int row, int column){		// I'm going to assume that if it's alive then change it to dead and visa versa.	}		public boolean isAlive(int row, int column){		// Return whether or not the object in this specific location is alive or not.		return true;	}		public int neighbours(int row, int column){		// Return the cells around the current one.		return 0;		}		public boolean lives(boolean occupied, int neightbours){		// Not sure		return true;	}		private void nextGeneration(){		// No idea.	}		public void printNeighbours(){		// Pretty obvious from the name, print the surrounding neighbours.	}		public String toString(){		// Again pretty self explanatory, convert something to a string.		return null;	}	}

Thanks man, I really appreciate it. One thing that's really confusing me is the rows and columns methods. I don't understand why they don't have parameters. Shouldn't they take the file name as a parameter and determine the rows and columns?

Link to comment
Share on other sites

Link to post
Share on other sites

Mostly what Sneaky said though I added some of my ideas (my comments are between the /* and */ )

I've put what I think each method should do. Let me know if you need more help. It would also help to be able to see the object class.

public class GameOfLifeBoard extends Object{	private boolean[][] current;	private boolean [][] next;		public GameOfLifeBoard(String file_name){		// In here you should read in the file.		/* What Sneaky said. Parse the file and put it in the variable "current".                    Though personally I'd prefer to just store the filename and have a separate parse method. */	}			public int rows(){		// Return the amount of rows.	}		public int columns(){		// Return the amount of colunms.	}			private boolean isBetween(int low, int value, int high){		// No idea.                /* From the method name it, looks like that it would return true if int value is between low and high.                    It's a private method so I would assume this would replace your "if (value > low && value < high)"                   with something like "if (isBetween(low,value,high))" */		return true;	}		public void toggle(int row, int column){		// I'm going to assume that if it's alive then change it to dead and visa versa.	}		public boolean isAlive(int row, int column){		// Return whether or not the object in this specific location is alive or not.		return true;	}		public int neighbours(int row, int column){		// Return the cells around the current one.		/* Sneaky's answer was what I originally thought but since the method returns an int and not an array of ints,                    I would assume it returns the number of cells around the cell[row,column] */		return 0;		}		public boolean lives(boolean occupied, int neightbours){		// Not sure		/* Answered in my previous reply. But to be more specific:		   returns false if occupied is true and neighbors is less than 2 [Rule #1]		   returns true if occupied is true and neighbors is 2 or 3 [Rule #2] 		   returns false if occupied is true and neighbors is more than 3 [Rule #3]		   returns true if occupied is false and neighbors is equal to 3 [Rule #4] */		return true;	}		private void nextGeneration(){		// No idea.		/* Basically compute every cell whether it lives or dies. Not sure if the array current[][] should be overwritten                    or store the results in next[][] but since I haven't seen any use of next[][] I would assume it would be stored there. */	}		public void printNeighbours(){		// Pretty obvious from the name, print the surrounding neighbours.	}		public String toString(){		// Again pretty self explanatory, convert something to a string.		/* Convert the class to a string. 		   This would be called in code with something like:			GameofLifeBoard golb(file_name);			...			golb_string = golb.toString();		   You should probably use the format of the input file. */		return null;	}	}

Thanks man, I really appreciate it. One thing that's really confusing me is the rows and columns methods. I don't understand why they don't have parameters. Shouldn't they take the file name as a parameter and determine the rows and columns?

It shouldn't need it. The file name is provided upon creation of the object via your constructor. So something like:

...GameofLifeBoard g(gol_file.txt);...
It would then parse the file and store the data to current[][], and then use current[][] to figure out how many rows and columns you have.

EDIT: Added reply to namarino

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks man, I really appreciate it. One thing that's really confusing me is the rows and columns methods. I don't understand why they don't have parameters. Shouldn't they take the file name as a parameter and determine the rows and columns?

notice that the constructor takes as a parameter a file name. when you instantiate a GameOfLifeBoard object, that particular object will be bound to the specific map that was present in the file specified as the parameter

GameOfLifeBoard simple_map = new GameOfLifeBoard('easy_setup.txt');GameOfLifeBoard complex_map = new GameOfLifeBoard('messed_up_setup.txt');simple_map.columns(); // columns of the map in easy_setup.txtcomplex_map.rows(); // rows of the map in messed_up_setup.txt
Link to comment
Share on other sites

Link to post
Share on other sites

 

notice that the constructor takes as a parameter a file name. when you instantiate a GameOfLifeBoard object, that particular object will be bound to the specific map that was present in the file specified as the parameter

GameOfLifeBoard simple_map = new GameOfLifeBoard('easy_setup.txt');GameOfLifeBoard complex_map = new GameOfLifeBoard('messed_up_setup.txt');simple_map.columns(); // columns of the map in easy_setup.txtcomplex_map.rows(); // rows of the map in messed_up_setup.txt

 

Mostly what Sneaky said though I added some of my ideas (my comments are between the /* and */ )

It shouldn't need it. The file name is provided upon creation of the object via your constructor. So something like:

...GameofLifeBoard g(gol_file.txt);...
It would then parse the file and store the data to current[][], and then use current[][] to figure out how many rows and columns you have.

EDIT: Added reply to namarino

 

Hmmmm I still don't quite understand how that works. Because doesn't the method need to read the file in order to determine how many rows there are? I'm sorry I'm not completely understanding this....

Link to comment
Share on other sites

Link to post
Share on other sites

Hmmmm I still don't quite understand how that works. Because doesn't the method need to read the file in order to determine how many rows there are? I'm sorry I'm not completely understanding this....

you need to understand the basic concepts of OOP. in this exercise you're using a class, an object, methods (and a constructor) and attributes

your teacher also tried to explicitly show you that you're using inheritance, but that's more specific to java and it's the step after you understood all those previous concepts

 

read about it here or on your textbook of choice

Link to comment
Share on other sites

Link to post
Share on other sites

Hmmmm I still don't quite understand how that works. Because doesn't the method need to read the file in order to determine how many rows there are? I'm sorry I'm not completely understanding this....

To me it seems like your problem is understanding the principals of Object Oriented Programming, you might want to do some reading up. It's too big of a topic for me to really explain right now.

Link to comment
Share on other sites

Link to post
Share on other sites

To me it seems like your problem is understanding the principals of Object Oriented Programming, you might want to do some reading up. It's too big of a topic for me to really explain right now.

No I understand object oriented programming. My problem is that you can't read a file into an array unless you know how big the array is and supposedly the job of the methods rows and columns is to read the array and figure out how big it is. So if you could explain how I do that without passing those methods a parameter to read the file itself and not the array, that would be great.

Link to comment
Share on other sites

Link to post
Share on other sites

No I understand object oriented programming. My problem is that you can't read a file into an array unless you know how big the array is and supposedly the job of the methods rows and columns is to read the array and figure out how big it is. So if you could explain how I do that without passing those methods a parameter to read the file itself and not the array, that would be great.

the first line of the file tells you the number of rows and columns

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×