From e0901c678b7b06167c3722ffe209cb56bff1726b Mon Sep 17 00:00:00 2001
From: Duriez <kilian.duriez@etu.univ-amu.fr>
Date: Sun, 18 Oct 2020 14:44:34 +0200
Subject: [PATCH] tp3 fin

---
 Cell.java          | 48 +++++++++++++----------
 GameOfLife.java    | 22 +++++------
 GameOfLifeGUI.java | 49 +++++++++++++----------
 Grid.java          | 97 +++++++++++++++++++++++++++++++++++++++++-----
 Main.java          | 30 +++++++-------
 5 files changed, 170 insertions(+), 76 deletions(-)

diff --git a/Cell.java b/Cell.java
index 9a0b4bd..c529da1 100644
--- a/Cell.java
+++ b/Cell.java
@@ -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;
+        }
     }
 }
diff --git a/GameOfLife.java b/GameOfLife.java
index 1d5bb85..5d8b4aa 100644
--- a/GameOfLife.java
+++ b/GameOfLife.java
@@ -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;
     }
 
     /**
diff --git a/GameOfLifeGUI.java b/GameOfLifeGUI.java
index c162ce0..2cd8f9e 100644
--- a/GameOfLifeGUI.java
+++ b/GameOfLifeGUI.java
@@ -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);
+            }
     }
 }
+
+
diff --git a/Grid.java b/Grid.java
index 939e05f..8554e3a 100644
--- a/Grid.java
+++ b/Grid.java
@@ -1,7 +1,4 @@
-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();
+
+            }
+        }
+
     }
 
 }
diff --git a/Main.java b/Main.java
index bf344ee..c5ca292 100644
--- a/Main.java
+++ b/Main.java
@@ -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());
-	}
-	
     }
 }
-- 
GitLab