Jump to content

Game of Life Java Program

namarino

Hey guys, I have to program conway's game of life in Java, but I'm having a lot of trouble, I feel like a complete moron right now. This is what I have so far:

public GameOfLifeBoard(String file_name){		Scanner input;		String character;				try {			input = new Scanner(new File(file_name));			rows = input.nextInt();			columns = input.nextInt();						current = new boolean[rows][columns];						for(int x = 0; x < current.length; x++){				for(int y = 0; y < current[x].length; y++){					character = input.next();					if(character.equals("*")){						current[x][y] = true;					}				}			}		} catch (FileNotFoundException e) {			System.out.print("The file " + file_name + " could not be found.");		}	}

This just reads the file into the array current. Right now, I'm trying to figure out a way to implement the rules of the game in order to determine whether a cell lives or dies, but I can't think of any way to do it without having a mile long line of code inside an if statement (I would be checking every element around the element in question). Can someone help me with this? Thanks a lot!

Link to comment
Share on other sites

Link to post
Share on other sites

"Have to program"? Why not start with something simpler?

School project :/

Link to comment
Share on other sites

Link to post
Share on other sites

School project :/

I might be able to help if you explain what you need help trying to do. (I don't remember the rules of life at all) 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

I might be able to help if you explain what you need help trying to do. (I don't remember the rules of life at all) 

Ok so basically the rules are as follows: A cell survives if it has 2 or 3 neighbors. A cell dies if it has more than 3 neighbors or less than 2. A cell is born if it is adjacent to exactly 3 cells. So I have to find a way to implement those rules basically. I have to determine whether a cell lives or dies by looking at its surroundings. Does that make sense?? Maybe I didn't explain it very well. 

Link to comment
Share on other sites

Link to post
Share on other sites

I might be able to help if you explain what you need help trying to do. (I don't remember the rules of life at all) 

 

Probably a lot of people around here can help, the issue is doing it without just writing the code for them.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so basically the rules are as follows: A cell survives if it has 2 or 3 neighbors. A cell dies if it has more than 3 neighbors or less than 2. A cell is born if it is adjacent to exactly 3 cells. So I have to find a way to implement those rules basically. I have to determine whether a cell lives or dies by looking at its surroundings. Does that make sense?? Maybe I didn't explain it very well. 

 

What solution do you have so far? Even just in pseudocode/plain english?

Link to comment
Share on other sites

Link to post
Share on other sites

Probably a lot of people around here can help, the issue is doing it without just writing the code for them.

Yeah I don't want the code, I just want someone to explain.

Link to comment
Share on other sites

Link to post
Share on other sites

What solution do you have so far? Even just in pseudocode/plain english?

Ok so basically I have an if statement that says:

if(array[x-1][y-1] && array[x-1][y] || etc...)

 

You can see how long that will go on for...

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah I don't want the code, I just want someone to explain.

 

Well like I said, it helps to write out the steps in english first. E.g. "For each cell, check how many neighbors it has and store the value. If the number of neighbors is more than three..." and so on.

Link to comment
Share on other sites

Link to post
Share on other sites

Well like I said, it helps to write out the steps in english first. E.g. "For each cell, check how many neighbors it has and store the value. If the number of neighbors is more than three..." and so on.

How can I check the number of neighbors in a more efficient way?

Link to comment
Share on other sites

Link to post
Share on other sites

How can I check the number of neighbors in a more efficient way?

 

Instead of checking for every possible combination of two neighbors or more than three, just count the total numbers. You don't care which neighbors are there, right? Just how many there are. So possibly a loop of some kind, maybe in it's own function that returns a single value. That narrows down your if statements when you actually do the death check.

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys, I have to program conway's game of life in Java, but I'm having a lot of trouble, I feel like a complete moron right now. This is what I have so far:

public GameOfLifeBoard(String file_name){		Scanner input;		String character;				try {			input = new Scanner(new File(file_name));			rows = input.nextInt();			columns = input.nextInt();						current = new boolean[rows][columns];						for(int x = 0; x < current.length; x++){				for(int y = 0; y < current[x].length; y++){					character = input.next();					if(character.equals("*")){						current[x][y] = true;					}				}			}		} catch (FileNotFoundException e) {			System.out.print("The file " + file_name + " could not be found.");		}	}

This just reads the file into the array current. Right now, I'm trying to figure out a way to implement the rules of the game in order to determine whether a cell lives or dies, but I can't think of any way to do it without having a mile long line of code inside an if statement (I would be checking every element around the element in question). Can someone help me with this? Thanks a lot!

What course are you taking?

Link to comment
Share on other sites

Link to post
Share on other sites

Instead of checking for every possible combination of two neighbors or more than three, just count the total numbers. You don't care which neighbors are there, right? Just how many there are. So possibly a loop of some kind, maybe in it's own function that returns a single value. That narrows down your if statements when you actually do the death check.

Oh so a for loop that loops around the cell in question? Ohhhhhh and then every time it passes a live cell, add 1 to some variable. Would that work?

Link to comment
Share on other sites

Link to post
Share on other sites

Oh so a for loop that loops around the cell in question? Ohhhhhh and then every time it passes a live cell, add 1 to some variable. Would that work?

 

Sure. A cell I assume can have at most eight neighbors, so there's your loop; you would return whatever that accumulator ended up at the end.

Link to comment
Share on other sites

Link to post
Share on other sites

Sure. A cell I assume can have at most eight neighbors, so there's your loop; you would return whatever that accumulator ended up at the end.

Ok got it. Let me try to code that real quick and I'll let you know if it works.

Link to comment
Share on other sites

Link to post
Share on other sites

Sure. A cell I assume can have at most eight neighbors, so there's your loop; you would return whatever that accumulator ended up at the end.

Ok so I'm not sure how good stylistically this is, but it works! haha You feed it the coordinates of the cell in questions and it returns the number of surrounding live cells.

public int neighbours(int row, int column){		int neighbours = 0;				for(int x = (row - 1); x < (row + 2); x++){			for(int y = (column - 1); y < (column + 2); y++){				if(current[x][y]){					neighbours++;				}				if(x == row && y == column){					neighbours--;				}			}		}		return neighbours;		}
Link to comment
Share on other sites

Link to post
Share on other sites

Ok so I'm not sure how good stylistically this is, but it works! haha You feed it the coordinates of the cell in questions and it returns the number of surrounding live cells.

 

That's the ticket. Of course there are a number of ways of doing it in the code but the result should be the same.

Link to comment
Share on other sites

Link to post
Share on other sites

A loop might not be the best idea since each cell is going to have different out of bounds conditions.

Also in your current code the current cell might not be true so doing 

if(x == row && y == column){    neighbours--;}

may end up with an off by 1 error.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

A loop might not be the best idea since each cell is going to have different out of bounds conditions.

Also in your current code the current cell might not be true so doing 

if(x == row && y == column){    neighbours--;}

may end up with an off by 1 error.

 

That's the ticket. Of course there are a number of ways of doing it in the code but the result should be the same.

I think a loop is probably the best way to do it. Do you have any other suggestions? 

Link to comment
Share on other sites

Link to post
Share on other sites

I think a loop is probably the best way to do it. Do you have any other suggestions? 

If you want to stick with a loop you could either get the minimum and maximum row and column before looping based on your current cell. So if you're at column 0 you wouldn't check column -1 or if you're at row = <# of rows -1> don't check row + 1 etc.

 

Or you could add a row of padding around the entire board and not worry about it.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

I think a loop is probably the best way to do it. Do you have any other suggestions? 

 

Padding, as suggested. Unrolling the loop wouldn't be so bad, though. Experiment, there are a number of ways to solve the problem.

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to stick with a loop you could either get the minimum and maximum row and column before looping based on your current cell. So if you're at column 0 you wouldn't check column -1 or if you're at row = <# of rows -1> don't check row + 1 etc.

 

Or you could add a row of padding around the entire board and not worry about it.

 

Padding, as suggested. Unrolling the loop wouldn't be so bad, though. Experiment, there are a number of ways to solve the problem.

Alright cool, thanks guys, I really appreciate your help!

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

×