Skip to content
Snippets Groups Projects
Commit 5e2d9612 authored by HAMADACHE Mohamed's avatar HAMADACHE Mohamed
Browse files

ok

parent 04a2093b
No related branches found
No related tags found
No related merge requests found
...@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame { ...@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
gridLayout = new GridLayout(numberOfRows, numberOfColumns); gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout); gridPanel = new JPanel(gridLayout);
labelGrid = new JLabel[numberOfRows][numberOfColumns]; labelGrid = new JLabel[numberOfRows][numberOfColumns];
for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++) {
for (int y = 0; y < numberOfRows; y++) { for (int y = 0; y < numberOfRows; y++) {
labelGrid[x][y] = new JLabel("*"); labelGrid[x][y] = new JLabel("*");
JLabel label; JLabel label;
if(g.getCell(x,y).isAlive()) if (g.getCell(x, y).isAlive()) {
if (g.getCell(x, y).isRed()) {
labelGrid[x][y].setForeground(Color.red); labelGrid[x][y].setForeground(Color.red);
else } else {
labelGrid[x][y].setForeground(Color.blue);
}
} else {
labelGrid[x][y].setForeground(Color.white); labelGrid[x][y].setForeground(Color.white);
}
gridPanel.add(labelGrid[x][y]); gridPanel.add(labelGrid[x][y]);
} }
}
frame = new JFrame("Game of Life"); frame = new JFrame("Game of Life");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(gridPanel); frame.setContentPane(gridPanel);
...@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame { ...@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
} }
public void update(Grid g) { public void update(Grid g) {
for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++) {
for (int y = 0; y < numberOfRows; y++) { for (int y = 0; y < numberOfRows; y++) {
JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) if (g.getCell(x, y).isAlive()) {
if (g.getCell(x, y).isBlue()) {
label.setForeground(Color.blue);
} else {
label.setForeground(Color.red); label.setForeground(Color.red);
else }
} else {
label.setForeground(Color.white); label.setForeground(Color.white);
} }
} }
} }
}
}
import java.util.Arrays; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> { ...@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
} }
private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() {
return null; boolean[][] states = new boolean[numberOfRows][numberOfColumns];
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < numberOfColumns; j++) {
states[i][j] = calculateNextState(i, j, cells[i][j]);
}
}
return states;
} }
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false; int neighboursAlive = countAliveNeighbours(rowIndex, columnIndex);
int blueNeighbours = countBlueNeighbours(rowIndex, columnIndex);
boolean nextStateAlive = cell.isAlive();
if (cell.isAlive()) {
if (neighboursAlive == 2) {
nextStateAlive = true;
}
else if (neighboursAlive == 3) {
nextStateAlive = true;
}
else {
cell.isDead();
nextStateAlive = false;
}
}
else if (cell.isDead()) {
if (neighboursAlive == 3) {
cell.isAlive();
nextStateAlive = true;
if (blueNeighbours >= 2) {
cell.setBlue();
}
else {
cell.setRed();
}
}
else {
nextStateAlive = false;
}
}
return nextStateAlive;
} }
private int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0; int nbAlive = 0;
for (Cell cell : getNeighbours(rowIndex, columnIndex)) {
nbAlive += cell.isAlive() ? 1 : 0;
}
return nbAlive;
}
private int countBlueNeighbours(int rowIndex, int columnIndex) {
int nbBlue = 0;
for (Cell cell : getNeighbours(rowIndex, columnIndex)) {
if (cell.isAlive()) {
nbBlue += cell.isBlue() ? 1 : 0;
}
}
return nbBlue;
} }
private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null; List<Cell> neighbourCells = new ArrayList<Cell>();
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex - 1 < 0 ? numberOfColumns - 1 : columnIndex - 1]);
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex]);
neighbourCells.add(cells[rowIndex - 1 < 0 ? numberOfRows - 1 : rowIndex - 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
neighbourCells.add(cells[rowIndex][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
neighbourCells.add(cells[rowIndex + 1 > numberOfRows - 1 ? 0 : rowIndex + 1][columnIndex + 1 > numberOfColumns - 1 ? 0 : columnIndex + 1]);
return neighbourCells;
} }
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] == 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,21 @@ public class Grid implements Iterable<Cell> { ...@@ -131,6 +212,21 @@ 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()) {
cells[i][j].setAlive();
if (random.nextBoolean()) {
cells[i][j].setBlue();
} else {
cells[i][j].setRed();
}
} else {
cells[i][j].setDead();
}
}
}
} }
} }
...@@ -17,6 +17,7 @@ public class Main{ ...@@ -17,6 +17,7 @@ public class Main{
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
} }
gameOfLife.next(); gameOfLife.next();
gui.update(gameOfLife.getGrid()); gui.update(gameOfLife.getGrid());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment