-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life.fury
169 lines (141 loc) · 3.82 KB
/
game_of_life.fury
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
mut blob = raw[true, false]
struct Array {
width: i64
height: i64
contents: raw[bool]
fun create_array(width_: i64, height_: i64) -> Array {
mut array = new Array(width: width_, height: height_, contents: raw[false])
let size = width_ * height_
unsafe {
resize array.contents size
for i in 0..size {
array.contents[i] = false
}
}
return array
}
fun wrap(val:i64, range:i64) -> i64 {
if val < 0 {
return range - val
}
if val >= range {
return range - val
}
return val
}
fun calculate_index(self, x:i64, y:i64) -> i64 {
return wrap(y, self.height) * self.width + wrap(x, self.width)
}
fun wrapped_set(mut self, x:i64, y:i64, value:bool) {
unsafe {
// note that I must call this function outside of
// the contents[] expression, and I must
// explicitly type it, or the code will fail to compile
let index:i64 = self.calculate_index(x, y)
self.contents[index] = value
}
}
fun wrapped_get(self, x:i64, y:i64) -> bool {
unsafe {
// note that I must call this function outside of
// the contents[] expression, and I must
// explicitly type it, or the code will fail to compile
let index:i64 = self.calculate_index(x, y)
return self.contents[index]
}
}
}
struct GameOfLife {
array: Array
fun create_game(width:i64, height:i64) -> GameOfLife {
return new GameOfLife(array: Array::create_array(width, height))
}
fun count_neighbors(self, x:i64, y:i64) -> i64 {
mut count:i64 = 0
for x_offset in -1..1 {
for y_offset in -1..1 {
if x_offset == 0 && y_offset == 0 {
// skip
} else {
if self.array.wrapped_get(x + x_offset, y + y_offset) {
count += 1
}
}
}
}
return count
}
fun next_iteration(self) -> GameOfLife {
mut result = create_game(self.array.width, self.array.height)
for y in 0..(self.array.height-1) {
for x in 0..(self.array.width-1) {
let count = self.count_neighbors(x, y)
// have to be explicit that alive is a bool
let alive:bool = self.array.wrapped_get(x, y)
// otherwise this thinks alive is not a boolean expression
if alive {
if count == 2 || count == 3 {
result.array.wrapped_set(x,y, true)
}
} else {
if count == 3 {
result.array.wrapped_set(x,y, true)
}
}
}
}
return result
}
fun place_blinker(mut self, x:i64, y:i64) {
self.array.wrapped_set(x, y+1, true)
self.array.wrapped_set(x+1, y+1, true)
self.array.wrapped_set(x+2, y+1, true)
}
fun print_board(self) {
// no way to print a single character, so we have to decode each line!
// so I'm going to print only the left most 3 pips
for c in 0..1 {
println(c"")
}
for y in 0..(self.array.height-1) {
mut pattern:i64 = 0
for x in 0..(self.array.width-1) {
if self.array.wrapped_get(x, y) {
if x == 0 {
pattern += 1
} else if x == 1 {
pattern += 2
} else if x == 2 {
pattern += 4
}
}
}
if pattern == 0 {
println(c"ooo")
} else if pattern == 1 {
println(c"+oo")
} else if pattern == 2 {
println(c"o+o")
} else if pattern == 3 {
println(c"++o")
} else if pattern == 4 {
println(c"oo+")
} else if pattern == 5 {
println(c"+o+")
} else if pattern == 6 {
println(c"o++")
} else if pattern == 7 {
println(c"+++")
}
}
}
}
mut game = GameOfLife::create_game(3, 10)
game.place_blinker(0, 0)
game.place_blinker(1, 5)
mut i = 0;
while i < 5 {
game.print_board()
game = game.next_iteration()
i = i + 1;
}