-
Notifications
You must be signed in to change notification settings - Fork 0
/
knight.rb
32 lines (26 loc) · 838 Bytes
/
knight.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
class Knight < Piece
POSSIBLE_CHANGES = [[1, 2], [2, 1], [-1, 2], [-2, -1],
[-2, 1], [1, -2], [2, -1], [-1, -2]]
def initialize(board,position,color)
mark = color == :white ? " " + "\u2658".encode + " " : " " + "\u265E".encode + " "
super(board, position, mark, color)
end
def moves
possible_moves = []
x, y = @position
POSSIBLE_CHANGES.each do |change|
update_position = [change[0] + x, change[1] + y]
if @board.piece_exist?(update_position)
if @board.piece_at_position(update_position).color == other_color(@color)
possible_moves << update_position
end
elsif @board.in_bounds?(update_position)
possible_moves << update_position
end
end
possible_moves
end
def valid_move?(pos)
moves.include?(pos)
end
end