-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife.java
More file actions
305 lines (271 loc) · 6.48 KB
/
GameOfLife.java
File metadata and controls
305 lines (271 loc) · 6.48 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import java.io.File;
import java.util.Random;
public class GameOfLife{
/*
* Private Grid variables.
*/
private char[][] currentGrid;
private char[][] bufferGrid;
/*
* Private variables for number of rows and nuber of columns.
*/
private int numOfRows, numOfCols;
/**
*Contructs instance of object GameOfLife using a seed file.
*
*@param seedFile that that is linked to in the command line.
*/
public GameOfLife(File seedFile){
//TODO
}
/**
* Called from the seed file contructor to read from the file
* and assign initial living cells.
*
* @param file this is the handle to the file that will be read
*/
public void initializeGridFromSeed(File file){
//TODO
}
/**
* Contructs an instance of the class GameOfLife using the
* parameters rows and cols.
*
* @param rows number of rows in the grid
* @param cols number of columns in the grid
*/
public GameOfLife(int rows, int cols){
numOfRows = rows;
numOfCols = cols;
initializeGridsRandomly();
}
/**
* Initializes grid by using the '#' chracter to represent a
* living cell.
*/
public void initializeGridsRandomly(){
Random rand = new Random();
currentGrid = new char[numOfRows][numOfCols];
bufferGrid = new char[numOfRows][numOfCols];
for(int r = 0; r < numOfRows; r++){
for(int c = 0; c < numOfCols; c++){
if(rand.nextInt(10)+1 > 9){
currentGrid[r][c] = '#';
bufferGrid[r][c] = '#';
}else{
currentGrid[r][c] = ' ';
bufferGrid[r][c] = ' ';
}
}//column loop
}//row loop
}
/**
* This method goes about updating the current state of the grid
* variables based on the state of the past one.
*
* It does this by maintaining two arrays. The buffer array is
* modified based on the state of the current array. The current
* array is then reasigned to a copy of the buffered array.
*
*/
public void update(){
for(int r = 0; r < numOfRows; r++){
for(int c = 0; c < numOfCols; c++){
//
if((checkLN(r,c) < 2 && currentGrid[r][c] == '#') || (checkLN(r,c) > 3 && currentGrid[r][c] == '#')){
bufferGrid[r][c] = ' ';
}
else if (currentGrid[r][c] == ' ' && checkLN(r,c) == 3){
bufferGrid[r][c] = '#';
}
}//column loop
System.out.println();
}//row loop
currentGrid = bufferGrid.clone();
}
/**
* This Method is used to identify how many neighbors ajacent
* to a specific cell are alive or dead.
*
* @param row index of the row that needs checking
* @param col index of the column that needs checking
* @return the number of neighbors around the identified cell that are alive
*/
public int checkLN(int row, int col){
int numOfNeighbors = 0;
//Checks everything besides outer boarder
if((row > 0 && row < numOfRows-1) && ( col > 0 && col < numOfCols-1)){
if (currentGrid[row-1][col-1] == '#'){
++numOfNeighbors;
}
if (currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
if (currentGrid[row-1][col+1] == '#'){
++numOfNeighbors;
}
if (currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
if (currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
if (currentGrid[row+1][col-1] == '#'){
++numOfNeighbors;
}
if (currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
if (currentGrid[row+1][col+1] == '#'){
++numOfNeighbors;
}
}
//Upper-Left Corner
else if ( row == 0 && col == 0){
if(currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
if(currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
if(currentGrid[row+1][col+1] == '#'){
++numOfNeighbors;
}
}
//Upper-Right Corner
else if ( row == 0 && col == numOfCols -1){
if(currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
if(currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
if(currentGrid[row+1][col-1] == '#'){
++numOfNeighbors;
}
}
//Lower-Right Corner
else if ( row == numOfRows-1 && col == numOfCols -1){
if(currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
if(currentGrid[row-1][col-1] == '#'){
++numOfNeighbors;
}
if(currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
}
//Lower-Left Corner
else if ( row == numOfRows -1 && col == 0){
if(currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
if(currentGrid[row][col] == '#'){
++numOfNeighbors;
}
if(currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
}
//Check upper boundry
else if (row == 0 && (col > 0 && col < numOfCols -1)){
//Left
if(currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
//Below
if(currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
//Right
if(currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
}
//Check right boundry
else if (col == numOfCols -1 && (row > 0 && row < numOfRows -1)){
//Left
if(currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
//Below
if(currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
//Above
if(currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
}
//Check lower boundry
else if (row == numOfCols-1 && (col > 0 && col < numOfCols -1)){
//Left
if(currentGrid[row][col-1] == '#'){
++numOfNeighbors;
}
//Above
if(currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
//Right
if(currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
}
//Check left boundry
else if (row == 0 && (col > 0 && col < numOfCols -1)){
//Above
if(currentGrid[row-1][col] == '#'){
++numOfNeighbors;
}
//Below
if(currentGrid[row+1][col] == '#'){
++numOfNeighbors;
}
//Right
if(currentGrid[row][col+1] == '#'){
++numOfNeighbors;
}
}
return numOfNeighbors;
}
/**
* Prints state of current grid
*/
public void printGrid(){
System.out.println();
for(int r = 0; r < numOfRows; r++){
for(int c = 0; c < numOfCols; c++){
System.out.print(currentGrid[r][c]);
}//column loop
System.out.println();
}//row loop
System.out.println();
}
/**
* Main loop where program continues to loop until user kills
* Should probably change
*/
public void run(){
while(true){
printGrid();
try{
Thread.sleep(1000);
}catch (InterruptedException e){
Thread.currentThread().interrupt();
}
update();
}
}
/**
* Entry point for your application
*
* @param args string arguments from the console
*/
public static void main (String[] args){
GameOfLife gol = new GameOfLife(100,100);
//run Game of Life Object
gol.run();
}
}