patternjavaMinor
My version of Conway's Game of Life in Java
Viewed 0 times
versionconwayjavagamelife
Problem
I am currently only doing one generation. I'd like a review of my code.
Game of Life: The universe of the Game of Life is an infinite
two-dimensional orthogonal grid of square cells, each of which is in
one of two possible states, live or dead. Every cell interacts with
its eight neighbours, which are the cells that are directly
horizontally, vertically, or diagonally adjacent. At each step in
time, the following transitions occur:
Game.java:
```
package tw51.assignment.gameoflife;
import java.util.*;
import static tw51.assignment.gameoflife.Cells.State;
import static tw51.assignment.gameoflife.Cells.State.Alive;
import static tw51.assignment.gameoflife.Cells.State.Dead;
/**
* Represents A Game with a set Of rules that forms patterns recursively.
*/
public class Game {
private List aliveCells = new ArrayList<>();
private List deadCells = new ArrayList<>();
private List changedCells = new ArrayList<>();
public void setAliveCells(List aliveCells) {
this.aliveCells = aliveCells;
for (Cells cell : aliveCells) {
cell.setState(Alive);
}
}
public State getNextState(Cells cell, int liveNeighbours) {
if ((cell.getCurrentState() == Alive) && (liveNeighbours 3)) {
cell.setNextState(Dead);
changedCells.add(cell);
} else if ((cell.getCurrentState() == Dead) && liveNeighbours == 3) {
cell.setNextState(Alive);
changedCells.add(cell);
} else {
cell.setNextState(cell.getCurrentState());
}
return cell.getNextState();
}
private void killCellsWithLessThanTwoOrMo
Game of Life: The universe of the Game of Life is an infinite
two-dimensional orthogonal grid of square cells, each of which is in
one of two possible states, live or dead. Every cell interacts with
its eight neighbours, which are the cells that are directly
horizontally, vertically, or diagonally adjacent. At each step in
time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if by loneliness.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any live cell with two or three live neighbours lives, unchanged, to the next generation.
- Any dead cell with exactly three live neighbours comes to life.
Game.java:
```
package tw51.assignment.gameoflife;
import java.util.*;
import static tw51.assignment.gameoflife.Cells.State;
import static tw51.assignment.gameoflife.Cells.State.Alive;
import static tw51.assignment.gameoflife.Cells.State.Dead;
/**
* Represents A Game with a set Of rules that forms patterns recursively.
*/
public class Game {
private List aliveCells = new ArrayList<>();
private List deadCells = new ArrayList<>();
private List changedCells = new ArrayList<>();
public void setAliveCells(List aliveCells) {
this.aliveCells = aliveCells;
for (Cells cell : aliveCells) {
cell.setState(Alive);
}
}
public State getNextState(Cells cell, int liveNeighbours) {
if ((cell.getCurrentState() == Alive) && (liveNeighbours 3)) {
cell.setNextState(Dead);
changedCells.add(cell);
} else if ((cell.getCurrentState() == Dead) && liveNeighbours == 3) {
cell.setNextState(Alive);
changedCells.add(cell);
} else {
cell.setNextState(cell.getCurrentState());
}
return cell.getNextState();
}
private void killCellsWithLessThanTwoOrMo
Solution
Regarding the
The
Cells class:- It is an implementation detail, and should not be public.
- As such, you need not even add getters and setters for it.
xandyis as clear asxCoordinate.
- Keeping the previous and next state inside the class is dubious in my opinion. I'd make the container keep previous and next state.
- Keeping
xandyas part of the cell is an open door to duplicate cells, in the collection, which forced you to handle this case explicitly. It also makes you keep redundant information about neighbors. A much more simple and "obvious" way is to keep a mapping from coordinates to state in the Game class.
- The
Statefields are not initialized.
The
Game class itself is both the board and the rules - it is not cohesive.Context
StackExchange Code Review Q#135773, answer score: 2
Revisions (0)
No revisions yet.