-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.rb
91 lines (68 loc) · 1.4 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
require_relative "piece"
class Board
attr_accessor :squares, :size
def initialize(size = 8)
@size = size
@squares = Array.new(@size) {Array.new(@size)}
end
def display
@size.times do |x|
@size.times do |y|
if @squares[x][y].nil?
print "_ "
else
print @squares[x][y].display + " "
end
end
print "\n"
end
end
def set_board
set_pieces(:white, 0)
set_pieces(:black, @size - 3)
end
def set_pieces(color, row)
3.times do |x|
@size.times do |y|
Piece.new([(row + x), y], color, self) if black_square?([(row + x), y])
end
end
end
def place_piece(piece, pos)
x, y = pos
@squares[x][y] = piece
piece.pos = pos
end
def remove_piece(pos)
x, y = pos
@squares[x][y] = nil
end
def get_piece(pos)
x, y = pos
@squares[x][y]
end
def black_square?(pos)
x, y = pos
if (x % 2 == 0 && y % 2 == 1) || (x % 2 == 1 && y % 2 == 0)
true
else
false
end
end
def dup
new_board = Board.new(@size)
@squares.flatten.compact.each do |piece|
piece.dup(new_board)
end
new_board
end
def winner?(color)
moves = []
pieces = @squares.flatten.compact.select {|piece| piece.color == color}
pieces.each do |piece|
moves += piece.slide_moves
moves += piece.jump_moves
end
moves.empty?
end
end