Skip to content
Snippets Groups Projects
Commit e0901c67 authored by DURIEZ Kilian's avatar DURIEZ Kilian
Browse files

tp3 fin

parent adb13dc8
Branches master
No related tags found
No related merge requests found
......@@ -4,11 +4,19 @@
public class Cell {
private boolean isAlive;
private String color = "Red";
public void setColor(String hisColor) {
color = hisColor;
}
public String getColor() {
return color;
}
public Cell(){
this.isAlive = false;
this.isAlive = false;
}
/**
* Determines whether this {@link Cell} is alive or not.
*
......@@ -36,7 +44,7 @@ public class Cell {
*/
public void setAlive() {
this.isAlive = true;
this.isAlive = true;
}
/**
......@@ -46,7 +54,7 @@ public class Cell {
*/
public void setDead() {
this.isAlive = false;
this.isAlive = false;
}
......@@ -55,24 +63,24 @@ public class Cell {
*/
public void toggleState() {
if(this.isAlive)
this.isAlive = false;
else
this.isAlive = true;
if(this.isAlive)
this.isAlive = false;
else
this.isAlive = true;
}
public boolean isAliveInNextState(int numberOfAliveNeighbours) {
if(isAlive()){
if (numberOfAliveNeighbours == 2 || numberOfAliveNeighbours == 3)
return true;
else
return false;
}
else{
if (numberOfAliveNeighbours == 3)
return true;
else
return false;
}
if(isAlive()){
if (numberOfAliveNeighbours == 2 || numberOfAliveNeighbours == 3)
return true;
else
return false;
}
else{
if (numberOfAliveNeighbours == 3)
return true;
else
return false;
}
}
}
......@@ -16,8 +16,8 @@ public class GameOfLife {
* @throws NullPointerException if {@code grid} is {@code null}
*/
public GameOfLife(Grid grid) {
// this.grid = requireNonNull(grid, "grid is null");
this.grid = grid;
// this.grid = requireNonNull(grid, "grid is null");
this.grid = grid;
grid.randomGeneration(random);
}
......@@ -26,7 +26,7 @@ public class GameOfLife {
*/
public void next() {
grid.nextGeneration();
generationNumber++;
generationNumber++;
}
......@@ -52,19 +52,19 @@ public class GameOfLife {
* Plays the game.
*/
public void play(int maxGenerations) {
for(int generation =0; generation < maxGenerations; generation++){
this.next();
System.out.println("generation : " + generation);
}
for(int generation =0; generation < maxGenerations; generation++){
this.next();
System.out.println("generation : " + generation);
}
}
/**
* Pauses the game.
*/
public void pause() {
// timeline.pause();
// timeline.pause();
}
/**
......@@ -73,7 +73,7 @@ public class GameOfLife {
public void clear() {
pause();
grid.clear();
generationNumber = 0;
generationNumber = 0;
}
/**
......
......@@ -10,39 +10,46 @@ public class GameOfLifeGUI extends JFrame {
private GridLayout gridLayout;
private JPanel gridPanel;
private JFrame frame;
public GameOfLifeGUI(Grid g) {
this.numberOfRows = g.getNumberOfRows();
this.numberOfColumns = g.getNumberOfColumns();
gridLayout = new GridLayout(numberOfRows, numberOfColumns);
this.numberOfRows = g.getNumberOfRows();
this.numberOfColumns = g.getNumberOfColumns();
gridLayout = new GridLayout(numberOfRows, numberOfColumns);
gridPanel = new JPanel(gridLayout);
labelGrid = new JLabel[numberOfRows][numberOfColumns];
labelGrid = new JLabel[numberOfRows][numberOfColumns];
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())
labelGrid[x][y].setForeground(Color.red);
else
labelGrid[x][y].setForeground(Color.white);
gridPanel.add(labelGrid[x][y]);
}
labelGrid[x][y] = new JLabel("*");
JLabel label;
if(g.getCell(x,y).isAlive())
labelGrid[x][y].setForeground(Color.red);
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);
frame.setSize(squareSize * numberOfRows, squareSize * numberOfColumns);
frame.setLocationByPlatform(true);
frame.setSize(squareSize * numberOfRows, squareSize * numberOfColumns);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void update(Grid g){
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())
label.setForeground(Color.red);
else
label.setForeground(Color.white);
}
JLabel label = labelGrid[x][y];
if(g.getCell(x,y).isAlive()) {
if ((g.getCell(x,y).getColor()).equals("Red")) {
label.setForeground(Color.red);
} else {
label.setForeground(Color.blue);
}
}
else
label.setForeground(Color.white);
}
}
}
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,94 @@ public class Grid implements Iterable<Cell> {
}
private boolean[][] calculateNextStates() {
return null;
boolean[][] nextState = new boolean[getNumberOfRows()][getNumberOfColumns()] ;
for(int row=0; row<numberOfRows; row++) {
for(int column = 0; column<numberOfColumns; column++){
nextState[row][column] = this.calculateNextState(row, column, this.getCell(row, column));
}
}
return nextState;
}
private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) {
return false;
boolean nextState = cell.isAliveInNextState(this.countAliveNeighbours(rowIndex, columnIndex));
if(nextState) {
if(this.countAliveNeighbours(rowIndex, columnIndex) != 2) {
cell.setColor(this.countColorNeighbours(rowIndex, columnIndex));
}
}
return nextState;
}
private String countColorNeighbours(int rowIndex, int columnIndex) {
String countColorCell;
int Red = 0;
int Blue = 0;
for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
if ((NeighboursCell.getColor()).equals("Red")) {
Red+=1;
}
else {
Blue+=1;
}
}
}
if (Red>Blue) {
countColorCell="Red";
}
else {
countColorCell="Blue";
}
return countColorCell;
}
private int countAliveNeighbours(int rowIndex, int columnIndex) {
return 0;
int alive = 0;
for(Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) {
if (NeighboursCell.isAlive()) {
alive = alive + 1;
}
}
return alive;
}
private List<Cell> getNeighbours(int rowIndex, int columnIndex) {
return null;
List<Cell> neighbours = new ArrayList<Cell>();
neighbours.add(this.getCell(rowIndex-1, columnIndex-1));
neighbours.add(this.getCell(rowIndex-1, columnIndex));
neighbours.add(this.getCell(rowIndex-1, columnIndex+1));
neighbours.add(this.getCell(rowIndex+1, columnIndex-1));
neighbours.add(this.getCell(rowIndex+1, columnIndex));
neighbours.add(this.getCell(rowIndex+1, columnIndex+1));
neighbours.add(this.getCell(rowIndex, columnIndex-1));
neighbours.add(this.getCell(rowIndex, columnIndex+1));
return neighbours;
}
private void goToNextState(boolean[][] nextState) {
for (int row = 0; row < numberOfRows; row++) {
for (int column = 0; column < numberOfColumns; column++) {
if (nextState[row][column])
getCell(row, column).setAlive();
else
getCell(row, column).setDead();
}
}
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void clear() {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
cell.setDead();
}
}
/**
......@@ -129,8 +191,25 @@ public class Grid implements Iterable<Cell> {
* @param random {@link Random} instance used to decide if each {@link Cell} is alive or dead
* @throws NullPointerException if {@code random} is {@code null}
*/
void randomGeneration(Random random) {
for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) {
Cell cell = it.next();
if (random.nextBoolean()) {
cell.setAlive();
if (random.nextBoolean()) {
cell.setColor("Red");
}
else {
cell.setColor("Blue");
}
}
else {
cell.setDead();
}
}
}
}
......@@ -4,22 +4,22 @@ import javax.swing.*;
public class Main{
public static void main(String args[]) throws IOException {
int NUMBER_OF_ROWS = 64;
int NUMBER_OF_COLUMNS = 64;
GameOfLife gameOfLife = new GameOfLife(new Grid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS));
GameOfLifeGUI gui = new GameOfLifeGUI(gameOfLife.getGrid());
int NUMBER_OF_ROWS = 64;
int NUMBER_OF_COLUMNS = 64;
for(int generation =0; generation < 1000; generation++){
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
GameOfLife gameOfLife = new GameOfLife(new Grid(NUMBER_OF_ROWS, NUMBER_OF_COLUMNS));
GameOfLifeGUI gui = new GameOfLifeGUI(gameOfLife.getGrid());
for(int generation =0; generation < 1000; generation++){
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
gameOfLife.next();
gui.update(gameOfLife.getGrid());
}
}
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