-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-a1.bal
180 lines (160 loc) · 5.21 KB
/
version-a1.bal
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
import ballerina/io;
const int GRID_SIZE_X = 5;
const int GRID_SIZE_Y = 5;
const int MAXX = GRID_SIZE_X - 1;
const int MAXY = GRID_SIZE_Y - 1;
type Grid boolean[GRID_SIZE_Y][GRID_SIZE_X];
const boolean LIVE = true;
const boolean DEAD = false;
// seed
Grid grid = [
[DEAD, DEAD, DEAD, DEAD, DEAD],
[DEAD, DEAD, LIVE, DEAD, DEAD],
[DEAD, DEAD, LIVE, DEAD, DEAD],
[DEAD, DEAD, LIVE, DEAD, DEAD],
[DEAD, DEAD, DEAD, DEAD, DEAD]
];
final Grid emptyGrid = [
[DEAD, DEAD, DEAD, DEAD, DEAD],
[DEAD, DEAD, DEAD, DEAD, DEAD],
[DEAD, DEAD, DEAD, DEAD, DEAD],
[DEAD, DEAD, DEAD, DEAD, DEAD],
[DEAD, DEAD, DEAD, DEAD, DEAD]
];
type Coord record {|
int x;
int y;
|};
const LIVE_STR = "■";
const DEAD_STR = "·";
public function main() {
int generation = 1;
while true {
print(generation);
int x = 0;
int y = 0;
Grid nextGrid = emptyGrid.clone();
foreach var line in grid {
x = 0;
foreach var _ in line {
final boolean currentCellState = grid[y][x];
final int numberOfLiveNeighbours = liveNeighboursOf({x, y});
nextGrid[y][x] = nextCellState(currentCellState, numberOfLiveNeighbours);
x += 1;
}
y += 1;
}
grid = nextGrid.clone();
generation += 1;
final string input = io:readln("Enter q to quit: ");
if input == "q" {
break;
}
}
}
function allNeighboursOf(Coord cell) returns Coord[] {
Coord[] neighbours = [];
// top grid row ----------------------------------------------------------
if cell.y == 0 {
if cell.x == 0 {
// #1 left-hand-side grid column (left top corner)
neighbours.push(
{y:0, x:1},
{y:1, x:0},{y:1, x:1}
);
} else if cell.x == MAXX {
// #2 right-hand-side grid column (right top corner)
neighbours.push(
{y:0, x:MAXX-1},
{y:1, x:MAXX-1},{y:1, x:MAXX}
);
} else {
// #3 all middle grid columns
neighbours.push(
{y:0, x:cell.x-1}, {y:0, x:cell.x+1},
{y:cell.y+1, x:cell.x-1},{y:cell.y+1, x:cell.x},{y:cell.y+1, x:cell.x+1}
);
}
}
// bottom grid row --------------------------------------------------------
else if cell.y == MAXY {
if cell.x == 0 {
// #4 left-hand-side grid column (left bottom corner)
neighbours.push(
{y:MAXY-1, x:0},{y:MAXY-1, x:1},
{y:MAXY, x:1}
);
} else if cell.x == MAXX {
// #5 right-hand-side grid column (right bottom corner)
neighbours.push(
{y:MAXY-1, x:MAXX-1},{y:MAXY-1, x:MAXX},
{y:MAXY, x:MAXX-1}
);
} else {
// #6 all middle grid columns
neighbours.push(
{y:MAXY-1, x:cell.x-1},{y:MAXY-1, x:cell.x},{y:MAXY-1, x:cell.x+1},
{y:MAXY, x:cell.x-1}, {y:MAXY, x:cell.x+1}
);
}
}
// all middle grid rows ---------------------------------------------------
else {
if cell.x == 0 {
// #7 left-hand-side grid column
neighbours.push(
{y:cell.y-1, x:0},{y:cell.y-1, x:1},
{y:cell.y, x:1},
{y:cell.y+1, x:0},{y:cell.y+1, x:1}
);
} else if cell.x == MAXX {
// #8 right-hand-side grid column
neighbours.push(
{y:cell.y-1, x:MAXX-1},{y:cell.y-1, x:MAXX},
{y:cell.y, x:MAXX-1},
{y:cell.y+1, x:MAXX-1},{y:cell.y+1, x:MAXX}
);
} else {
// #9 all middle grid columns
neighbours.push(
{y:cell.y-1, x:cell.x-1},{y:cell.y-1, x:cell.x},{y:cell.y-1, x:cell.x+1},
{y:cell.y, x:cell.x-1}, {y:cell.y, x:cell.x+1},
{y:cell.y+1, x:cell.x-1},{y:cell.y+1, x:cell.x},{y:cell.y+1, x:cell.x+1}
);
}
}
return neighbours;
}
function liveNeighboursOf(Coord cell) returns int {
Coord[] neighbours = allNeighboursOf(cell);
return neighbours.reduce(function(int total, Coord c) returns int {
return grid[c.y][c.x] ? total + 1 : total;
}, 0);
}
function nextCellState(boolean cellState, int liveNeighbours) returns boolean {
match cellState {
LIVE => {
match liveNeighbours {
2|3 => { return LIVE; }
_ => { return DEAD; }
}
}
DEAD => {
match liveNeighbours {
3 => { return LIVE; }
_ => { return DEAD; }
}
}
}
return DEAD; // dead-code to keep compiler happy :(
}
function print(int generation) {
io:println("===");
io:println(string`generation: ${generation}`);
foreach var line in grid {
foreach var cell in line {
io:print(cell ? LIVE_STR : DEAD_STR);
}
io:println();
}
}