-
Notifications
You must be signed in to change notification settings - Fork 0
/
play1.rb
47 lines (42 loc) · 966 Bytes
/
play1.rb
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
size = 30
board = Array.new(size*size) { rand(2) }
loop do
# print board
board.each.with_index do |cell, index|
print "\n" if index % size == 0
print cell
end
# calculate next generation
board = board.map.with_index do |cell, index|
sum = 0
n=index-size
s=index+size
w=index-1
e=index+1
# NOTE: the || is used to catch going outside the array boundary
# TODO: I think there's a bug here where west and east boundaries are not checked.
sum += board[n] || 0
sum += board[s] || 0
sum += board[w] || 0
sum += board[e] || 0
sum += board[n-1] || 0
sum += board[n+1] || 0
sum += board[s-1] || 0
sum += board[s+1] || 0
if board[index] == 0 # dead
if sum == 3
1
else
0
end
else # alive
if sum < 2 || sum > 3
0
else
1
end
end
end
sleep 0.1 # add delay to see animation
puts "\n" * 100 # clear screen
end