-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
179 lines (142 loc) · 4.01 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
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
170
171
172
173
174
175
176
177
178
179
require_relative 'sliding_piece.rb'
require_relative 'stepping_piece.rb'
require_relative 'pawn.rb'
class Board
SIZE ||= 8
NON_PAWNS ||= [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook]
def initialize(all_nils = false)
@grid = Array.new(SIZE) { Array.new(SIZE) }
place_pieces unless all_nils
end
def in_check?(color)
king = find_king(color)
other_color = (color == "black" ? "white" : "black")
find_team(other_color).each do |piece|
piece.moves.each do |pos|
return true if pos == king.pos
end
end
false
end
def checkmate?(color)
return false unless in_check?(color)
find_team(color).each do |piece|
return false unless piece.valid_moves.empty?
end
true
end
def move(start_pos, end_pos, player_color)
piece = piece_at(start_pos)
no_piece_error = ArgumentError.new "You don't have a piece there"
wrong_color_error = ArgumentError.new "This is not your piece!"
invalid_move_error = ArgumentError.new "You can't move there!"
check_error = ArgumentError.new "That move move would put you in check, idiot!"
raise no_piece_error if piece.nil?
raise wrong_color_error unless player_color == piece.color
raise invalid_move_error unless piece.moves.include?(end_pos)
raise check_error unless piece.valid_moves.include?(end_pos)
move!(start_pos, end_pos)
end
def move!(start_pos, end_pos)
piece = piece_at(start_pos)
self[start_pos[0], start_pos[1]] = nil
self[end_pos[0], end_pos[1]] = piece
piece.pos = end_pos
end
def deep_dup
new_board = Board.new(true)
all_pieces = find_team("white") + find_team("black")
all_pieces.each do |piece|
x = piece.pos[0]
y = piece.pos[1]
new_board[x,y] = piece.class.new(piece.color, piece.pos, new_board)
end
new_board
end
def display
display_col_numbers
@grid.each_with_index do |row, idx|
char_num = idx + 97
print "#{char_num.chr} "
row.each_with_index do |piece, idx2|
cell_string = ""
cell_string += piece.nil? ? " " : " #{piece.picture} "
print cell_string.colorize(colorize_hash(idx, idx2, piece))
end
print " #{char_num.chr}"
puts ""
end
display_col_numbers
nil
end
def piece_at(pos)
self[pos[0], pos[1]] if on_board?(pos)
end
def [](x, y)
@grid[x][y]
end
def []=(x, y, piece)
@grid[x][y] = piece
end
def on_board?(potential_pos)
(0...SIZE).include?(potential_pos[0]) && (0...SIZE).include?(potential_pos[1])
end
def color_of_piece_at(pos)
piece = piece_at(pos)
piece.nil? ? nil : piece.color
end
def inspect
"I am a board"
end
private
def colorize_hash(idx, idx2, piece)
colorize_hash = {}
colorize_hash[:color] = text_color(piece) unless piece.nil?
colorize_hash[:background] = background_color(idx, idx2)
colorize_hash
end
def display_col_numbers
puts " " + (' 0 '..' 7 ').to_a.join("")
end
def background_color(idx, idx2)
totalIdx = idx + idx2
background_color = totalIdx % 2 == 0 ? :light_blue : :red
end
def text_color(piece)
piece.color == "white" ? :white : :black
end
def place_pieces
place_non_pawns("black")
place_non_pawns("white")
place_pawns("black")
place_pawns("white")
end
def place_non_pawns(color)
x = color == "black" ? 0 : (SIZE - 1)
NON_PAWNS.each_with_index do |val, idx|
self[x, idx] = val.new(color, [x, idx], self)
end
end
def place_pawns(color)
x = color == "black" ? 1 : (SIZE - 2)
(0...SIZE).each do |i|
self[x, i] = Pawn.new(color, [x, i], self)
end
end
def find_king(color)
find_team(color).find do |piece|
piece.class == King
end
end
def find_team(color)
find_all.select do |piece|
piece.color == color
end
end
def find_all
@grid.flatten.compact
end
def occupied?(potential_pos)
@grid
end
end