diff --git a/Cell.java b/Cell.java index dab8cf07c7c8440fe22822f89dfa27221db7fbb9..1f89f6d74cd35e91e98e3eb4e14346046951cae6 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 0ad28eee391453edb5d128d29cda745e19e9b822..d8b32ee826f6514ecef42d0f5538937ab6bca48d 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 742f6aa76704c129c8d1217b7a4b4d880172cbf0..7bba0ae1684523c8770fc80c22a741f145795142 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(); }