-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathking.rb
75 lines (63 loc) · 2.36 KB
/
king.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
class King < Piece
POSSIBLE_CHANGES = [[0,1], [1,1],[1,0], [1,-1], [0,-1], [-1,-1], [-1,0], [-1,1]]
attr_reader :can_castle
def initialize(board,position,color)
mark = color == :white ? " " + "\u2654".encode + " " : " " + "\u265A".encode + " "
super(board, position, mark, color)
@can_castle = true
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
#Castling feature
if @can_castle
#King side
rook = @board.piece_at_position([@position[0], 0])
if rook.is_a?(Rook) && rook.can_castle
#Making sure there is a rook there and that the rook can still castle
castle_positions = [[@position[0],1], [@position[0],2]]
#Checking to make sure there are no pieces
if castle_positions.all? {|pos| @board.piece_at_position(pos).is_a?(EmptyPiece) && !in_check?(pos)}
possible_moves << [@position[0],1]
end
end
rook_queen = @board.piece_at_position([@position[0], 7])
if rook_queen.is_a?(Rook) && rook_queen.can_castle
#Making sure there is a rook there and that the rook can still castle
castle_positions = [[@position[0],4], [@position[0],5], [@position[0],6] ]
#Checking to make sure there are no pieces
if castle_positions.all? {|pos| @board.piece_at_position(pos).is_a?(EmptyPiece) && !in_check?(pos)}
possible_moves << [@position[0],5]
end
end
end
possible_moves
end
def valid_move?(pos)
moves.include?(pos)
end
def update_pos(new_pos, upgrade = false)
#for king side castle only
if @can_castle
if new_pos[1] == 1
rook = @board.piece_at_position([@position[0],0])
@board.move_rook_castling(rook,[@position[0],0],[@position[0],2])
elsif new_pos[1] == 5 #Queen side
rook = @board.piece_at_position([@position[0],7])
@board.move_rook_castling(rook,[@position[0],7],[@position[0],4])
end
end
@position = new_pos
@can_castle = false
end
end