|
| 1 | +import java.awt.Toolkit; |
| 2 | +import javax.swing.JFrame; |
| 3 | + |
| 4 | +/** |
| 5 | + * Conway's Game of Life. |
| 6 | + */ |
| 7 | +public class Conway { |
| 8 | + |
| 9 | + /** |
| 10 | + * Counts the number of live neighbors (without going out of bounds). |
| 11 | + * |
| 12 | + * @param grid the grid |
| 13 | + * @param r row index |
| 14 | + * @param c column index |
| 15 | + * @return number of live neighbors |
| 16 | + */ |
| 17 | + public static int countAlive(Grid grid, int r, int c) { |
| 18 | + int count = 0; |
| 19 | + int cols = grid.getCols(); |
| 20 | + int rows = grid.getRows(); |
| 21 | + |
| 22 | + // previous row |
| 23 | + if (r > 0) { |
| 24 | + if (c > 0 && grid.isOn(r - 1, c - 1)) { |
| 25 | + count++; |
| 26 | + } |
| 27 | + if (grid.isOn(r - 1, c)) { |
| 28 | + count++; |
| 29 | + } |
| 30 | + if (c < cols - 1 && grid.isOn(r - 1, c + 1)) { |
| 31 | + count++; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // current row |
| 36 | + if (c > 0 && grid.isOn(r, c - 1)) { |
| 37 | + count++; |
| 38 | + } |
| 39 | + if (c < cols - 1 && grid.isOn(r, c + 1)) { |
| 40 | + count++; |
| 41 | + } |
| 42 | + |
| 43 | + // next row |
| 44 | + if (r < rows - 1) { |
| 45 | + if (c > 0 && grid.isOn(r + 1, c - 1)) { |
| 46 | + count++; |
| 47 | + } |
| 48 | + if (grid.isOn(r + 1, c)) { |
| 49 | + count++; |
| 50 | + } |
| 51 | + if (c < cols - 1 && grid.isOn(r + 1, c + 1)) { |
| 52 | + count++; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return count; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Apply the rules of Conway's Game of Life. |
| 61 | + * |
| 62 | + * @param cell the cell to update |
| 63 | + * @param count number of live neighbors |
| 64 | + */ |
| 65 | + public static void updateCell(Cell cell, int count) { |
| 66 | + if (cell.isOn()) { |
| 67 | + if (count < 2) { |
| 68 | + // Any live cell with fewer than two live neighbors dies, |
| 69 | + // as if by underpopulation. |
| 70 | + cell.turnOff(); |
| 71 | + } else if (count > 3) { |
| 72 | + // Any live cell with more than three live neighbors dies, |
| 73 | + // as if by overpopulation. |
| 74 | + cell.turnOff(); |
| 75 | + } else { |
| 76 | + // Any live cell with two or three live neighbors lives on |
| 77 | + // to the next generation. |
| 78 | + cell.turnOn(); |
| 79 | + } |
| 80 | + } else { |
| 81 | + if (count == 3) { |
| 82 | + // Any dead cell with exactly three live neighbors |
| 83 | + // becomes a live cell, as if by reproduction. |
| 84 | + cell.turnOn(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Simulates one round of Conway's Game of Life. |
| 91 | + * |
| 92 | + * @param grid the grid |
| 93 | + */ |
| 94 | + public static void update(Grid grid) { |
| 95 | + int cols = grid.getCols(); |
| 96 | + int rows = grid.getRows(); |
| 97 | + |
| 98 | + // count neighbors before changing anything |
| 99 | + int[][] counts = new int[rows][cols]; |
| 100 | + for (int r = 0; r < rows; r++) { |
| 101 | + for (int c = 0; c < cols; c++) { |
| 102 | + counts[r][c] = countAlive(grid, r, c); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // update each cell based on neighbor counts |
| 107 | + for (int r = 0; r < rows; r++) { |
| 108 | + for (int c = 0; c < cols; c++) { |
| 109 | + updateCell(grid.getCell(r, c), counts[r][c]); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Sets up the grid, creates the drawing, and plays the game. |
| 116 | + * |
| 117 | + * @param args command-line arguments |
| 118 | + */ |
| 119 | + public static void main(String[] args) { |
| 120 | + |
| 121 | + Grid grid = new Grid(30, 25, 20); |
| 122 | + grid.flip(1, 2); |
| 123 | + grid.flip(2, 2); |
| 124 | + grid.flip(3, 2); |
| 125 | + Drawing drawing = new Drawing(grid); |
| 126 | + |
| 127 | + // set up the window frame |
| 128 | + JFrame frame = new JFrame("Conway's Game of Life"); |
| 129 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 130 | + frame.setResizable(false); |
| 131 | + frame.add(drawing); |
| 132 | + frame.pack(); |
| 133 | + frame.setVisible(true); |
| 134 | + |
| 135 | + // main simulation loop |
| 136 | + Toolkit toolkit = frame.getToolkit(); |
| 137 | + while (true) { |
| 138 | + |
| 139 | + // update the drawing |
| 140 | + update(grid); |
| 141 | + drawing.repaint(); |
| 142 | + |
| 143 | + // delay the simulation |
| 144 | + try { |
| 145 | + Thread.sleep(500); |
| 146 | + toolkit.sync(); |
| 147 | + } catch (InterruptedException e) { |
| 148 | + // do nothing |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments