forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-of-life.py
37 lines (34 loc) · 918 Bytes
/
game-of-life.py
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
# Not finished
func(board)->board
if cell was alive:
still alive if it has 2 or 3 living neighbors
otherwise dead
if cell was dead:
alive if it has 3 living neighbors
otherwise dead
public static void main(String args[]) {
int board[][] = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int aliveNeighbors = 0;
if (insideBoard(i, j)) {
for (int k = j-1; k < j++) {
if (board[i-1][k]) aliveNeighbors++;
if (board[i+1][k]) aliveNeighbors++;
}
if (board[i][j-1]) aliveNeighbors++;
if (board[i][j+1]) aliveNeighbors++;
if (board[i][j] == 1) {
if (aliveNeigbhbors == 2 || aliveNeigbhbors == 3)
board[i][j] = 1;
else
board[i][j] = 0;
} else {
}
else board[i][j] = 0;
} else {
if (i == 0 || i == n-1)
}
}
}
}