-
Notifications
You must be signed in to change notification settings - Fork 0
/
queen.rb
71 lines (60 loc) · 1.86 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
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
require_relative "board"
require_relative "slideable"
class Queen < Piece
include Slideable
def initialize(board,position,color)
mark = color == :white ? " " + "\u2655".encode + " " : " " + "\u265B".encode + " "
super(board, position, mark, color)
end
def moves
possible_moves = []
straight_lines = diagonal(@position)
straight_lines.each do |new_pos|
seen_piece = false
dx = new_pos[0] - @position[0]
dy = new_pos[1] - @position[1]
dir = [dx / dx.abs, dy / dy.abs]
(1..dx.abs).each do |multiplier|
break if seen_piece
change = dir.map { |e| e * multiplier }
step = [change[0] + @position[0] , change[1] + @position[1]]
if @board.piece_exist?(step)
seen_piece = true
if @board.piece_at_position(step).color == @board.other_color(@color)
possible_moves << step unless possible_moves.include?(step)
end
else
possible_moves << step
end
end
end
straight_lines = horizontal(@position)
straight_lines.each do |new_pos|
seen_piece = false
dx = new_pos[0] - @position[0]
dy = new_pos[1] - @position[1]
if dx.zero?
dir = [0, dy / dy.abs]
else
dir = [dx / dx.abs, 0]
end
(1..[dx.abs, dy.abs].max).each do |multiplier|
break if seen_piece
change = dir.map { |e| e * multiplier }
step = [change[0] + @position[0] , change[1] + @position[1]]
if @board.piece_exist?(step)
seen_piece = true
if @board.piece_at_position(step).color == @board.other_color(@color)
possible_moves << step unless possible_moves.include?(step)
end
else
possible_moves << step
end
end
end
possible_moves
end
def valid_move?(new_pos)
moves.include?(new_pos)
end
end