-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.rb
80 lines (72 loc) · 1.32 KB
/
script.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
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
def build_board(width, height)
board = []
height.times do |x|
board.push([])
width.times do |y|
board[x].push([0,1].sample)
end
end
board
end
def print_board(board)
puts "printing board"
board.each {|row|
row.each {|cell|
if cell == 1
char = "*"
else
char = "_"
end
print char
}
puts ''
}
end
def calculate_cell_value(board, x, y)
# calculate value of one cell
n = board.size
neighbor_value = board[x-1][y-1] +
board[x-1][y] +
board[x-1][(y+1) % n ] +
board[x][y-1] +
board[x][(y+1) % n] +
board[(x+1) % n] [y-1] +
board[(x+1) % n] [y] +
board[(x+1) % n][(y+1) % n]
if board[x][y] == 0 #if cell is dead
if neighbor_value == 3
cell_value = 1
else
cell_value = 0
end
else #if cell is alive
if neighbor_value < 2
cell_value = 0
elsif neighbor_value > 3
cell_value = 0
else
cell_value = 1
end
end
cell_value
end
def build_new_board(old_board, width, height)
new_board = build_board(width, height)
height.times do |x|
width.times do |y|
new_board[x][y] = calculate_cell_value(old_board, x, y)
end
end
new_board
end
def run
size = 5
board = [[0,0,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,1,1,1,0],[0,0,0,0,0]]
while true
puts '======='
print_board(board)
board = build_new_board(board, size, size)
sleep 1
end
end
run