-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife.java
More file actions
332 lines (259 loc) · 10.4 KB
/
Copy pathGameOfLife.java
File metadata and controls
332 lines (259 loc) · 10.4 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
package GameOfLife;
public class GameOfLife {
public static void main(String[] args) {
System.out.println("Welcome to the game of life!");
// The size of the matrix
int SizeOfMatt = MyConsole.readInt("Please enter the size of the matrix: ");
while (SizeOfMatt < 0 || SizeOfMatt > 99) { // Check input
System.out.println("Wrong value for the size of the matrix. Try again.");
SizeOfMatt = MyConsole.readInt("Please enter the size of the matrix: ");
// generation population
} // while
// The maximum number of generations
int MaxNumGen = MyConsole.readInt("Please enter the maximum number of generations: ");
while (MaxNumGen < 0) { // Check input
System.out.println("Wrong value for the maximum number of generations. Try again.");
MaxNumGen = MyConsole.readInt("Please enter the maximum number of generations: ");
// generation population
} // while
boolean matt[][] = new boolean[SizeOfMatt][SizeOfMatt]; // New Matrix - The size of the Matrix is 'SizeOfMatt'
// The first generation
int NumOfCreatures = 0; // The number of creatures in this generation
firstGen(matt); // Call to function firstGen
System.out.println('\n' + "The game begins!");
System.out.println("Generation number 1");
printBoard(matt); // Call to function printBoard
System.out.println(); // Descending line
// The number of creatures in first generation
for(int i = 0; i < matt.length; i++){
for(int j = 0; j < matt.length; j++) {
if(matt[i][j])
NumOfCreatures++;
} // loop j
} // loop i
System.out.println("The number of creatures in this generation is: " + NumOfCreatures);
System.out.println("The number of creatures born when moving to this generation is: " + NumOfCreatures);
System.out.println("The number of creatures died when moving to this generation is: 0 ");
System.out.println('\n'); // Descending line
// The next generation
int GenerationNum = 2; // Generation Number
while(!No_living_Creatures(matt) && !Generation_Equals(matt) && NumOfCreatures < MaxNumGen) {
NumOfCreatures = 0;
System.out.println("Generation number " + GenerationNum++);
int Num_Of_Creatures_Born_Died[] = nextGen(matt);
printBoard(matt); // Call to function printBoard
System.out.println(); // Descending line
for(int i = 0; i < matt.length; i++){
for(int j = 0; j < matt.length; j++) {
if(matt[i][j])
NumOfCreatures++;
} // loop j
} // loop i
System.out.println("The number of creatures in this generation is: " + NumOfCreatures);
System.out.println("The number of creatures born when moving to this generation is: " + Num_Of_Creatures_Born_Died[0]);
System.out.println("The number of creatures died when moving to this generation is: " + Num_Of_Creatures_Born_Died[1]);
System.out.println(); // Descending line
} // while
// Game over
if(Generation_Equals(matt) && !No_living_Creatures(matt)) {
System.out.println("Generation number " + GenerationNum++);
printBoard(matt); // Call to function printBoard
System.out.println(); // Descending line
System.out.println("The number of creatures in this generation is: " + NumOfCreatures);
System.out.println("The number of creatures born when moving to this generation is: 0");
System.out.println("The number of creatures died when moving to this generation is: 0");
System.out.println(); // Descending line
System.out.println("Game over: this generation equals to the previous one.");
} // if
if(No_living_Creatures(matt))
System.out.println("Game over: The whole generation is dead.");
if(NumOfCreatures >= MaxNumGen)
System.out.println("Game over: We developed the number of generations we wanted");
} // main
// How many creatures would start
public static int firstGen(boolean[][] matt) {
System.out.println("How many creatures would you like to start with?");
int K = MyConsole.readInt(""); // Number of first generation
int X, Y; // population
// Check input
while (K < 0 || K > matt.length * matt.length) {
System.out.println("Wrong value for the number of first generations. Try again.");
System.out.println("How many creatures would you like to start with?");
K = MyConsole.readInt(""); // Number of first generation
} // while
for (int i = 1; i <= K; i++) {
System.out.println("Creature number " + i);
X = MyConsole.readInt("Enter X value: ");
// Check input
while (X < 1 || X > matt.length) {
System.out.println("Wrong X value for creature number " + i + " Try again.");
System.out.println("Creature number " + i);
X = MyConsole.readInt("Enter X value: ");
} // while
Y = MyConsole.readInt("Enter Y value: ");
// Check input
while (Y < 1 || Y > matt.length) {
System.out.println("Wrong Y value for creature number " + i + " Try again.");
System.out.println("Creature number " + i);
X = MyConsole.readInt("Enter X value: ");
Y = MyConsole.readInt("Enter Y value: ");
} // while
if(!matt[Y-1][X-1])
matt[Y-1][X-1] = true;
else {
System.out.println("This creature has been entered choose a new value.");
i--;
} // else
} // for
return K; // return the number of the first generations
} // firstGen
// The Function Returns the number of neighbors of a certain cell.
public static int numOfNeighbors(boolean[][] matt, int x, int y) {
int numberOfNeighbors = 0; // Initial value
x += 1;
y += 1;
if(x < matt.length)
if (matt[y - 1][x])
numberOfNeighbors++;
if(x > 1)
if (matt[y - 1][x - 2])
numberOfNeighbors++;
if(x < matt.length && y < matt.length)
if (matt[y][x])
numberOfNeighbors++;
if(y < matt.length)
if (matt[y][x - 1])
numberOfNeighbors++;
if(x > 1 && y < matt.length)
if (matt[y][x - 2])
numberOfNeighbors++;
if(y > 1 && x < matt.length)
if (matt[y - 2][x])
numberOfNeighbors++;
if(y > 1)
if (matt[y - 2][x - 1])
numberOfNeighbors++;
if(y > 1 && x > 1)
if (matt[y - 2][x - 2])
numberOfNeighbors++;
return numberOfNeighbors; // return the number Of Neighbors
} // numOfNeighbors
// function to print the Board
public static void printBoard(boolean[][] matt) {
String matrix[][] = new String[matt.length][matt.length]; // new matrix for the '*'
System.out.print(" "); // Print a space
for (int i = 1; i <= matrix.length; i++) // Prints the numbers from 1 to matt.length
if(i < 10)
System.out.print(" " + i + " ");
else
System.out.print(" " + i);
System.out.println(); // Descending line
System.out.print(" "); // Print a space
// print "---" depending on the matrix
for (int i = 1; i < matrix.length; i++)
System.out.print("---");
System.out.print("--");
System.out.println(); // Descending line
// print the Board
for(int i = 0; i < matrix.length; i++){
if(i < 9)
System.out.print(" " + (i+1) + "|");
else
System.out.print((i+1) + "|");
// if there is a living creature print "*", else print space
for(int j = 0; j < matrix.length; j++) {
if(matt[i][j])
matrix[i][j] = "*";
else
matrix[i][j] = " ";
System.out.print(matrix[i][j]);
if(j == matrix.length - 1)
System.out.print(" |");
else
System.out.print(" "); // Print a space
} // for j
System.out.println();
} // for i
System.out.print(" "); // Print a space
// print "---" depending on the matrix
for (int i = 1; i < matrix.length; i++)
System.out.print("---");
System.out.print("--");
} // printBoard
// The Function Returns How many creatures born or died in next generation
public static int[] nextGen(boolean[][] matt) {
String mattrix[][] = new String[matt.length][matt.length]; // new matrix for the '*'
int arr[] = new int[2]; // new array - size 2
int CountDied = 0; // counter for How many creatures died
int CountBorn = 0; // counter for How many creatures born
// We need to work on another matrix so we need a new matrix
for(int i = 0; i < mattrix.length; i++){
for(int j = 0; j < mattrix.length; j++) {
if(matt[i][j])
mattrix[i][j] = "*";
else
mattrix[i][j] = " ";
} // loop j
} // loop i
// We check how many creatures were born or died in the next generation
for(int i = 0; i < mattrix.length; i++){
for(int j = 0; j < mattrix.length; j++) {
if(mattrix[i][j] == "*") {
if((i == 0) || (i == mattrix.length-1) || (j == mattrix.length-1) || (j == 0)) {
mattrix[i][j] = " ";
CountDied++;
} // if
else if((numOfNeighbors(matt ,j ,i) < 2) || (numOfNeighbors(matt ,j ,i) > 3)) {
mattrix[i][j] = " ";
CountDied++;
} // if
} // if
else {
if((numOfNeighbors(matt ,j ,i) == 3) && !((i == 0) || (i == mattrix.length-1) || (j == mattrix.length-1) || (j == 0))){
mattrix[i][j] = "*";
CountBorn++;
} // if
} // else
} // loop j
} // loop i
// After a change we will update to the original matrix
for(int i = 0; i < mattrix.length; i++){
for(int j = 0; j < mattrix.length; j++) {
if(mattrix[i][j] == "*")
matt[i][j] = true;
else
matt[i][j] = false;
} // loop j
} // loop i
arr[0] = CountBorn; // The number of creatures born to index 0
arr[1] = CountDied; // The number of creatures died to index 1
return arr; // return the array
} // nextGen
// The function returns if the whole generation is dead
public static boolean No_living_Creatures(boolean[][] matt) {
for(int i = 0; i < matt.length; i++){
for(int j = 0; j < matt.length; j++) {
if(matt[i][j])
return false;
} // loop j
} // loop i
return true;
} // No_living_creatures
// The function returns if the generation is equal to the previous one
public static boolean Generation_Equals(boolean[][] matt) {
boolean matrix[][] = new boolean[matt.length][matt.length];
for(int i = 0; i < matt.length; i++){
for(int j = 0; j < matt.length; j++) {
matrix[i][j] = matt[i][j];
} // loop j
} // loop i
nextGen(matrix);
for(int i = 0; i < matt.length; i++){
for(int j = 0; j < matt.length; j++) {
if(matt[i][j] != matrix[i][j])
return false;
} // loop j
} // loop i
return true;
} // Generation_Equals
} // GameOfLife