Skip to content
Snippets Groups Projects
Commit 85bd0c3c authored by zaid's avatar zaid
Browse files

version finale

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.concurrent.Callable;
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>.
...@@ -97,30 +95,113 @@ public class Grid implements Iterable<Cell> { ...@@ -97,30 +95,113 @@ public class Grid implements Iterable<Cell> {
goToNextState(calculateNextStates()); goToNextState(calculateNextStates());
} }
private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() {
return null; boolean [][] MyMatrice=new boolean [numberOfRows][numberOfColumns];
for(int i=0; i<numberOfRows;i++){
for (int j=0; j<numberOfColumns;j++){
MyMatrice[i][j]= calculateNextState(i,j);
}
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return MyMatrice; }
private boolean calculateNextState(int rowIndex, int columnIndex) {
int alive=countAliveNeighbours(rowIndex, columnIndex);
Cell c=getCell(rowIndex,columnIndex);
if (c.isAlive()){
if ((alive == 2) || (alive == 3)){
return true;
}
else{
return false; return false;
} }
}
else {
if(alive == 3){
return true;
}
else{
return false;
}
}
}
private int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0; List<Cell> neighbours = getNeighbours(rowIndex, columnIndex);
int count=0;
for (Cell i: neighbours) {
if (i.isAlive()) {
count++;
}
}
return count;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null; List<Cell> voisines = new ArrayList<>();
Cell c = getCell(rowIndex,columnIndex);
voisines.add(c);
Cell v1=getCell(rowIndex-1,columnIndex-1);
voisines.add(v1);
Cell v2=getCell(rowIndex-1,columnIndex);
voisines.add(v2);
Cell v3=getCell(rowIndex-1,columnIndex+1);
voisines.add(v3);
Cell v4=getCell(rowIndex,columnIndex-1);
voisines.add(v4);
Cell v5=getCell(rowIndex,columnIndex+1);
voisines.add(v5);
Cell v6=getCell(rowIndex+1,columnIndex-1);
voisines.add(v6);
Cell v7=getCell(rowIndex+1,columnIndex);
voisines.add(v7);
Cell v8=getCell(rowIndex+1,columnIndex+1);
voisines.add(v8);
return voisines;
} }
private void goToNextState(boolean[][] nextState) { private void goToNextState(boolean[][] nextState) {
nextState=calculateNextStates();
for (int i=0;i<numberOfRows;i++){
for(int j=0;j<numberOfColumns;j++){
if(nextState[i][j]==true){
cells[i][j].setAlive();
}
else{
cells[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++){
cells[i][j].setDead();
}
}
} }
/** /**
...@@ -131,6 +212,20 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +212,20 @@ public class Grid implements Iterable<Cell> {
*/ */
void randomGeneration(Random random) { void randomGeneration(Random random) {
boolean b;
for(int i=0;i<numberOfRows;i++){
for(int j=0;j<numberOfColumns;j++){
b= random.nextBoolean();
if(b){
cells[i][j].setAlive();
}
else{
cells[i][j].setDead();
} }
}
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment