|
| 1 | +import java.awt.Canvas; |
| 2 | +import java.awt.Color; |
| 3 | +import java.awt.Graphics; |
| 4 | + |
| 5 | +/** |
| 6 | + * 2D array of cells. |
| 7 | + */ |
| 8 | +public class Grid extends Canvas { |
| 9 | + |
| 10 | + private final int rows; |
| 11 | + private final int cols; |
| 12 | + |
| 13 | + /** Cells stored in row-major order. */ |
| 14 | + private Cell[][] array; |
| 15 | + |
| 16 | + /** |
| 17 | + * Constructs a drawing of given size. |
| 18 | + * |
| 19 | + * @param rows number of rows |
| 20 | + * @param cols number of columns |
| 21 | + * @param size pixels per cell |
| 22 | + */ |
| 23 | + public Grid(int rows, int cols, int size) { |
| 24 | + |
| 25 | + // build 2D array of cells |
| 26 | + this.rows = rows; |
| 27 | + this.cols = cols; |
| 28 | + this.array = new Cell[rows][cols]; |
| 29 | + for (int r = 0; r < rows; r++) { |
| 30 | + for (int c = 0; c < cols; c++) { |
| 31 | + this.array[r][c] = new Cell(c * size, r * size, size); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // initialize the display |
| 36 | + setSize(cols * size, rows * size); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Toggles the cell color. |
| 41 | + * |
| 42 | + * @param r row index |
| 43 | + * @param c column index |
| 44 | + */ |
| 45 | + public void flip(int r, int c) { |
| 46 | + Cell cell = array[r][c]; |
| 47 | + if (cell.alive()) { |
| 48 | + cell.setColor(Color.WHITE); |
| 49 | + } else { |
| 50 | + cell.setColor(Color.BLACK); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Counts the number of live neighbors (without going out of bounds). |
| 56 | + * |
| 57 | + * @param r row index |
| 58 | + * @param c column index |
| 59 | + * @return number of live neighbors |
| 60 | + */ |
| 61 | + public int countLive(int r, int c) { |
| 62 | + int count = 0; |
| 63 | + |
| 64 | + // previous row |
| 65 | + if (r > 0) { |
| 66 | + if (c > 0 && array[r - 1][c - 1].alive()) { |
| 67 | + count++; |
| 68 | + } |
| 69 | + if (array[r - 1][c].alive()) { |
| 70 | + count++; |
| 71 | + } |
| 72 | + if (c < cols - 1 && array[r - 1][c + 1].alive()) { |
| 73 | + count++; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // current row |
| 78 | + if (c > 0 && array[r][c - 1].alive()) { |
| 79 | + count++; |
| 80 | + } |
| 81 | + if (c < cols - 1 && array[r][c + 1].alive()) { |
| 82 | + count++; |
| 83 | + } |
| 84 | + |
| 85 | + // next row |
| 86 | + if (r < rows - 1) { |
| 87 | + if (c > 0 && array[r + 1][c - 1].alive()) { |
| 88 | + count++; |
| 89 | + } |
| 90 | + if (array[r + 1][c].alive()) { |
| 91 | + count++; |
| 92 | + } |
| 93 | + if (c < cols - 1 && array[r + 1][c + 1].alive()) { |
| 94 | + count++; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return count; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Apply the rules of Conway's Game of Life. |
| 103 | + * |
| 104 | + * @param cell the cell to update |
| 105 | + * @param count number of live neighbors |
| 106 | + */ |
| 107 | + public void updateCell(Cell cell, int count) { |
| 108 | + if (cell.alive()) { |
| 109 | + if (count < 2) { |
| 110 | + // Any live cell with fewer than two live neighbors dies, |
| 111 | + // as if by underpopulation. |
| 112 | + cell.setColor(Color.WHITE); |
| 113 | + } else if (count > 3) { |
| 114 | + // Any live cell with more than three live neighbors dies, |
| 115 | + // as if by overpopulation. |
| 116 | + cell.setColor(Color.WHITE); |
| 117 | + } else { |
| 118 | + // Any live cell with two or three live neighbors lives on |
| 119 | + // to the next generation. |
| 120 | + cell.setColor(Color.BLACK); |
| 121 | + } |
| 122 | + } else { |
| 123 | + if (count == 3) { |
| 124 | + // Any dead cell with exactly three live neighbors |
| 125 | + // becomes a live cell, as if by reproduction. |
| 126 | + cell.setColor(Color.BLACK); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Simulates one round of Conway's Game of Life. |
| 133 | + */ |
| 134 | + public void playGame() { |
| 135 | + |
| 136 | + // count neighbors |
| 137 | + int[][] counts = new int[rows][cols]; |
| 138 | + for (int r = 0; r < rows; r++) { |
| 139 | + for (int c = 0; c < cols; c++) { |
| 140 | + counts[r][c] = countLive(r, c); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // update cells |
| 145 | + for (int r = 0; r < rows; r++) { |
| 146 | + for (int c = 0; c < cols; c++) { |
| 147 | + Cell cell = array[r][c]; |
| 148 | + updateCell(cell, counts[r][c]); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Draws the grid, cell by cell. |
| 155 | + * |
| 156 | + * @param g graphics context |
| 157 | + */ |
| 158 | + public void paint(Graphics g) { |
| 159 | + for (Cell[] row : array) { |
| 160 | + for (Cell cell : row) { |
| 161 | + cell.draw(g); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Overriding this method makes the simulation more smooth, because the |
| 168 | + * Canvas does not need to be cleared before redrawing. |
| 169 | + * |
| 170 | + * @param g graphics context |
| 171 | + */ |
| 172 | + public void update(Graphics g) { |
| 173 | + paint(g); |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments