-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameOfLife.java
125 lines (108 loc) · 4.39 KB
/
GameOfLife.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
/* Copyright (c) 2020 Claus Jørgensen */
import java.util.*;
import java.io.*;
/**
* Conway's Game of Life
*
* https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
*/
public final class GameOfLife {
private int[][] map;
public GameOfLife(int[][] map) {
this.map = map;
}
private void transition() {
var newMap = new int[this.map.length][this.map[0].length];
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
var neighbors = getNeighbors(x, y);
int numAlive = neighbors.stream().reduce(0, (a, c) -> (c == 1 ? a + 1 : a));
if (numAlive < 2) {
newMap[x][y] = 0; // death by under population
} else if (numAlive > 3) {
newMap[x][y] = 0; // death by over population
} else if (map[x][y] == 0 && numAlive == 3) {
newMap[x][y] = 1; // alive by reproduction
} else {
newMap[x][y] = this.map[x][y]; // continue unchanged
}
}
}
this.map = newMap;
}
private List<Integer> getNeighbors(int x, int y) {
var neighbors = new ArrayList<Integer>();
int mapSizeX = this.map.length;
int mapSizeY = this.map[0].length;
if ((x - 1) >= 0) {
neighbors.add(map[x - 1][y]); // left
}
if ((x + 1) < mapSizeX) {
neighbors.add(map[x + 1][y]); // right
}
if ((y - 1) >= 0) {
neighbors.add(map[x][y - 1]); // top
}
if ((y + 1) < mapSizeY) {
neighbors.add(map[x][y + 1]); // bottom
}
if ((x - 1) >= 0 && (y - 1) >= 0) {
neighbors.add(map[x - 1][y - 1]); // top-left
}
if ((x - 1) >= 0 && (y + 1) < mapSizeY) {
neighbors.add(map[x - 1][y + 1]); // bottom-left
}
if ((x + 1) < mapSizeX && (y + 1) < mapSizeY) {
neighbors.add(map[x + 1][y + 1]); // top-right
}
if ((x + 1) < mapSizeX && (y - 1) >= 0) {
neighbors.add(map[x + 1][y - 1]); // bottom-right
}
return neighbors;
}
private String getString() {
var output = "";
for (int x = 0; x < map.length; x++) {
for (int y = 0; y < map[x].length; y++) {
if (map[x][y] == 1) {
output += "+ ";
} else {
output += " ";
}
}
output += "\n";
}
return output;
}
public static void main(String[] args) throws IOException, InterruptedException {
int[][] pentadecathlon = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
var game = new GameOfLife(pentadecathlon);
while (true) {
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println(game.getString());
game.transition();
Thread.sleep(1000);
}
}
}