Skip to content
Snippets Groups Projects
Commit e68dbee2 authored by adam's avatar adam
Browse files

Nouvelle classe Grid

parent 7672771d
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>.
...@@ -87,7 +84,7 @@ public class Grid implements Iterable<Cell> { ...@@ -87,7 +84,7 @@ public class Grid implements Iterable<Cell> {
* <ul> * <ul>
* <li>Any live {@link Cell} with fewer than two live neighbours dies, i.e. underpopulation.</li> * <li>Any live {@link Cell} with fewer than two live neighbours dies, i.e. underpopulation.</li>
* <li>Any live {@link Cell} with two or three live neighbours lives on to the next * <li>Any live {@link Cell} with two or three live neighbours lives on to the next
* generation.</li> * * generation.</li>
* <li>Any live {@link Cell} with more than three live neighbours dies, i.e. overpopulation.</li> * <li>Any live {@link Cell} with more than three live neighbours dies, i.e. overpopulation.</li>
* <li>Any dead {@link Cell} with exactly three live neighbours becomes a live cell, i.e. * <li>Any dead {@link Cell} with exactly three live neighbours becomes a live cell, i.e.
* reproduction.</li> * reproduction.</li>
...@@ -96,31 +93,69 @@ public class Grid implements Iterable<Cell> { ...@@ -96,31 +93,69 @@ public class Grid implements Iterable<Cell> {
void nextGeneration() { void nextGeneration() {
goToNextState(calculateNextStates()); goToNextState(calculateNextStates());
} }
public List<Cell> getNeighbours(int rowIndex, int colummIndex) {
List<Cell> listofNeighbours= new ArrayList<Cell>(0);
listofNeighbours.add(getCell(rowIndex+1,colummIndex-1));
listofNeighbours.add(getCell(rowIndex+1,colummIndex));
listofNeighbours.add(getCell(rowIndex+1,colummIndex+1));
listofNeighbours.add(getCell(rowIndex,colummIndex+1));
listofNeighbours.add(getCell(rowIndex-1,colummIndex+1));
listofNeighbours.add(getCell(rowIndex-1,colummIndex));
listofNeighbours.add(getCell(rowIndex-1,colummIndex-1));
listofNeighbours.add(getCell(rowIndex,colummIndex-1));
return listofNeighbours;
private boolean[][] calculateNextStates() {
return null;
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) {
int AliveNeighbours=0;
for (Cell Neighbour : getNeighbours(rowIndex,columnIndex)){
if (Neighbour.isAlive())
AliveNeighbours++;
}
return AliveNeighbours;
}
private boolean calculateNextState(int rowIndex, int columnIndex){
if (countAliveNeighbours(rowIndex,columnIndex)==3 && !getCell(rowIndex,columnIndex).isAlive())
return true;
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { if (getCell(rowIndex,columnIndex).isAlive() && (countAliveNeighbours(rowIndex,columnIndex)==2 || countAliveNeighbours(rowIndex,columnIndex) == 3))
return true;
else
return false; return false;
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) { private boolean[][] calculateNextStates() {
return 0; boolean[][] tableau=new boolean[numberOfRows][numberOfColumns];
for (int row=0;row<numberOfRows;row++){
for (int column=0;column<numberOfColumns;column++){
tableau[row][column]=calculateNextState(row,column);
}
}
return tableau;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null;
}
private void goToNextState(boolean[][] nextState) { private void goToNextState(boolean[][] nextState) {
for (int i = 0; i <numberOfRows ; i++) {
for (int j = 0; j <numberOfColumns ; j++) {
if (nextState[i][j])
getCell(i,j).setAlive();
else
getCell(i,j).setDead();
}
}
} }
/** /**
* 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 (int i = 0; i <numberOfRows ; i++) {
for (int j = 0; j <numberOfColumns ; j++) {
getCell(i,j).setDead();
}
}
} }
/** /**
...@@ -131,6 +166,16 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +166,16 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { void randomGeneration(Random random) {
for (int i = 0; i <numberOfRows ; i++) {
for (int j = 0; j <numberOfColumns ; j++) {
if (random.nextBoolean())
getCell(i,j).setAlive();
else
getCell(i,j).setDead();
}
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment