-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
356 lines (274 loc) · 10.6 KB
/
Game.java
File metadata and controls
356 lines (274 loc) · 10.6 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
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
// =============================================================================
/**
* The <code>Game</code> class. Manage a single <i>Game of Life</i> by loading
* the initial grid of cells and evolving them as requested.
*
* @author Scott F. Kaplan -- sfkaplan@cs.amherst.edu
**/
// =============================================================================
// =============================================================================
// IMPORTS
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.util.InputMismatchException;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
// =============================================================================
// =============================================================================
public class Game{
// =============================================================================
// =========================================================================
// DATA MEMBERS
/**
* The grid itself.
**/
private Grid _grid;
/**
* The current generation number.
**/
private int _generation;
/**
* Number of generations to play
**/
private int maxGenerations;
/**
* Window size of visualization
**/
public int WIDTH=1440;
public int HEIGHT=900;
/**
* The size of one cell in pixels for visualization.
* smaller size corresponds to larger grid
**/
public int boxsize=10;
/**
* Wait time between frames for visualization.
**/
public int wait = 100;
/**
* Number of cells the grid extends by offscreen.
**/
public int offscreenMargins=10;
/**
* Boolean flag for whether visualization is needed.
**/
public boolean showGraphics;
/**
* The <code>UserInterface</code> that controls and displays the game.
**/
private UserInterface _userInterface;
/**
* The <code>VisualInterface</code> that displays the game.
**/
private VisualInterface _visualInterface;
// =========================================================================
// =========================================================================
/**
* The constructor. Read the initial state of a game from a provided
* pathname, creating the <code>Grid</code> and the specified live
* <code>Cell</code>s.
*
* @param initialStatePath The file from which the initial state of the
* universe is taken.
**/
public Game (String initialStatePath,int maxGenerationsArg,boolean showGraphicsArg) {
this.showGraphics = showGraphicsArg;
this.maxGenerations = maxGenerationsArg;
_userInterface = new UserInterface(this);
// Read the initial state, creating a grid of cells as specified.
readInitialState(initialStatePath);
// Create visual interface if needed.
if (showGraphics) {
_visualInterface = new VisualInterface(this);
}
// Start counting at generation 0.
_generation = 0;
}// Game()
// =========================================================================
// =========================================================================
/**
* Read the initial state file, creating a <code>Grid</code> and
* initializing it with <code>Cell</code>s as specified by that file.
*
* @param initialStatePath The file from which the initial state of the
* universe is taken.
**/
private void readInitialState (String initialStatePath) {
// Create the reader for this file.
Scanner reader = null;
try {
reader = new Scanner(new File(initialStatePath));
} catch (FileNotFoundException e) {
Support.abort("ERROR: File not found: " + initialStatePath);
}
int rows = -1;
int columns = -1;
// Dimensions to be used with graphics mode
int graphicsRows = HEIGHT/(boxsize);
int graphicsColumns = WIDTH/(boxsize);
// Read the first line, which contains the dimensions of the grid for nographics mode.
try {
rows = reader.nextInt();
columns = reader.nextInt();
} catch (InputMismatchException e) {
Support.abort("ERROR: Invalid dimensions at line 1");
}
// Create a Grid with these dimensions
// For graphics mode the grid size depends on window size and not on dimensions specified in first line of init file
if(showGraphics) {
_grid = new Grid(graphicsRows+offscreenMargins, graphicsColumns+offscreenMargins);
}
else {
_grid = new Grid(rows, columns);
}
// Read coordinates for initially live cells until the end-of-file is
// reached.
int lineNumber = 2;
while (true) {
int row = -1;
int col = -1;
try {
row = reader.nextInt();
col = reader.nextInt();
} catch (InputMismatchException e) {
Support.abort("ERROR: From initial state file, " +
"could not read coordinates at line " +
lineNumber);
} catch (NoSuchElementException e) {
break;
}
System.out.printf("DEBUG: Setting initially live cell at " + row + ", " + col + "\n");
// Set the cells to be alive at the center of the screen for graphics mode
if (showGraphics) {
_grid.getCell((graphicsRows + offscreenMargins - rows) / 2 + row, (graphicsColumns + offscreenMargins - columns) / 2 + col).makeAlive();
}
// Set the cell to be alive at coordinates specified by init file for nographics mode
else{
_grid.getCell(row,col).makeAlive();
}
lineNumber += 1;
}
}// readInitialState ()
// =========================================================================
// =========================================================================
/**
* Evolve a game of Life through its generations, emitting the state of the
* game at each generation. Evolution will continue until the universe
* becomes static or until the given maximum number of generations is
* reached.
*
* @param generations The number of generations to evolve.
**/
public void play () {
while(_generation < maxGenerations)
{
evolve();
//System.out.println("evolved");
// Wait before showing the next frame of visualization.
try{
Thread.sleep(wait);
}
catch(InterruptedException e){}
// If in graphics mode, draw the current stage.
if(showGraphics)_visualInterface.repaint();
// Display the current stage.
_userInterface.display();
// Advance to the next stage.
advance();
//System.out.println("advanced");
// Increment the generation counter
_generation+=1;
} // evolution loop
} // play ()
// =========================================================================
// =========================================================================
/**
* Calculate the state of the game's universe from the current generation to
* the next.
**/
public void evolve () {
//WRITE ME
for(int iteratingRow = 0; iteratingRow < getRows(); iteratingRow++) {
for (int iteratingCol = 0; iteratingCol < getColumns(); iteratingCol++) {
this.getCell(iteratingRow, iteratingCol).evolve();
}
}
} // evolve ()
// =========================================================================
// =========================================================================
/**
* Advance the state of the game's universe to the state generated in evolve()
**/
public void advance(){
//WRITE ME
for(int iteratingRow = 0; iteratingRow < getRows(); iteratingRow++) {
for (int iteratingCol = 0; iteratingCol < getColumns(); iteratingCol++) {
this.getCell(iteratingRow, iteratingCol).advance();
}
}
} // advance ()
// =========================================================================
// =========================================================================
/**
* Provide the current generation number.
*
* @return The current generation number for this universe.
**/
public int getGeneration () {
return _generation;
}
// =========================================================================
// =========================================================================
/**
* Provide the number of live cells.
*
* @return The number of live cells in the current universe.
**/
public int getPopulation () {
//WRITE ME
int population = 0;
for(int iteratingRow = 0; iteratingRow < getRows(); iteratingRow++) {
for (int iteratingCol = 0; iteratingCol < getColumns(); iteratingCol++) {
if (getCell(iteratingRow, iteratingCol).isAlive() == true) {
population++;
}
}
}
return population;
} // getPopulation()
// =========================================================================
// =========================================================================
/**
* Provide the number of rows in the universe.
*
* @return The number of rows in the universe.
**/
public int getRows () {
return _grid.getRows();
} // getRows()
// =========================================================================
// =========================================================================
/**
* Provide the number of columns in the universe.
*
* @return The number of columns in the universe.
**/
public int getColumns () {
return _grid.getColumns();
} // getColumns()
// =========================================================================
// =========================================================================
/**
* Provide direct access to a <code>Cell</code> from this universe.
*
* @param row The row coordinate of the <code>Cell</code>.
* @param column The column coordinate of the <code>Cell</code>.
**/
public Cell getCell (int row, int column) {
return _grid.getCell(row, column);
} // getCell()
// =========================================================================
// =============================================================================
} // class Game
// =============================================================================