-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
158 lines (130 loc) · 3.78 KB
/
test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require_relative 'lib/chess'
@board = Board.new
@rook = Rook.new(:white)
@rook2 = Rook.new(:black)
@bishop = Bishop.new(:black)
@pawn2 = Pawn.new(:black)
@Bking = King.new(:black)
@board.data[5][2] = @rook
@board.data[7][3] = @rook2
@board.data[3][2] = @bishop
@board.data[5][6] = @pawn2
@board.data[0][2] = @Bking
@current_move = {start: [5,2], end: [4,2]}
@colour = :white
class MoveValidator
attr_accessor :piece, :board, :start, :finish
def setup(piece, board, start, finish)
@piece, @board, @start, @finish = piece, board, start, finish
end
def get_moves
piece_moves.each do |coordinates|
end
end
private
def piece_moves
piece.move_set
end
end
puts @board
move = MoveValidator.new
puts move.valid_move?(@rook, @board, @current_move[:start],@current_move[:end])
puts move.king_in_sight?(@rook, @board, @current_move[:start],@current_move[:end])
#puts MOVES[@rook.class.to_s.to_sym].inspect
=begin
class MoveValidator
attr_accessor :piece, :board, :start, :finish
MOVES = {
Bishop: [[-1, -1], [-1, 1], [1, 1], [1, -1]],
King: [[-1, -1], [-1, 1], [1, 1], [1, -1], [-1, 0], [0, 1], [1, 0], [0, -1]],
Knight: [[-2, 1], [-2, -1], [-1, 2], [-1, -2],[1, -2], [1, 2], [2, 1], [2, -1]],
Queen: [[-1, -1], [-1, 1], [1, 1], [1, -1],[-1, 0], [0, 1], [1, 0], [0, -1]],
Rook: [[-1, 0], [0, 1], [1, 0], [0, -1]],
BlackPawn: [[-1,0]],
WhitePawn: [[1,0]]
}
def valid_move?(piece, board, start, finish)
setup(piece, board, start, finish)
moves.include?(finish)
end
def king_in_sight?(piece, board, start, finish)
setup(piece, board, start, finish)
!sights.empty?
end
def sights
sights = {
:king_location => [],
:blockers => []
}
get_piece_coordinates.each do |cord|
next_node = start
loop do
next_node = update_node(next_node, cord)
begin
board.square(*next_node)
rescue IndexError
break
end
break if next_node.any? {|num| num < 0}
if board.square(*next_node).kind_of?(King) && board.square(*next_node).colour != piece.colour
sights[:king_location] << next_node
end
end
end
puts sights.inspect
sights
end
def travel_back(start, king_location)
next_node.zip(cord).map {|x,y| x - y }
end
def moves
moves = []
get_piece_coordinates.each do |cord|
next_node = start
loop do
next_node = update_node(next_node, cord)
begin
board.square(*next_node)
rescue IndexError
break
end
break if next_node.any? {|num| num < 0}
if board.square(*next_node).nil?
moves << next_node
break if piece.class == King
break if piece.class == Knight
break if piece.class == Pawn
next
end
if board.square(*next_node).colour != piece.colour
moves << next_node unless piece.class == Pawn
break
end
break if board.square(*next_node).colour == piece.colour
end
end
if piece.class == Pawn
moves << check_diagonal_pawn_moves
end
puts moves.inspect
moves
end
def check_diagonal_pawn_moves
return_arr = []
diagonal_moves = piece.colour == :white ? [[1,1],[1,-1]] : [[-1,1],[-1,-1]]
diagonal_moves.each do |cord|
next_node = update_node(start, cord)
next if board.square(*next_node).nil?
return_arr << next_node unless board.square(*next_node).colour == piece.colour
end
return_arr.empty? ? nil : return_arr.flatten
end
private
def update_node(next_node, cord)
next_node.zip(cord).map {|x,y| x - y }
end
def setup(piece, board, start, finish)
@piece, @board, @start, @finish = piece, board, start, finish
end
end
=end