-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckers.rb
178 lines (143 loc) · 3.64 KB
/
checkers.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
class Piece
attr_reader :color
def initialize(pos, board, king = false, start_pos = nil)
@board = board
@king = king
@pos = pos
@start_pos = start_pos || @pos
@color = set_color(@start_pos)
@board[@pos] = self
end
def perform_moves!(move_seq)
if move_seq.count == 1
perform_slide(move_seq.first) || perform_jump(move_seq.first)
else
move_seq.each do |move|
return unless perform_jump(move)
end
end
end
def perform_slide(move)
return false if !diagonal_moves.include?(move) || @board[move]
exec_move(move)
end
def valid_move_seq?(move_seq)
dummy_board = @board.dup
dummy_self = dummy_board[@pos]
dummy_self.perform_moves!(move_seq)
end
def perform_moves(move_seq)
if valid_move_seq?(move_seq)
perform_moves!(move_seq)
else
raise 'Invalid Move'
end
end
def perform_jump(move)
delta_used = delta_math(move, @pos.map {|coord| coord*(-1)})
jumped_space = delta_math(@pos, delta_used.map {|coord| (coord*0.5).to_i})
return false unless diagonal_moves(2).include?(move) && @board[move].nil? &&
@board[jumped_space].is_enemy?(self)
@board[jumped_space] = nil
exec_move(move)
end
def exec_move(move)
@board[move] = self
@board[@pos] = nil
@pos = move
maybe_promote
true
end
def is_enemy?(piece)
return false if self.nil? || self.color == piece.color
true
end
def y_directions
return [1,-1] if @king
@color == :white ? [-1] : [1]
end
def dup(duped_board)
dummy_piece = Piece.new(@pos, duped_board, @king, @start_pos)
end
def set_color(start)
start[0] >= @board.grid.count / 2 ? :white : :black
end
def maybe_promote
@king = true if (@color == :white && @pos.first == 0) ||
(@color == :black && @pos.first == @board.grid.count-1)
end
def diagonal_moves(length = 1)
deltas = []
y_directions.each do |y_dir|
[1,-1].each do |x_dir|
deltas << [y_dir * length, x_dir * length]
end
end
new_locations = deltas.map {|dy, dx| [@pos.first + dy, @pos.last + dx]}
end
def delta_math(pos, delt)
[pos.first + delt.first, pos.last + delt.last]
end
end
class Board
attr_reader :grid, :size
def initialize(size = 8, self_pop = true)
@size = size
@grid = Array.new(size) {Array.new(size)}
initialize_sides if self_pop
end
def [](pos)
grid[pos.first][pos.last]
end
def []=(pos, piece)
raise "Invalid Space" if pos.any? {|coord| !coord.between?(0, @grid.count-1)}
grid[pos.first][pos.last] = piece
end
def clear(pos)
self[pos]= nil
end
def dup
duped_board = Board.new(@size, false)
@grid.each do |row|
row.each do |piece|
piece.dup(duped_board) if piece
end
end
duped_board
end
def all_pieces(color)
@grid.flatten.compact.select {|piece| piece.color == color}
end
def has_moves?(color)
all_pieces(color).any? do |piece|
piece.diagonal_moves.each do |move|
return true if piece.valid_move_seq?([move])
end
end
end
def initialize_sides
(0..2).each {|row| initialize_row(row)}
(@grid.count-3...@grid.count).each {|row| initialize_row(row)}
end
def render
@grid.each do |row|
row.each do |pos|
if pos.nil?
print " "
else
print pos.color == :white ? '○' : '●'
end
end
puts
end
nil
end
def initialize_row(row)
offset = row % 2 == 0 ? 0 : 1
(0...@grid.count).each do |col|
next if col - offset != 0
Piece.new([row, col], self)
offset += 2
end
end
end