-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconway_game.java
More file actions
166 lines (142 loc) · 4.13 KB
/
Copy pathconway_game.java
File metadata and controls
166 lines (142 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.*;
class Solution {
//use of 'String' below seems odd - but this is the standard line in the caller class - yes?
public static void main(String[] args) {
// System.out.println(Arrays.asList(args));
// this is a constructor
World w = new World(30, 20);
// calls the seed method to create/populate grid
w.seedFirstGeneration();
// prints current grid and generates new grid
for (int i=0; i<10; i++) {
w.print();
w.computeNextGeneration();
// }
}
}
class World {
// grid = [][]
private boolean[][] grid;
private boolean[][] nextGrid;
//so in Java you must declare this before you use as parameters on line 26?
private final int rows;
private final int cols;
//let's talk about the next 4 lines
public World(int rows, int cols) {
grid = new boolean[rows][cols];
//does this look like [false, false, ...30 times][false, false, ...20 times]
nextGrid = new boolean[rows][cols];
//adding 'this' makes these variable accessible to other functions?
this.rows = rows; //==30?
this.cols = cols;
}
public void seedFirstGeneration() {
// in Java you have to declare that you are using a built-in class?
Random rand = new Random();
//loops through 30 rows, and 20 cols (see line 7, 26)
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
// pick a number between 0..9
int i = rand.nextInt(10);
if (i == 0) {
grid[row][col] = true;
}
}
}
}
public void print() {
for (boolean[] row : grid) {
//we don't need to declare 'cell' first - we can just create/name on-the-fly?
for (boolean cell : row) {
if (cell) {
System.out.print("#");
} else {
System.out.print(".");
}
}
System.out.println();
}
// Print some blank spaces after the grid.
System.out.println();
System.out.println();
}
// private int rows() {
// //grid is accessible without using 'this' after line 27?
// return grid.length;
// }
public int getLiveNeighborCount(int row, int col) {
int liveNeighborCount = 0;
// let's talk about delta
for (int dx=-1; dx<=1; dx++) {
for (int dy=-1; dy<=1; dy++) {
if (dx == 0 && dy == 0) {
continue;
}
if (aliveAt(row + dy, col + dx)) {
liveNeighborCount++;
}
}
}
return liveNeighborCount;
}
private boolean aliveAt(int row, int col) {
// Since the caller added a delta to row and col, they may
// go out of bounds. This wraps to keep in the grid.
if (row < 0) {
row = rows-1;
} else if (row == rows) {
row = 0;
}
if (col < 0) {
col = cols - 1;
} else if (col == cols) {
col = 0;
}
return grid[row][col];
}
public void computeNextGeneration() {
/*
- Any live cell with fewer than two live neighbours
dies, as if caused by under-population.
- Any live cell with two or three live neighbours
lives on to the next generation.
- Any live cell with more than three live neighbours
dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours
becomes a live cell, as if by reproduction.
*/
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
boolean alive = grid[row][col];
int liveNeighbors = getLiveNeighborCount(row, col);
boolean nextState = false;
if (alive) {
if (liveNeighbors < 2) {
nextState = false;
} else if (liveNeighbors < 4) {
// 2-3 neighbors
nextState = true;
} else {
// more than 3
nextState = false;
}
} else {
if (liveNeighbors == 3) {
// reproduction
nextState = true;
}
}
nextGrid[row][col] = nextState;
}
}
// Swap
boolean[][] tmp = grid;
grid = nextGrid;
nextGrid = tmp;
}
}
class Maze
class Cell
(objects instead of primitives)
word search
secret santa