Reputation: 45
I am trying to reproduce the Game of Life but I've a bug. Cells are born according to design, but they don't die. This confuses me because my strategy for killing cells is the same as for giving birth to them. Here is a segment of the console output, 'x' represents living cells, '-' represents dead cells.
---------
---------
---------
---xx----
----x----
----x----
----xx---
---------
---------
---------
---------
---------
---xx----
----xx---
---xx----
----xx---
---------
---------
---------
---------
---------
---xxx---
----xx---
---xx----
---xxx---
---------
---------
And the relevant piece of code:
public class Life {
final static int WIDTH = 9, HEIGHT = 9;
void start(){
// scanning input file
char[][][] board = new char[WIDTH][HEIGHT][maxAllowedGenerations];
board = getInitialBoard(initialBoardString, maxAllowedGenerations, board);
for (int generation = 1; generation < maxAllowedGenerations; generation++){
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++){
int numberOfNeighbours = getNumberOfNeighbours(x, y, generation - 1 , board);
if (board[x][y][generation - 1] == '-' && numberOfNeighbours == 3)
board[x][y][generation] = 'x';
else if (board[x][y][generation - 1] == 'x' && numberOfNeighbours < 2)
board[x][y][generation] = '-';
else board[x][y][generation] = board[x][y][generation - 1];
if (board[x][y][generation] == 'x')
ui.place(x, y, LifeUserInterface.ALIVE);
else
ui.place(x, y, LifeUserInterface.DEAD);
out.print(board[x][y][generation]);
}
out.println();
}
}
out.println("Max number of generations reached");
System.exit(0);
}
Upvotes: 1
Views: 917
Reputation: 45
Found two bugs! One of them was impossible for you to spot because I didn't post the code in which it was contained: I am a cell at [x][y][g]. I was considering [x][y][g - 1] to be a neighbour, but that is of course me! I am not my own neighbour.
The other bug was a bit embarrasing actually. I had left out rule number 2... >.<
I also realize I should have posted the rules of the Game of Life instead of assuming that you all know them or that you would bother researching them. It's a bit late now of course, but I'll post them anyway in case you are interested. Also, I really reccomend the wiki article for anyone interested in self-organization.
Rules:
Thank you for all input!
Upvotes: 1
Reputation: 6207
I agree with @elyashiv - if you change char[][][] board
to SomeEnum[][][] board
, with SomeEnum
defined with values LIVE_CELL
and DEAD_CELL
that would make things much more readable.
Also, there is no such thing as an empty character ''. An empty String
is simply a String
with zero length (ie no characters), but '' makes no sense. You could use null
, but then you'd have to move away from the primitive char
declaration and use Character
instead since primitives can't be null
.
That said, much better to use enums to represent the data. If you want, you can even make your enum look like this so you can represent your X and empty characters like so:
public enum SomeEnum {
LIVE_CELL("X"),
DEAD_CELL("");
public final displayString;
SomeEnum(String displayString) {
this.displayString = displayString;
}
}
Then for your display you could reference SomeEnum.LIVE_CELL.displayString
in your code
Upvotes: 2