From ad595afabb02f4572a05d5198c99ce336b8eb548 Mon Sep 17 00:00:00 2001
From: etienne <etiennemuller12@gmail.com>
Date: Tue, 13 Oct 2020 18:45:44 +0200
Subject: [PATCH] =?UTF-8?q?bug=20de=20r=C3=A9partition=20des=20couleur=20c?=
 =?UTF-8?q?orrig=C3=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Cell.java          |  6 +++--
 GameOfLifeGUI.java | 57 ++++++++++++++++++++++------------------------
 Grid.java          |  8 +++----
 3 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/Cell.java b/Cell.java
index dab8cf0..1f89f6d 100644
--- a/Cell.java
+++ b/Cell.java
@@ -1,3 +1,5 @@
+import java.util.Random;
+
 /**
  * {@link Cell} instances represent the cells of <i>The Game of Life</i>.
  */
@@ -9,7 +11,7 @@ public class Cell {
 
     public Cell(){
 	this.isAlive = false;
-	this.isRed = false;
+	this.isRed = new Random().nextBoolean();
 
     }
 
@@ -18,7 +20,7 @@ public class Cell {
     }
 
     public boolean isRed(){
-        return isRed;
+        return this.isRed;
     }
     /**
      * Determines whether this {@link Cell} is alive or not.
diff --git a/GameOfLifeGUI.java b/GameOfLifeGUI.java
index 0ad28ee..d8b32ee 100644
--- a/GameOfLifeGUI.java
+++ b/GameOfLifeGUI.java
@@ -10,45 +10,42 @@ 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);
-		    //if (g.getCell(x,y).isRed()) labelGrid[x][y].setForeground(Color.red);
-            //else labelGrid[x][y].setForeground(Color.BLUE);
-        }
-		else
-		    labelGrid[x][y].setForeground(Color.white);
-		gridPanel.add(labelGrid[x][y]);
-	    }
+            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);
+                    //if (g.getCell(x,y).isRed()) labelGrid[x][y].setForeground(Color.red);
+                    //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);
-	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){
+    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()){
-		    if (g.getCell(x,y).isRed()) label.setForeground(Color.red);
-		    else label.setForeground(Color.BLUE);
-        }
-
-		else
-		    label.setForeground(Color.white);
-	    }
+            for (int y = 0; y < numberOfRows; y++) {
+                JLabel label = labelGrid[x][y];
+                if (g.getCell(x, y).isAlive()) {
+                    if (g.getCell(x, y).isRed()) label.setForeground(Color.red);
+                    else label.setForeground(Color.BLUE);
+                } else
+                    label.setForeground(Color.white);
+            }
     }
 }
diff --git a/Grid.java b/Grid.java
index 742f6aa..7bba0ae 100644
--- a/Grid.java
+++ b/Grid.java
@@ -202,8 +202,7 @@ public class Grid implements Iterable<Cell> {
     private void goToNextColor(boolean[][] nextColor) {
         for (int row = 0; row < this.numberOfRows; row++){
             for (int col = 0; col < this.numberOfColumns; col++){
-                if (nextColor[row][col]) this.cells[row][col].setColor(true);
-                else this.cells[row][col].setColor(false);
+                this.cells[row][col].setColor(nextColor[row][col]);
             }
         }
 
@@ -230,8 +229,9 @@ public class Grid implements Iterable<Cell> {
     void randomGeneration(Random random) {
         for (int row = 0; row < this.numberOfRows; row++){
             for (int col = 0; col < this.numberOfColumns; col++) {
-                if (random.nextBoolean()){
-                    this.cells[row][col].setColor(random.nextBoolean());
+                int a = random.nextInt(100);
+                if (a<=50){
+                    this.cells[row][col].setColor(a<25);
                     this.cells[row][col].setAlive();
 
                 }
-- 
GitLab