11import java .awt .Canvas ;
2- import java .awt .Color ;
32import java .awt .Graphics ;
43
54/**
6- * 2D array of cells.
5+ * Drawing of a 2D array of cells.
76 */
87public class Grid extends Canvas {
98
@@ -42,12 +41,12 @@ public Grid(int rows, int cols, int size) {
4241 * @param r row index
4342 * @param c column index
4443 */
45- public void flip (int r , int c ) {
44+ public void toggle (int r , int c ) {
4645 Cell cell = array [r ][c ];
47- if (cell .alive ()) {
48- cell .setColor ( Color . WHITE );
46+ if (cell .isOff ()) {
47+ cell .turnOn ( );
4948 } else {
50- cell .setColor ( Color . BLACK );
49+ cell .turnOff ( );
5150 }
5251 }
5352
@@ -58,39 +57,39 @@ public void flip(int r, int c) {
5857 * @param c column index
5958 * @return number of live neighbors
6059 */
61- public int countLive (int r , int c ) {
60+ public int countAlive (int r , int c ) {
6261 int count = 0 ;
6362
6463 // previous row
6564 if (r > 0 ) {
66- if (c > 0 && array [r - 1 ][c - 1 ].alive ()) {
65+ if (c > 0 && array [r - 1 ][c - 1 ].isOn ()) {
6766 count ++;
6867 }
69- if (array [r - 1 ][c ].alive ()) {
68+ if (array [r - 1 ][c ].isOn ()) {
7069 count ++;
7170 }
72- if (c < cols - 1 && array [r - 1 ][c + 1 ].alive ()) {
71+ if (c < cols - 1 && array [r - 1 ][c + 1 ].isOn ()) {
7372 count ++;
7473 }
7574 }
7675
7776 // current row
78- if (c > 0 && array [r ][c - 1 ].alive ()) {
77+ if (c > 0 && array [r ][c - 1 ].isOn ()) {
7978 count ++;
8079 }
81- if (c < cols - 1 && array [r ][c + 1 ].alive ()) {
80+ if (c < cols - 1 && array [r ][c + 1 ].isOn ()) {
8281 count ++;
8382 }
8483
8584 // next row
8685 if (r < rows - 1 ) {
87- if (c > 0 && array [r + 1 ][c - 1 ].alive ()) {
86+ if (c > 0 && array [r + 1 ][c - 1 ].isOn ()) {
8887 count ++;
8988 }
90- if (array [r + 1 ][c ].alive ()) {
89+ if (array [r + 1 ][c ].isOn ()) {
9190 count ++;
9291 }
93- if (c < cols - 1 && array [r + 1 ][c + 1 ].alive ()) {
92+ if (c < cols - 1 && array [r + 1 ][c + 1 ].isOn ()) {
9493 count ++;
9594 }
9695 }
@@ -105,25 +104,25 @@ public int countLive(int r, int c) {
105104 * @param count number of live neighbors
106105 */
107106 public void updateCell (Cell cell , int count ) {
108- if (cell .alive ()) {
107+ if (cell .isOn ()) {
109108 if (count < 2 ) {
110109 // Any live cell with fewer than two live neighbors dies,
111110 // as if by underpopulation.
112- cell .setColor ( Color . WHITE );
111+ cell .turnOff ( );
113112 } else if (count > 3 ) {
114113 // Any live cell with more than three live neighbors dies,
115114 // as if by overpopulation.
116- cell .setColor ( Color . WHITE );
115+ cell .turnOff ( );
117116 } else {
118117 // Any live cell with two or three live neighbors lives on
119118 // to the next generation.
120- cell .setColor ( Color . BLACK );
119+ cell .turnOn ( );
121120 }
122121 } else {
123122 if (count == 3 ) {
124123 // Any dead cell with exactly three live neighbors
125124 // becomes a live cell, as if by reproduction.
126- cell .setColor ( Color . BLACK );
125+ cell .turnOn ( );
127126 }
128127 }
129128 }
@@ -133,19 +132,18 @@ public void updateCell(Cell cell, int count) {
133132 */
134133 public void playGame () {
135134
136- // count neighbors
135+ // count neighbors before changing anything
137136 int [][] counts = new int [rows ][cols ];
138137 for (int r = 0 ; r < rows ; r ++) {
139138 for (int c = 0 ; c < cols ; c ++) {
140- counts [r ][c ] = countLive (r , c );
139+ counts [r ][c ] = countAlive (r , c );
141140 }
142141 }
143142
144- // update cells
143+ // update each cell based on neighbor counts
145144 for (int r = 0 ; r < rows ; r ++) {
146145 for (int c = 0 ; c < cols ; c ++) {
147- Cell cell = array [r ][c ];
148- updateCell (cell , counts [r ][c ]);
146+ updateCell (array [r ][c ], counts [r ][c ]);
149147 }
150148 }
151149 }
@@ -155,6 +153,7 @@ public void playGame() {
155153 *
156154 * @param g graphics context
157155 */
156+ @ Override
158157 public void paint (Graphics g ) {
159158 for (Cell [] row : array ) {
160159 for (Cell cell : row ) {
@@ -164,11 +163,13 @@ public void paint(Graphics g) {
164163 }
165164
166165 /**
167- * Overriding this method makes the simulation more smooth, because the
168- * Canvas does not need to be cleared before redrawing.
166+ * Overriding this method helps the simulation run more smoothly. Normally
167+ * the Canvas is cleared before painting, but there is no need to clear it
168+ * since paint draws the entire grid.
169169 *
170170 * @param g graphics context
171171 */
172+ @ Override
172173 public void update (Graphics g ) {
173174 paint (g );
174175 }
0 commit comments