-
Notifications
You must be signed in to change notification settings - Fork 0
/
LifeGame.java
144 lines (124 loc) · 3.25 KB
/
LifeGame.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
// Life Game Implement
import java.io.*;
import java.util.Scanner;
class LifeGame {
int maxRow = 25, maxCol = 25;
int dead = 0, alive = 1;
int[][] map = new int[maxRow][maxCol];
int[][] newmap = new int[maxRow][maxCol];
int generation = 0;
Scanner keyboard = new Scanner(System.in);
LifeGame() {
generation = 0;
}
public void init() {
int row = 0, col = 0;
// initial map status = cells all dead in the beginning.
for(row=0;row<maxRow;row++) {
for (col=0;col<maxCol;col++) {
map[row][col] = dead;
}
}
System.out.print("Game of life Program \n");
System.out.print("Enter (x,y) where (x,y) is a living cell\n");
System.out.print(" 0 <= x <= " + (maxRow - 1) + " , 0 <= y <= "+ (maxCol - 1) +"\n");
System.out.print("Terminate with (x,y) = (-1, -1) \n");
do {
System.out.print("x-->");
System.out.flush();
row = keyboard.nextInt();
System.out.print("y-->");
col = keyboard.nextInt();
if (0 <= row && row <maxRow && 0 <= col && col < maxCol) {
map[row][col] = alive;
}
else {
System.out.print("(x, y) exceeds map range!\n");
}
} while (row != -1 || col != -1);
}
public int neighbors(int row, int col) {
int count = 0, c = 0, r = 0;
// calculate each cell's neighbor count
for(r=row-1;r<=row+1;r++) {
for(c=col-1;c<=col+1;c++) {
if(r<0 || r>=maxRow || c<0 || c>= maxCol)
continue;
if(map[r][c] == alive)
count ++;
}
}
// adjust neighbor count
if(map[row][col] == alive) {
count --;
}
return count;
}
public void output_map() {
int row = 0, col = 0;
System.out.println(" Game of life cell status");
System.out.println(" -----Generation"+ (++generation) + "-----");
for(row=0;row<maxRow;row++) {
System.out.println();
System.out.print(" ");
for(col=0;col<maxCol;col++) {
if(map[row][col] == alive)
System.out.print("@");
else
System.out.print("-");
}
}
}
public void copy_map() {
int row = 0, col = 0;
for(row=0;row<maxRow;row++) {
for (col=0;col<maxCol;col++) {
map[row][col] = newmap[row][col];
}
}
}
public void access() {
int row = 0, col = 0;
String ans = "";
do {
for(row=0;row<maxRow;row++) {
for(col=0;col<maxCol;col++) {
switch(neighbors(row, col)) {
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
newmap[row][col] = dead;
break;
case 2:
newmap[row][col] = map[row][col];
break;
case 3:
newmap[row][col] = alive;
break;
}
}
}
copy_map();
do {
System.out.print("\nContinue next Generation ?(y/n): ");
ans = keyboard.next();
if(ans.equals("y") || ans.equals("n")) {
break;
}
} while(true);
if(ans.equals("y")) {
output_map();
}
} while(ans.equals("y"));
}
public static void main(String[] args) {
LifeGame obj = new LifeGame();
obj.init();
obj.output_map();
obj.access();
}
}