Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da0053b

Browse filesBrowse files
committed
Changing cell implementation
1 parent c32030b commit da0053b
Copy full SHA for da0053b

File tree

Expand file treeCollapse file tree

4 files changed

+26
-27
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+26
-27
lines changed
Open diff view settings
Collapse file

‎ch15/Cell.java‎

Copy file name to clipboardExpand all lines: ch15/Cell.java
+13-14Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
*/
77
public class Cell {
88

9-
public static final Color OFF = Color.WHITE;
10-
public static final Color ON = Color.BLACK;
9+
public static final Color colors[] = {Color.WHITE, Color.BLACK};
1110

1211
private final int x;
1312
private final int y;
1413
private final int size;
15-
private Color color;
14+
private int state;
1615

1716
/**
1817
* Constructs a new cell, initially turned off.
@@ -25,7 +24,7 @@ public Cell(int x, int y, int size) {
2524
this.x = x;
2625
this.y = y;
2726
this.size = size;
28-
this.color = OFF;
27+
this.state = 0;
2928
}
3029

3130
/**
@@ -34,38 +33,38 @@ public Cell(int x, int y, int size) {
3433
* @param g graphics context
3534
*/
3635
public void draw(Graphics g) {
37-
g.setColor(this.color);
36+
Color color = colors[this.state];
37+
g.setColor(color);
3838
g.fillRect(x + 1, y + 1, size - 1, size - 1);
3939
g.setColor(Color.LIGHT_GRAY);
4040
g.drawRect(x, y, size, size);
4141
}
4242

4343
/**
44-
* @return true if the color is OFF
44+
* @return true if the cell is OFF
4545
*/
4646
public boolean isOff() {
47-
return color == OFF;
47+
return state == 0;
4848
}
4949

5050
/**
51-
* @return true if the color is ON
51+
* @return true if the cell is ON
5252
*/
5353
public boolean isOn() {
54-
return color == ON;
54+
return state == 1;
5555
}
5656

5757
/**
58-
* Sets the cell's color to OFF.
58+
* Sets the cell's state to OFF.
5959
*/
6060
public void turnOff() {
61-
color = OFF;
61+
state = 0;
6262
}
6363

6464
/**
65-
* Sets the cell's color to ON.
65+
* Sets the cell's state to ON.
6666
*/
6767
public void turnOn() {
68-
color = ON;
68+
state = 1;
6969
}
70-
7170
}
Collapse file

‎ch15/Conway.java‎

Copy file name to clipboardExpand all lines: ch15/Conway.java
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public class Conway extends Automaton {
1313
*/
1414
public Conway() {
1515
grid = new GridCanvas(30, 25, SIZE);
16-
grid.init(1, 2);
17-
grid.init(2, 2);
18-
grid.init(3, 2);
19-
grid.init(6, 1);
20-
grid.init(7, 2);
21-
grid.init(7, 3);
22-
grid.init(8, 1);
23-
grid.init(8, 2);
16+
grid.turnCellOn(1, 2);
17+
grid.turnCellOn(2, 2);
18+
grid.turnCellOn(3, 2);
19+
grid.turnCellOn(6, 1);
20+
grid.turnCellOn(7, 2);
21+
grid.turnCellOn(7, 3);
22+
grid.turnCellOn(8, 1);
23+
grid.turnCellOn(8, 2);
2424
}
2525

2626
/**
@@ -71,7 +71,7 @@ public Conway(String path, int margin) {
7171
for (int c = 0; c < line.length(); c++) {
7272
char x = line.charAt(c);
7373
if (x == 'O') {
74-
grid.init(r + margin, c + margin);
74+
grid.getCell(r + margin, c + margin).turnOn();
7575
}
7676
}
7777
}
@@ -139,7 +139,7 @@ public void update() {
139139
// update each cell based on neighbor counts
140140
for (int r = 0; r < rows; r++) {
141141
for (int c = 0; c < cols; c++) {
142-
Cell cell = grid.cellAt(r, c);
142+
Cell cell = grid.getCell(r, c);
143143
updateCell(cell, counts[r][c]);
144144
}
145145
}
Collapse file

‎ch15/GridCanvas.java‎

Copy file name to clipboardExpand all lines: ch15/GridCanvas.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public int numCols() {
5151
* @param c column index
5252
* @return the cell
5353
*/
54-
public Cell cellAt(int r, int c) {
54+
public Cell getCell(int r, int c) {
5555
return array[r][c];
5656
}
5757

@@ -61,7 +61,7 @@ public Cell cellAt(int r, int c) {
6161
* @param r row index
6262
* @param c column index
6363
*/
64-
public void init(int r, int c) {
64+
public void turnCellOn(int r, int c) {
6565
array[r][c].turnOn();
6666
}
6767

Collapse file

‎ch15/Langton.java‎

Copy file name to clipboardExpand all lines: ch15/Langton.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public Langton(int rows, int cols) {
2525
*/
2626
public void update() {
2727

28-
Cell cell = grid.cellAt(xpos, ypos);
28+
Cell cell = grid.getCell(xpos, ypos);
2929
if (cell.isOff()) {
3030
// at a white square; turn right and flip color
3131
head = (head + 1) % 4;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.