3 files + 112 − 15 Side-by-side Compare changes Side-by-side Inline Show whitespace changes Files 3 Cell.java +7 −1 Original line number Original line Diff line number Diff line Loading @@ -5,10 +5,16 @@ public class Cell { public class Cell { private boolean isAlive; private boolean isAlive; private String color = "Red"; public Cell(){ public Cell(){ this.isAlive = false; this.isAlive = false; } } public void setColor(String hisColor) { color = hisColor; } public String getColor() { return color; } /** /** * Determines whether this {@link Cell} is alive or not. * Determines whether this {@link Cell} is alive or not. * * Loading GameOfLifeGUI.java +7 −2 Original line number Original line Diff line number Diff line Loading @@ -39,8 +39,13 @@ public class GameOfLifeGUI extends JFrame { for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++) for (int y = 0; y < numberOfRows; y++){ for (int y = 0; y < numberOfRows; y++){ JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y]; if(g.getCell(x,y).isAlive()) if(g.getCell(x,y).isAlive()) { if ((g.getCell(x,y).getColor()).equals("Red")) { label.setForeground(Color.red); label.setForeground(Color.red); } else { label.setForeground(Color.blue); } } else else label.setForeground(Color.white); label.setForeground(Color.white); } } Loading Grid.java +98 −12 Original line number Original line Diff line number Diff line import java.util.Arrays; import java.util.*; import java.util.Iterator; import java.util.List; import java.util.Random; /** /** * {@code Grid} instances represent the grid in <i>The Game of Life</i>. * {@code Grid} instances represent the grid in <i>The Game of Life</i>. Loading Loading @@ -98,29 +95,101 @@ public class Grid implements Iterable<Cell> { } } private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() { return null; boolean[][] newState = new boolean[getNumberOfRows()][getNumberOfColumns()]; for (int r=0; r<numberOfRows; r++) { for (int c=0; c<numberOfColumns; c++) { newState[r][c] = this.calculateNextState(r,c,this.getCell(r,c)); } } return newState; } private void goToNextState(boolean[][] nextState) { for (int r=0; r<numberOfRows; r++) { for (int c=0; c<numberOfColumns; c++) { if (nextState[r][c]) { this.getCell(r,c).setAlive(); } else { this.getCell(r,c).setDead(); } } } } } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { 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 int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) { return 0; int CounterAliveCell = 0; for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) { if (NeighboursCell.isAlive()) { CounterAliveCell+=1; } } return CounterAliveCell; } private String countColorNeighbours(int rowIndex, int columnIndex) { String CounterColorCell; 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) { CounterColorCell="Red"; } else { CounterColorCell="Blue"; } return CounterColorCell; } } private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) { return null; List<Cell> Neighbours = new ArrayList<>(); for (int r=-1; r<=1; r++) { for (int c=-1; c<=1; c++) { if ((r != 0) || (c != 0)) { Neighbours.add(this.getCell(rowIndex + r, columnIndex + c)); } } } } private void goToNextState(boolean[][] nextState) { return Neighbours; } } /** /** * Sets all {@link Cell}s in this {@code Grid} as dead. * Sets all {@link Cell}s in this {@code Grid} as dead. */ */ void clear() { void clear() { for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) { Cell cell = it.next(); cell.setDead(); } } } /** /** Loading @@ -131,6 +200,23 @@ public class Grid implements Iterable<Cell> { */ */ void randomGeneration(Random random) { 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(); } } } } } } No newline at end of file
Cell.java +7 −1 Original line number Original line Diff line number Diff line Loading @@ -5,10 +5,16 @@ public class Cell { public class Cell { private boolean isAlive; private boolean isAlive; private String color = "Red"; public Cell(){ public Cell(){ this.isAlive = false; this.isAlive = false; } } public void setColor(String hisColor) { color = hisColor; } public String getColor() { return color; } /** /** * Determines whether this {@link Cell} is alive or not. * Determines whether this {@link Cell} is alive or not. * * Loading
GameOfLifeGUI.java +7 −2 Original line number Original line Diff line number Diff line Loading @@ -39,8 +39,13 @@ public class GameOfLifeGUI extends JFrame { for (int x = 0; x < numberOfColumns; x++) for (int x = 0; x < numberOfColumns; x++) for (int y = 0; y < numberOfRows; y++){ for (int y = 0; y < numberOfRows; y++){ JLabel label = labelGrid[x][y]; JLabel label = labelGrid[x][y]; if(g.getCell(x,y).isAlive()) if(g.getCell(x,y).isAlive()) { if ((g.getCell(x,y).getColor()).equals("Red")) { label.setForeground(Color.red); label.setForeground(Color.red); } else { label.setForeground(Color.blue); } } else else label.setForeground(Color.white); label.setForeground(Color.white); } } Loading
Grid.java +98 −12 Original line number Original line Diff line number Diff line import java.util.Arrays; import java.util.*; import java.util.Iterator; import java.util.List; import java.util.Random; /** /** * {@code Grid} instances represent the grid in <i>The Game of Life</i>. * {@code Grid} instances represent the grid in <i>The Game of Life</i>. Loading Loading @@ -98,29 +95,101 @@ public class Grid implements Iterable<Cell> { } } private boolean[][] calculateNextStates() { private boolean[][] calculateNextStates() { return null; boolean[][] newState = new boolean[getNumberOfRows()][getNumberOfColumns()]; for (int r=0; r<numberOfRows; r++) { for (int c=0; c<numberOfColumns; c++) { newState[r][c] = this.calculateNextState(r,c,this.getCell(r,c)); } } return newState; } private void goToNextState(boolean[][] nextState) { for (int r=0; r<numberOfRows; r++) { for (int c=0; c<numberOfColumns; c++) { if (nextState[r][c]) { this.getCell(r,c).setAlive(); } else { this.getCell(r,c).setDead(); } } } } } private boolean calculateNextState(int rowIndex, int columnIndex, Cell cell) { 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 int countAliveNeighbours(int rowIndex, int columnIndex) { private int countAliveNeighbours(int rowIndex, int columnIndex) { return 0; int CounterAliveCell = 0; for (Cell NeighboursCell : this.getNeighbours(rowIndex, columnIndex)) { if (NeighboursCell.isAlive()) { CounterAliveCell+=1; } } return CounterAliveCell; } private String countColorNeighbours(int rowIndex, int columnIndex) { String CounterColorCell; 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) { CounterColorCell="Red"; } else { CounterColorCell="Blue"; } return CounterColorCell; } } private List<Cell> getNeighbours(int rowIndex, int columnIndex) { private List<Cell> getNeighbours(int rowIndex, int columnIndex) { return null; List<Cell> Neighbours = new ArrayList<>(); for (int r=-1; r<=1; r++) { for (int c=-1; c<=1; c++) { if ((r != 0) || (c != 0)) { Neighbours.add(this.getCell(rowIndex + r, columnIndex + c)); } } } } private void goToNextState(boolean[][] nextState) { return Neighbours; } } /** /** * Sets all {@link Cell}s in this {@code Grid} as dead. * Sets all {@link Cell}s in this {@code Grid} as dead. */ */ void clear() { void clear() { for (Iterator<Cell> it = this.iterator(); it.hasNext(); ) { Cell cell = it.next(); cell.setDead(); } } } /** /** Loading @@ -131,6 +200,23 @@ public class Grid implements Iterable<Cell> { */ */ void randomGeneration(Random random) { 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(); } } } } } } No newline at end of file