-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.rb
executable file
·51 lines (41 loc) · 1 KB
/
piece.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
require 'debugger'
require './board.rb'
require 'colorize'
class Piece
attr_reader :color, :board
attr_accessor :position
def initialize(position, board, color)
@position = position
@board = board
@color = color
end
def opponent?(pos)
return false if @board.rows[pos[0]][pos[1]].nil?
@color != @board.rows[pos[0]][pos[1]].color
end
def on_board?(pos)
pos.all? { |coord| (0...8).cover?(coord) }
end
def inspect
"#{@color} #{self.class} pos: #{@position} moves: #{moves}"
end
def piece_dup(new_board)
self.class.new(@position.dup, new_board, @color)
end
def moves
raise NotImplementedError
end
def valid_moves
moves.select { |move| !move_into_check?(move)}
end
def get_sprite
@color == :black ? self.class::SPRITE.black : self.class::SPRITE.white
end
private
def move_into_check?(target_pos)
dup_board = @board.dup
dup_board.test_move(@position, target_pos)
return true if dup_board.in_check?(@color)
false
end
end