-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
51 lines (44 loc) · 1.39 KB
/
Controller.java
File metadata and controls
51 lines (44 loc) · 1.39 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
package com.company;
/*
* This class acts as an interface between the GUI and the World object that will be displayed
*/
public class Controller {
private final GameOfLife gol;
private World world;
public Controller() {
this.gol = new GameOfLife();
this.world = newWorld();
}
/*
* This method keeps the program running and handles things such as pausing and resetting the world
*/
public void run() {
while (true) {
gol.displayWorld(world); // Send the World object to the GUI to display
try { // Delay the thread depending on the speed setting of the GUI
Thread.sleep(2000 / gol.getSpeed());
} catch (InterruptedException e) {
e.printStackTrace();
}
if (gol.isReset()) {
world = newWorld();
gol.setReset(false);
gol.displayWorld(world);
}
while (gol.isPaused()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
world.nextGeneration();
}
}
private World newWorld() {
int worldSize = gol.getWorldSize();
World world = new World(worldSize);
gol.setWorldSize(worldSize);
return world;
}
}