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

ok

parent 04a2093b
Branches master
No related tags found
No related merge requests found
......@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout);
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++) {
labelGrid[x][y] = new JLabel("*");
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);
else
} else {
labelGrid[x][y].setForeground(Color.blue);
}
} else {
labelGrid[x][y].setForeground(Color.white);
}
gridPanel.add(labelGrid[x][y]);
}
}
frame = new JFrame("Game of Life");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setContentPane(gridPanel);
......@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
}
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++) {
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);
else
}
} else {
label.setForeground(Color.white);
}
}
}
}
}
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
......@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
}
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) {
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) {
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) {
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) {
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,21 @@ public class Grid implements Iterable<Cell> {
*/
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{
} catch (InterruptedException ie) {
}
gameOfLife.next();
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