/
sliding_piece.rb
140 lines (124 loc) · 4.4 KB
/
sliding_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
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
module SlidingPiece
SLIDINGMOVES = {
:upleft => [-1, -1],
:upright => [-1, 1],
:downleft => [1, -1],
:downright => [1, 1],
:down => [1,0],
:up => [-1,0],
:left => [0,-1],
:right => [0,1]
}
def possible_moves
possible_moves = []
if type.include?(:diag)
ul_or_dr = []
#moving up and left
cur_pos = [@pos[0] + SLIDINGMOVES[:upleft][0], @pos[1] + SLIDINGMOVES[:upleft][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
ul_or_dr << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:upleft][0], cur_pos[1] + SLIDINGMOVES[:upleft][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
ul_or_dr << cur_pos
end
end
#moving down and right
cur_pos = [@pos[0] + SLIDINGMOVES[:downright][0], @pos[1] + SLIDINGMOVES[:downright][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
ul_or_dr << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:downright][0], cur_pos[1] + SLIDINGMOVES[:downright][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
ul_or_dr << cur_pos
end
end
ur_or_dl = []
#moving down and left
cur_pos = [@pos[0] + SLIDINGMOVES[:downleft][0], @pos[1] + SLIDINGMOVES[:downleft][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
ul_or_dr << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:downleft][0], cur_pos[1] + SLIDINGMOVES[:downleft][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
ul_or_dr << cur_pos
end
end
#moving up and right
cur_pos = [@pos[0] + SLIDINGMOVES[:upright][0], @pos[1] + SLIDINGMOVES[:upright][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
ul_or_dr << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:upright][0], cur_pos[1] + SLIDINGMOVES[:upright][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
ul_or_dr << cur_pos
end
end
possible_moves << ur_or_dl
possible_moves << ul_or_dr
end
if type.include?(:straight)
up_or_down = []
#moving up
cur_pos = [@pos[0] + SLIDINGMOVES[:up][0], @pos[1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
up_or_down << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:up][0], cur_pos[1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
up_or_down << cur_pos
end
end
#moving down and right
cur_pos = [@pos[0] + SLIDINGMOVES[:down][0], @pos[1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
up_or_down << cur_pos
cur_pos = [cur_pos[0] + SLIDINGMOVES[:down][0], cur_pos[1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
up_or_down << cur_pos
end
end
left_or_right = []
#moving left
cur_pos = [@pos[0], @pos[1] + SLIDINGMOVES[:left][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
left_or_right << cur_pos
cur_pos = [cur_pos[0], cur_pos[1] + SLIDINGMOVES[:left][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
left_or_right << cur_pos
end
end
#moving up and right
cur_pos = [@pos[0], @pos[1] + SLIDINGMOVES[:right][1]]
while (board.valid_pos?(cur_pos) &&
board.grid[cur_pos[0]][cur_pos[1]].class == NullPiece)
left_or_right << cur_pos
cur_pos = [cur_pos[0], cur_pos[1] + SLIDINGMOVES[:right][1]]
end
if board.valid_pos?(cur_pos)
if board.grid[cur_pos[0]][cur_pos[1]].color != @color
left_or_right << cur_pos
end
end
possible_moves << left_or_right
possible_moves << up_or_down
end
possible_moves.flatten(1).uniq
end
end