patternjavaMinor
Easy Game of Life project
Viewed 0 times
lifeeasygameproject
Problem
I've developed easy Game of Life project. I'd highly appreciate any feedback and possibilities to improve my code, both in efficiency and style. It works on list and it's terminal program.
ICell.java
Cell.java
IGameOfLife.java
GameOfLife.java
```
public class GameOfLife implements IGameOfLife{
private List livingCells = new ArrayList();
public void setNewList(List cells) {
this.livingCells = cells;
if(livingCells.isEmpty())
System.out.print("No more living cells.\n");
}
public List getLivingCells() {
return livingCells;
}
public List live() {
IEngine engine = new GameOfLifeEngine(livingCells);
// Checks if living cells will continue to live
List newList = engine.checkLivingCells();
// Finds new cells reproduced by 3 nearby cells
newList.addAll(engine.reproduction());
return newList;
}
public List getPresetCells(IInputMethod sourceDataReader) {
int amountOfInputData = sourceDataReader.getInput();
List presetCells = new ArrayList();
for (int i = 0; i sourceReader = new SourceDataReader(args);
IInputMethod userInp
ICell.java
public interface ICell {
public int getX();
public int getY();
}Cell.java
public class Cell implements ICell {
private int x, y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!Cell.class.isAssignableFrom(obj.getClass())) {
return false;
}
final Cell other = (Cell) obj;
if(this.x != other.getX() || this.y != other.getY())
return false;
return true;
}
@Override
public String toString() {
return new String("(x: " + this.x + ", y: " + y + ")");
}
}IGameOfLife.java
public interface IGameOfLife {
public void setNewList(List cells);
public List getLivingCells();
public List live();
public List getPresetCells(IInputMethod sourceDataReader);
}GameOfLife.java
```
public class GameOfLife implements IGameOfLife{
private List livingCells = new ArrayList();
public void setNewList(List cells) {
this.livingCells = cells;
if(livingCells.isEmpty())
System.out.print("No more living cells.\n");
}
public List getLivingCells() {
return livingCells;
}
public List live() {
IEngine engine = new GameOfLifeEngine(livingCells);
// Checks if living cells will continue to live
List newList = engine.checkLivingCells();
// Finds new cells reproduced by 3 nearby cells
newList.addAll(engine.reproduction());
return newList;
}
public List getPresetCells(IInputMethod sourceDataReader) {
int amountOfInputData = sourceDataReader.getInput();
List presetCells = new ArrayList();
for (int i = 0; i sourceReader = new SourceDataReader(args);
IInputMethod userInp
Solution
Code duplication
vs
How can we avoid such repetition? The total number of cells around any cell is 8 and a cell is either alive or dead, so:
Yes, I noticed that
private int findAllLivingCellsAroundCoordinates(int i, int j) {
int neighbours = 0;
if (livingCells.contains(new Cell(i - 1, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i - 1, j)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j)))
neighbours++;
if (livingCells.contains(new Cell(i - 1, j + 1)))
neighbours++;
if (livingCells.contains(new Cell(i, j + 1)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j + 1)))
neighbours++;
return neighbours;
}vs
private int findAllDeadCellsAroundCoordinates(int i, int j) {
int neighbours = 0;
if (!livingCells.contains(new Cell(i - 1, j - 1)))
addToEmptySpaces(new Cell(i - 1, j - 1));
if (!livingCells.contains(new Cell(i, j - 1)))
addToEmptySpaces(new Cell(i, j - 1));
if (!livingCells.contains(new Cell(i + 1, j - 1)))
addToEmptySpaces(new Cell(i + 1, j - 1));
if (!livingCells.contains(new Cell(i - 1, j)))
addToEmptySpaces(new Cell(i - 1, j));
if (!livingCells.contains(new Cell(i + 1, j)))
addToEmptySpaces(new Cell(i + 1, j));
if (!livingCells.contains(new Cell(i - 1, j + 1)))
addToEmptySpaces(new Cell(i - 1, j + 1));
if (!livingCells.contains(new Cell(i, j + 1)))
addToEmptySpaces(new Cell(i, j + 1));
if (!livingCells.contains(new Cell(i + 1, j + 1)))
addToEmptySpaces(new Cell(i + 1, j + 1));
return neighbours;
}How can we avoid such repetition? The total number of cells around any cell is 8 and a cell is either alive or dead, so:
private int findAllDeadCellsAroundCoordinates(int i, int j) {
return 8 - findAllLivingCellsAroundCoordinates(i, j);
}Yes, I noticed that
findAllDeadCellsAroundCoordinates also performed another job, but this means that it was performing two jobs, while the single responsibility principle suggests only one job per function. So write another function to findDeadCellsNearLivingOnes. When you expect a function to only retrieve an information and it has un-expected side effects debugging becomes a nightmare.Code Snippets
private int findAllLivingCellsAroundCoordinates(int i, int j) {
int neighbours = 0;
if (livingCells.contains(new Cell(i - 1, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j - 1)))
neighbours++;
if (livingCells.contains(new Cell(i - 1, j)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j)))
neighbours++;
if (livingCells.contains(new Cell(i - 1, j + 1)))
neighbours++;
if (livingCells.contains(new Cell(i, j + 1)))
neighbours++;
if (livingCells.contains(new Cell(i + 1, j + 1)))
neighbours++;
return neighbours;
}private int findAllDeadCellsAroundCoordinates(int i, int j) {
int neighbours = 0;
if (!livingCells.contains(new Cell(i - 1, j - 1)))
addToEmptySpaces(new Cell(i - 1, j - 1));
if (!livingCells.contains(new Cell(i, j - 1)))
addToEmptySpaces(new Cell(i, j - 1));
if (!livingCells.contains(new Cell(i + 1, j - 1)))
addToEmptySpaces(new Cell(i + 1, j - 1));
if (!livingCells.contains(new Cell(i - 1, j)))
addToEmptySpaces(new Cell(i - 1, j));
if (!livingCells.contains(new Cell(i + 1, j)))
addToEmptySpaces(new Cell(i + 1, j));
if (!livingCells.contains(new Cell(i - 1, j + 1)))
addToEmptySpaces(new Cell(i - 1, j + 1));
if (!livingCells.contains(new Cell(i, j + 1)))
addToEmptySpaces(new Cell(i, j + 1));
if (!livingCells.contains(new Cell(i + 1, j + 1)))
addToEmptySpaces(new Cell(i + 1, j + 1));
return neighbours;
}private int findAllDeadCellsAroundCoordinates(int i, int j) {
return 8 - findAllLivingCellsAroundCoordinates(i, j);
}Context
StackExchange Code Review Q#122984, answer score: 2
Revisions (0)
No revisions yet.