-
Notifications
You must be signed in to change notification settings - Fork 0
/
version-a2.bal
159 lines (138 loc) · 4.56 KB
/
version-a2.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
import ballerina/io;
const int GRID_SIZE_Y = 5;
const int GRID_SIZE_X = 5;
const int MAX_INDEX_Y = GRID_SIZE_Y - 1;
const int MAX_INDEX_X = GRID_SIZE_X - 1;
type Grid boolean[GRID_SIZE_Y][GRID_SIZE_X];
const boolean LIVE = true;
const boolean DEAD = false;
// current generation with seed (for generation 1)
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]
];
type Coord record {|
int x;
int y;
|};
const LIVE_STR = "■";
const DEAD_STR = "·";
public function main() {
int generation = 1;
while true {
print(generation);
Grid nextGrid = [[DEAD]];
foreach int y in 0..<GRID_SIZE_Y {
foreach int x in 0..<GRID_SIZE_X {
final boolean currentCellState = grid[y][x];
final int numberOfLiveNeighbours = numberOfLiveNeighboursOf({x, y});
nextGrid[y][x] = nextCellState(currentCellState, numberOfLiveNeighbours);
}
}
grid = nextGrid.clone();
generation += 1;
final string input = io:readln("Enter q to quit: ");
if input == "q" {
break;
}
}
}
function numberOfLiveNeighboursOf(Coord cell) returns int {
int countOfLiveNeighbours = 0;
// count only alive cells
final var x = function(Coord c) {
if grid[c.y][c.x] == LIVE {
countOfLiveNeighbours += 1;
}
};
// for better readability
final int cy = cell.y;
final int cx = cell.x;
final int MY = MAX_INDEX_Y;
final int MX = MAX_INDEX_X;
//
// check all possible neighbours
//
// top grid row ----------------------------------------------------------
if cy == 0 {
if cx == 0 {
// #1 left-hand-side grid column (left top corner)
x({y:0, x:1});
x({y:1, x:0}); x({y:1, x:1});
} else if cx == MX {
// #2 right-hand-side grid column (right top corner)
x({y:0, x:MX-1});
x({y:1, x:MX-1}); x({y:1, x:MX});
} else {
// #3 all middle grid columns
x({y:0, x:cx-1}); x({y:0, x:cx+1});
x({y:cy+1, x:cx-1}); x({y:cy+1, x:cx}); x({y:cy+1, x:cx+1});
}
}
// bottom grid row --------------------------------------------------------
else if cy == MY {
if cx == 0 {
// #4 left-hand-side grid column (left bottom corner)
x({y:MY-1, x:0}); x({y:MY-1, x:1});
x({y:MY+0, x:1});
} else if cx == MX {
// #5 right-hand-side grid column (right bottom corner)
x({y:MY-1, x:MX-1}); x({y:MY-1, x:MX});
x({y:MY+0, x:MX-1});
} else {
// #6 all middle grid columns
x({y:MY-1, x:cx-1}); x({y:MY-1, x:cx}); x({y:MY-1, x:cx+1});
x({y:MY+0, x:cx-1}); x({y:MY+0, x:cx+1});
}
}
// all middle grid rows ---------------------------------------------------
else {
if cx == 0 {
// #7 left-hand-side grid column
x({y:cy-1, x:0}); x({y:cy-1, x:1});
x({y:cy+0, x:1});
x({y:cy+1, x:0}); x({y:cy+1, x:1});
} else if cx == MX {
// #8 right-hand-side grid column
x({y:cy-1, x:MX-1}); x({y:cy-1, x:MX});
x({y:cy+0, x:MX-1});
x({y:cy+1, x:MX-1}); x({y:cy+1, x:MX});
} else {
// #9 all middle grid columns
x({y:cy-1, x:cx-1}); x({y:cy-1, x:cx}); x({y:cy-1, x:cx+1});
x({y:cy+0, x:cx-1}); x({y:cy+0, x:cx+1});
x({y:cy+1, x:cx-1}); x({y:cy+1, x:cx}); x({y:cy+1, x:cx+1});
}
}
return countOfLiveNeighbours;
}
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();
}
}