-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.java
377 lines (359 loc) · 9.91 KB
/
GameOfLife.java
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* The GameOfLife class handles all properties
* of Conway's Game of Life simulation and provides
* methods to output information to create a GUI.
* @author chinm3
*
*/
public class GameOfLife {
private int[][] currentGrid;
private int[][] nextGrid;
private int rows;
private int cols;
private String outputPattern;
private int currentTick = 0;
private int threadCount = 1;
private List<int[][]> grids;
/**
* Constructor for an instance of GameOfLife
* @param initState 2D array containing the initial seed
* @param ticks Number of iterations in simulation
* @param output Output pattern of each tick
*/
public GameOfLife() {
grids = new ArrayList<int[][]>();
currentGrid = new int[10][10];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
currentGrid[i][j] = 0;
}
}
rows = 10;
cols = 10;
outputPattern = "defaultPattern";
}
/**Return current tick of game board.
* @return int Current tick
*/
public int getTick() {
return currentTick;
}
/**
* Set the output pattern to string s
* @param s output pattern to be set
*/
public void setOutput(String s) {
outputPattern = s;
}
public void setThreads(int threads) {
threadCount = threads;
}
/**
* Reverts board to previous tick. This copies the previous tick board to the current game board.
*/
public void previousTick() {
if(currentTick == 0) {
return;
}
int[][] prev = grids.get(grids.size()-1);
for(int i = 0; i < rows; i++) {
currentGrid[i] = prev[i].clone();
}
grids.remove(grids.size()-1);
//System.out.println(grids);
currentTick--;
}
public void changeGridSize(int rows, int cols) {
int[][] generateGrid = new int[rows][cols];
Random rand = new Random();
for(int i= 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
int n = rand.nextInt(100);
if(n > 49) {
generateGrid[i][j] = 1;
}else {
generateGrid[i][j] = 0;
}
}
}
currentGrid = generateGrid;
this.rows = rows;
this.cols = cols;
currentTick = 0;
grids.clear();
grids.add(currentGrid);
}
/**
* Advances board a tick. Next grid is calculated and then stored in currentGrid.
*/
public void advanceTick() {
// if(currentTick >= 0) {
// int[][] currCopy = new int[rows][cols];
// for(int i = 0; i < rows; i++) {
// currCopy[i] = currentGrid[i].clone();
// }
// grids.add(currCopy);
// }
nextGrid = new int[rows][cols];
int x = (int) Math.sqrt(threadCount);
int threadWidth = cols;
int threadHeight = rows;
if(Math.pow(x,2) == threadCount) {
threadWidth = cols/x;
threadHeight = rows/x;
}else {
threadWidth = cols/2;
int temp = threadCount/2;
threadHeight = rows/temp;
}
//write for loop to properly section each part
ArrayList<BoardThread> threads = new ArrayList<BoardThread>();
for(int i = 0; i < rows; i+=threadHeight) {
for(int j = 0; j < cols; j+= threadWidth) {
System.out.println("Section: (" + i + " , " + j + ") TO ("+(i+threadHeight)+" , "+(j+threadWidth)+ ")");
BoardThread t = new BoardThread(this, this.currentGrid, this.nextGrid,i,j,i+threadHeight, j+threadWidth);
t.start();
threads.add(t);
}
}
for(BoardThread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for(int i = 0; i < nextGrid.length; i++) {
currentGrid[i] = nextGrid[i].clone();
}
currentTick++;
}
/**
* @param row Row index
* @param col Column index
* @return int State of cell at row,col in previous tick.
*/
// public int getPreviousState(int row, int col) {
// return(grids.get(currentTick)[row][col]);
// }
//
/**
* Handles loading a file's path.
* @param inputFile path to file to be loaded
*/
public void loadConfig(String inputFile) {
BufferedReader reader;
int[][] initialState = null;
int i = 0;
int j = 0;
try {
reader = new BufferedReader(new FileReader(inputFile));
String line = reader.readLine();
String[] dimensions = line.split(", ");
int rows = Integer.parseInt(dimensions[0]);
int cols = Integer.parseInt(dimensions[1]);
if(rows < 3 || cols < 3) {
printError("Invalid dimensions");
}
initialState = new int[rows][cols];
line = reader.readLine();
while(line != null) {
String[] currRow = line.split(",");
for(String x : currRow) {
if(x.isEmpty()) {
printError("Invalid grid input");
}
initialState[i][j] = Integer.parseInt(x);
if(initialState[i][j] != 0 && initialState[i][j] != 1) {
printError("Invalid grid input");
}
j++;
}
i++;
if(j != cols) {
printError("Invalid grid input");
}
j=0;
line = reader.readLine();
}
if(i != rows) {
printError("Invalid grid input");
}
currentGrid = initialState;
this.rows = initialState.length;
this.cols = initialState[0].length;
currentTick = 0;
grids.clear();
grids.add(currentGrid);
}catch(IOException e) {
System.out.println("Invalid filename");
}
}
/**
* Grabs value at row,col in current grid.
* @param row row of value
* @param col column of value
* @return int value at row,col in the current grid
*/
public int getValueAt(int row, int col) {
return currentGrid[row][col];
}
/**
* This method outputs the next tick of the simulation to a file named by the output pattern with the tick
* number appended to it.
* @param tick Current tick to append to output pattern file name.
*/
public void outputTickFile(int tick) {
String fileName = outputPattern + Integer.toString(tick)+ ".txt";
try {
System.out.println(grids+ " :: " + tick);
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
for(int[] row : currentGrid) {
for(int col : row) {
writer.print(col);
}
writer.println();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* This method runs the simulation and outputs each tick to a file named by the designated output pattern.
*/
public void runSimulation(int start, int end) {
currentGrid = grids.get(0);
nextGrid = new int[rows][cols];
while(this.currentTick > start) {
previousTick();
}
while(currentTick <= end) {
outputTickFile(currentTick);
advanceTick();
}
}
/**
* Get number of rows
* @return Rows in current board
*/
public int getRows() {
return rows;
}
/**
* Get number of columns
* @return Columns in current board
*/
public int getCols() {
return cols;
}
/**
* Get living cells on board
* @return Living cell count in current board
*/
public int getLive() {
int sum = 0;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(currentGrid[i][j] == 1) sum++;
}
}
return sum;
}
/**
* This method counts the number of live neighbors around an index, supporting wrapping around edges
* so that at each index, there will always be 8 neighbors.
* @param row Row index of grid
* @param col Column index of grid
* @return int Number of live neighbors around grid[row][col]
*/
public int getLiveNeighborCount(int row, int col) {
int liveNeighbors = 0;
int leftBound = col-1;
int rightBound = col+1;
int upperBound = row-1;
int lowerBound = row+1;
if(leftBound < 0) {
leftBound += cols;
}
if(rightBound > cols-1) {
rightBound -= cols;
}
if(upperBound < 0) {
upperBound += rows;
}
if(lowerBound > rows-1) {
lowerBound -= rows;
}
if(currentGrid[row][leftBound] == 1) liveNeighbors++;
if(currentGrid[row][rightBound] == 1) liveNeighbors++;
if(currentGrid[upperBound][col] == 1) liveNeighbors++;
if(currentGrid[lowerBound][col] == 1) liveNeighbors++;
if(currentGrid[upperBound][leftBound] == 1) liveNeighbors++;
if(currentGrid[upperBound][rightBound] == 1) liveNeighbors++;
if(currentGrid[lowerBound][leftBound] == 1) liveNeighbors++;
if(currentGrid[lowerBound][rightBound] == 1) liveNeighbors++;
return liveNeighbors;
}
/**
* This method takes an index in the grid and returns the next state based on how many live neighbors are
* surrounding the index.
* @param row Row index in grid
* @param col Column index in grid
* @return int The next state of the current grid index at [i][j], following Conway's rules.
*/
public int getNextState(int row, int col) {
int neighbors = getLiveNeighborCount(row, col);
if(currentGrid[row][col] == 1 && neighbors < 2) return 0;
if(currentGrid[row][col] == 1 && neighbors < 4) return 1;
if(currentGrid[row][col] == 1 && neighbors > 3) return 0;
if(currentGrid[row][col] == 0 && neighbors == 3) return 1;
return 0;
}
/**
* This method prints the current state of the board to the terminal.
*/
public void printBoard() {
for(int[] row : currentGrid) {
for(int col : row) {
System.out.print(col);
}
System.out.println();
}
}
/**
* Saves a cfg.txt file on exit with load path and output pattern.
*/
public void saveOnExit() {
String fileName = "cfg.txt";
try {
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
int[][] base = grids.get(0);
writer.println(rows + ", "+cols);
for(int i=0; i < base.length; i++) {
for(int j = 0; j < base[i].length;j++) {
writer.print(base[i][j]);
if(j < base[i].length) {
writer.print(",");
}
}
writer.println();
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void printError(String s) {
System.out.println("ERROR occurred: " + s);
System.exit(1);
}
}