diff --git a/Grid.java b/Grid.java index 939e05fd8e4207e92eb7ffaedebfcd6d4d0f6d0b..4e40009132f2810cb01296137b676ac78c4abbde 100644 --- a/Grid.java +++ b/Grid.java @@ -1,7 +1,5 @@ -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.Random; +import java.util.*; +import java.util.concurrent.Callable; /** * {@code Grid} instances represent the grid in <i>The Game of Life</i>. @@ -97,30 +95,113 @@ public class Grid implements Iterable<Cell> { goToNextState(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); + } + } + + + + 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; + } + } + else { + if(alive == 3){ + return true; + } + else{ + return false; + } + + } - private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { - return false; } 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) { - 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) { + 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. */ 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> { */ 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(); + } + } + } + } + }