Skip to content
Snippets Groups Projects
Commit 78e8daf7 authored by Hai Dang's avatar Hai Dang
Browse files

Updated Grid

Modification des methodes getNeighbours, countAlivesNeighbours,
calculateNextState, calculateNextStates, goToNextStates.
parent ed6b5d53
Branches
No related tags found
No related merge requests found
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.*;
/**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
......@@ -98,29 +95,61 @@ public class Grid implements Iterable<Cell> {
}
private boolean[][] calculateNextStates() {
return null;
boolean[][] array = new boolean[getNumberOfRows()][getNumberOfColumns()];
for (int collum = 0; collum < getNumberOfColumns(); collum++) {
for (int row = 0; row < getNumberOfRows(); row++) {
array[row][collum] = calculateNextState(row,collum,getCell(row,collum));
}
}
return array;
}
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false;
return cell.isAliveInNextState(countAliveNeighbours(rowIndex,columnIndex));
}
private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0;
int numCellAlive = 0;
for (Cell cell : getNeighbours(rowIndex,columnIndex)){
if(cell.isAlive()){
numCellAlive ++;
}
}
return numCellAlive;
}
private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null;
List<Cell> neighboursList = new ArrayList<>();
neighboursList.add(getCell(rowIndex+1,columnIndex));
neighboursList.add(getCell(rowIndex-1,columnIndex));
neighboursList.add(getCell(rowIndex,columnIndex+1));
neighboursList.add(getCell(rowIndex,columnIndex-1));
neighboursList.add(getCell(rowIndex+1,columnIndex+1));
neighboursList.add(getCell(rowIndex-1,columnIndex-1));
neighboursList.add(getCell(rowIndex-1,columnIndex+1));
neighboursList.add(getCell(rowIndex+1,columnIndex-1));
return neighboursList;
}
private void goToNextState(boolean[][] nextState) {
boolean[][] array = nextState;
for (int collum = 0; collum < getNumberOfColumns(); collum++) {
for (int row = 0; row < getNumberOfRows(); row++) {
if(array[row][collum]){
cells[row][collum].setAlive();
}
else cells[row][collum].setDead();
}
}
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void clear() {
}
/**
......@@ -131,6 +160,7 @@ public class Grid implements Iterable<Cell> {
*/
void randomGeneration(Random random) {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment