-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.rb
91 lines (80 loc) · 1.76 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
81
82
83
84
85
86
87
88
89
90
91
class Board
attr_accessor :grid
def self.make_starting_board
board = Board.new
(0..2).each do |row|
(0..7).each do |col|
if row.even? == col.even?
board.place_piece(:red, [row, col])
end
end
end
(5..7).each do |row|
(0..7).each do |col|
if row.even? == col.even?
board.place_piece(:white, [row, col])
end
end
end
board
end
def initialize
@grid = Array.new(8) { Array.new(8) }
end
def place_piece(color, pos)
piece = Piece.new(self, color, pos)
self[pos] = piece
end
def dup
dupped_board = Board.new
@grid.each_with_index do |row, r_index|
row.each_with_index do |tile, c_index|
unless tile.nil?
# TA: what if the piece is already a king?
pos = [r_index, c_index]
color = tile.color
dupped_board.place_piece(color, pos)
end
end
end
dupped_board
end
def capturable?(position, curr_color)
self[position].color != curr_color
end
def inspect
return @grid.each {|r| p r.to_s}
end
def render
color = :black
puts " 0 1 2 3 4 5 6 7"
@grid.each_with_index do |row, i|
print "#{i} "
row.each do |tile|
if tile.nil?
print " ".colorize(:background => color)
else
print tile.render
end
color = toggle_color(color)
end
print " #{i}"
color = toggle_color(color)
puts
end
puts " 0 1 2 3 4 5 6 7"
end
def toggle_color(color)
color == :red ? :black : :red
end
def [](pos)
x = pos[0]
y = pos[1]
@grid[x][y]
end
def []=(pos, value)
x = pos[0]
y = pos[1]
@grid[x][y] = value
end
end