-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
128 lines (106 loc) · 2.72 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
# encoding: utf-8
require_relative 'pieces/pawn'
require_relative 'pieces'
require_relative 'exceptions'
require 'colorize'
class Board
attr_accessor :grid
def initialize(grid = nil)
if grid.nil?
@grid = Array.new(8) {Array.new(8)}
set_up_pawns
set_up_others
else
@grid = grid
end
end
def checkmate?(color)
return false unless in_check?(color)
pieces.select { |piece| piece.color == color }.all? do |piece|
piece.valid_moves.empty?
end
end
def pieces
@grid.flatten.compact
end
def within_bounds?(x, y)
[x, y].min >= 0 && [x, y].max < 8
end
def display_grid
puts " 0 1 2 3 4 5 6 7"
puts
@grid.each_with_index do |row, index|
print "#{index} "
row.each_with_index do |tile, idx|
bg_color = (idx.odd? && index.odd?) || (idx.even? && index.even?) ? :red : :white
if tile.nil?
print " ".colorize(:color => :black, :background => bg_color)
else
print "#{tile.render_unicode} ".colorize(:color => :black, :background => bg_color)
end
end
puts
end
end
def [](pos)
x, y = pos
@grid[x][y]
end
def move(start_pos, end_pos)
unless self[start_pos].valid_moves.include?(end_pos)
raise MovePieceError.new("Invalid ending position")
end
move!(start_pos, end_pos)
end
def move!(start_pos, end_pos)
a, b = start_pos
x, y = end_pos
@grid[x][y] = @grid[a][b]
@grid[a][b] = nil
@grid[x][y].position = end_pos
end
def in_check?(color)
king_position = find_king_pos(color)
return true if possible_opposing_moves(color).include?(king_position)
end
def dup
duped_board = Board.new
pieces.each do |piece|
piece.class.new(duped_board, piece.position, piece.color)
end
duped_board
end
private
def find_king_pos(color)
pieces.each do |piece|
return piece.position if (piece.is_a?(King) && piece.color == color)
end
end
def possible_opposing_moves(color)
opposing_pieces = pieces.select do |piece|
piece.color != color
end
possible_moves = opposing_pieces.inject([]) do |result, piece|
result.concat(piece.moves)
end
possible_moves.uniq
end
def set_up_pawns
[1, 6].each do |row|
color = row == 1 ? "black" : "white"
@grid[row].each_index do |col|
@grid[row][col] = Pawn.new(self, [row, col], color)
end
end
end
BACKROW_PIECES =
[Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook]
def set_up_others
[0, 7].each do |row|
color = row == 0 ? "black" : "white"
BACKROW_PIECES.each_with_index do |piece, col|
@grid[row][col] = piece.new(self, [row, col], color)
end
end
end
end