-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.rb
80 lines (72 loc) · 1.63 KB
/
Board.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
require './Cell'
class Board
attr_reader :test_array
# I am assuming width and height are the same, since it is an inifinite board.
# number of cells is size*size (e.g size of 6 will create a board of 36)
def initialize(initial_board=nil)
@size = initial_board && initial_board.length || 10
@initial_board = initial_board # used when existing patterns are passed
@cells = []
if @initial_board
@size.times do |x|
@cells.push([])
@size.times do |y|
@cells[x].push(Cell.new(self, x, y, @initial_board[x][y]))
end
end
else
@size.times do |x|
@cells.push([])
@size.times do |y|
@cells[x].push(Cell.new(self, x, y))
end
end
end
end
def print_board()
puts "printing board"
@cells.each {|row|
row.each {|cell|
cell.print_cell
}
puts ''
}
end
def num_live_neighbors(x,y)
board = @cells
n = @size
# ruby wraps the board when negative values are involved
live_neighbors = board[x-1][y-1].value +
board[x-1][y].value +
board[x-1][(y+1) % n ].value +
board[x][y-1].value +
board[x][(y+1) % n].value +
board[(x+1) % n] [y-1].value +
board[(x+1) % n] [y].value +
board[(x+1) % n][(y+1) % n].value
live_neighbors
end
def next_turn()
@cells.each {|row|
row.each {|cell|
cell.calculate_new_cell_value()
}
}
@cells.each {|row|
row.each {|cell|
cell.update_to_new_gen_value()
}
}
end
def output_array()
# function used for testing
@test_array = []
@size.times do |x|
@test_array.push([])
@size.times do |y|
test_array[x].push(@cells[x][y].value)
end
end
@test_array
end
end