Skip to content
Snippets Groups Projects
Commit b6abba86 authored by Mathias's avatar Mathias
Browse files

First part of the TP.

parent adb13dc8
No related branches found
No related tags found
No related merge requests found
import java.util.Arrays; import java.util.*;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
/** /**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>. * {@code Grid} instances represent the grid in <i>The Game of Life</i>.
...@@ -98,29 +95,68 @@ public class Grid implements Iterable<Cell> { ...@@ -98,29 +95,68 @@ public class Grid implements Iterable<Cell> {
} }
private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() {
return null; boolean[][] newState = new boolean[getNumberOfRows()][getNumberOfColumns()];
for (int r=0; r<numberOfRows; r++) {
for (int c=0; c<numberOfColumns; c++) {
newState[r][c] = this.calculateNextState(r,c,this.getCell(r,c));
}
}
return newState;
}
private void goToNextState(boolean[][] nextState) {
for (int r=0; r<numberOfRows; r++) {
for (int c=0; c<numberOfColumns; c++) {
if (nextState[r][c]) {
this.getCell(r,c).setAlive();
}
else {
this.getCell(r,c).setDead();
}
}
}
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false; return cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex));
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0; int CounterAliveCell = 0;
for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
CounterAliveCell+=1;
}
}
return CounterAliveCell;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null; List<Cell> Neighbours = new ArrayList<>();
for (int r=-1; r<=1; r++) {
for (int c=-1; c<=1; c++) {
if ((r != 0) || (c != 0)) {
Neighbours.add(this.getCell(rowIndex + r, columnIndex + c));
}
}
} }
private void goToNextState(boolean[][] nextState) { return Neighbours;
} }
/** /**
* Sets all {@link Cell}s in this {@code Grid} as dead. * Sets all {@link Cell}s in this {@code Grid} as dead.
*/ */
void clear() { void clear() {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
cell.setDead();
}
} }
/** /**
...@@ -131,6 +167,15 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +167,15 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { void randomGeneration(Random random) {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
if (random.nextBoolean()) {
cell.setAlive();
}
else {
cell.setDead();
}
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment