-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameThread.java
More file actions
166 lines (146 loc) · 3.63 KB
/
GameThread.java
File metadata and controls
166 lines (146 loc) · 3.63 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.Random;
import java.util.Scanner;
import java.util.InputMismatchException;
class GridThread extends Thread {
private Thread t;
private String name;
GridThread(String name) {
this.name = name;
}
public void run() {
Random rand = new Random();
Scanner s = new Scanner(System.in);
int width = 30;
int height = 30;
int maxClock = 0;
System.out.print("Please enter the simulation width: ");
boolean received = false;
while (!received) {
try {
width = s.nextInt();
if (width <= 0) {
System.out.println("Please enter a positive integer.");
}
else {
received = true;
}
} catch (InputMismatchException e) {
System.out.println("Please enter a positive integer.");
}
}
System.out.print("Please enter the simulation height: ");
received = false;
while (!received) {
try {
height = s.nextInt();
if (height <= 0) {
System.out.println("Please enter a positive integer.");
}
else {
received = true;
}
} catch (InputMismatchException e) {
System.out.println("Please enter a positive integer.");
}
}
System.out.print("Please enter the max number of clock cycles, or 0 for infinite: ");
received = false;
while (!received) {
try {
maxClock = s.nextInt();
if (maxClock < 0) {
System.out.println("Please enter zero (for infinite) or higher.");
}
else {
received = true;
}
} catch (InputMismatchException e) {
System.out.println("Please enter an integer >= 0.");
}
}
int clock = 1;
World w = World.getInstance(width, height);
DisplayGUI d = new DisplayGUI(w);
while (true) {
//print the world
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.print(w);
System.out.print("\nClock cycle: "+clock+"\n");
d.setClockCycle(clock);
d.setLabelsText(w);
if (rand.nextInt(10) < 9) {
//carnivores move 90% of the time
w.moveCarnivores();
}
if (rand.nextInt(2) == 0) {
//herbivores move 50% of the time
w.moveHerbivores();
}
if (clock%3 == 0) {
//age every 3 cycles
w.age();
}
if (clock%4 == 0) {
//add plants every 4 cycles
w.addPlants();
}
if (clock == maxClock) {
break;
}
clock++;
//wait 400ms
try {
Thread.sleep(400);
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
public void start() {
if(t == null) {
t = new Thread(this, name);
System.out.println("GridThread Started");
t.start();
}
}
}
class InputThread extends Thread{
private Thread t;
private String name;
InputThread(String name) {
this.name = name;
}
public void run() {
/* REPLACE THIS IMPLEMENTATION WITH KEYBOARD INPUTS */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
Scanner scanner = new Scanner(System.in);
String input = "";
System.out.println("InputThread Started");
while(!(input.equals("exit"))) {
System.out.println("InputThread: ");
input = scanner.nextLine();
}
System.out.println("InputThread Finished");
}
public void start() {
if(t == null){
t = new Thread(this, name);
System.out.println("InputThread Started");
t.start();
}
}
}
public class GameThread {
public static void main(String[] args) {
GridThread game = new GridThread("GridThread");
InputThread input = new InputThread("InputThread");
game.start();
input.start();
}
}