-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueen.rb
46 lines (44 loc) · 1.13 KB
/
Queen.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
require_relative 'Piece'
class Queen < Piece
def initialize x,y,board,color
super x,y,board,color,10
end
def check_possible_moves
@possible_moves = []
if @alive
possible_moves = []
xy=[[1,0],[0,-1],[-1,0],[0,1],[1,1],[1,-1],[-1,1],[-1,-1]]
(1..8).each do |i|
moves = xy.map{|m| [@x+m[0]*i,@y+m[1]*i]}
i=0
n=moves.length
while i<n
if moves[i][0]>0 and moves[i][1]>0 and moves[i][0]<=8 and moves[i][1]<=8 and @board[moves[i][0]][moves[i][1]].piece != nil
xy.delete_at(i)
if @board[moves[i][0]][moves[i][1]].piece.color == @color
moves.delete_at(i)
i=i-1
n=n-1
end
end
i=i+1
end
possible_moves = possible_moves+moves
end
possible_moves.each do |move|
if move[0]>0 and move[1]>0 and move[0]<=8 and move[1]<=8
# puts 'debug '+move.to_s
if @board[move[0]][move[1]].piece == nil or @board[move[0]][move[1]].piece.color != @color
if @board[move[0]][move[1]].piece
move[2] = @board[move[0]][move[1]].piece.value
else
move[2] = 1
end
@possible_moves << move
end
end
end
end
@possible_moves
end
end