-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
293 lines (256 loc) · 7.63 KB
/
game.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
class Array # for holes
def x; self[0]; end
def y; self[1]; end
end
class Board < Array
attr_accessor :origin, :parent
attr_reader :hole1, :hole2, :possibilities
def initialize(*args)
super *args
@origin = :initial
@possibilities = []
# process_movements
end
def deep_clone
board = Board.new
self.each do |row|
board << row.clone
end
board
end
def possibilities
return @possibilities if @possibilities.size > 0
find_and_set_holes
hole = @hole1
oole = @hole2
@possibilities += one_hole_movements(@hole2)
@possibilities += one_hole_movements(@hole1)
@possibilities += double_hole_movements(@hole1, @hole2)
@possibilities.each { |p| p.parent = self }
end
def show_board
puts "** #{origin} **"
puts self
puts
end
def show_possibilities
possibilities.each do |board|
board.show_board
end
end
def show_board_and_possibilities
show_board
show_possibilities
end
protected
def one_hole_movements(hole)
possibilities = []
# Movements with one hole
# =======================
# when :up
if above(hole) == "o"
b = deep_clone
b.origin = :small_square_down
b[hole.y] [hole.x] = "o"
b[hole.y - 1][hole.x] = " "
possibilities << b
elsif above(hole) == "|"
b = deep_clone
b.origin = :vertical_bar_down
b[hole.y] [hole.x] = "|"
b[hole.y - 1][hole.x] = "I"
b[hole.y - 2][hole.x] = " "
possibilities << b
end
# when :down
if below(hole) == "o"
b = deep_clone
b.origin = :small_square_up
b[hole.y] [hole.x] = "o"
b[hole.y + 1][hole.x] = " "
possibilities << b
elsif below(hole) == "I"
b = deep_clone
b.origin = :vertical_bar_up
b[hole.y] [hole.x] = "I"
b[hole.y + 1][hole.x] = "|"
b[hole.y + 2][hole.x] = " "
possibilities << b
end
# when :left
if left(hole) == "o"
b = deep_clone
b.origin = :small_square_right
b[hole.y][hole.x] = "o"
b[hole.y][hole.x - 1] = " "
possibilities << b
elsif left(hole) == "="
b = deep_clone
b.origin = :horizontal_bar_right
b[hole.y][hole.x] = "="
b[hole.y][hole.x - 2] = " "
possibilities << b
end
# when :right
if right(hole) == "o"
b = deep_clone
b.origin = :small_square_left
b[hole.y][hole.x] = "o"
b[hole.y][hole.x + 1] = " "
possibilities << b
elsif right(hole) == "="
b = deep_clone
b.origin = :horizontal_bar_left
b[hole.y][hole.x] = "="
b[hole.y][hole.x + 2] = " "
possibilities << b
end
possibilities
end
def double_hole_movements(hole, oole)
possibilities = []
# Movements with two holes
if horizontal_neighbor?(hole, oole)
if above(hole) == "=" && above(oole) == "="
b = deep_clone
b.origin = :horizontal_bar_down
b[hole.y][hole.x] = "="
b[oole.y][oole.x] = "="
b[hole.y - 1][hole.x] = " "
b[oole.y - 1][oole.x] = " "
possibilities << b
elsif above(hole) == "@" && above(oole) == "@"
b = deep_clone
b.origin = :square_up
b[hole.y][hole.x] = "@"
b[oole.y][oole.x] = "@"
b[hole.y - 2][hole.x] = " "
b[oole.y - 2][oole.x] = " "
possibilities << b
end
if below(hole) == "=" && below(oole) == "="
b = deep_clone
b.origin = :horizontal_bar_up
b[hole.y][hole.x] = "="
b[oole.y][oole.x] = "="
b[hole.y + 1][hole.x] = " "
b[oole.y + 1][oole.x] = " "
possibilities << b
elsif below(hole) == "@" && below(oole) == "@"
b = deep_clone
b.origin = :square_up
b[hole.y][hole.x] = "@"
b[oole.y][oole.x] = "@"
b[hole.y + 2][hole.x] = " "
b[oole.y + 2][oole.x] = " "
possibilities << b
end
elsif vertical_neighbor?(hole, oole)
if left(hole) == "I" # hole.x is always lesser than oole.x
b = deep_clone
b.origin = :vertical_bar_right
b[hole.y][hole.x] = "I"
b[oole.y][oole.x] = "|"
b[hole.y][hole.x - 1] = " "
b[oole.y][oole.x - 1] = " "
possibilities << b
elsif left(hole) == "@" && left(oole) == "@"
b = deep_clone
b.origin = :square_right
b[hole.y][hole.x] = "@"
b[oole.y][oole.x] = "@"
b[hole.y][hole.x - 2] = " "
b[oole.y][oole.x - 2] = " "
possibilities << b
end
if right(hole) == "I" # hole.x is always lesser than oole.x
b = deep_clone
b.origin = :vertical_bar_left
b[hole.y][hole.x] = "I"
b[oole.y][oole.x] = "|"
b[hole.y][hole.x + 1] = " "
b[oole.y][oole.x + 1] = " "
possibilities << b
elsif right(hole) == "@" && right(oole) == "@"
b = deep_clone
b.origin = :square_left
b[hole.y][hole.x] = "@"
b[oole.y][oole.x] = "@"
b[hole.y][hole.x + 2] = " "
b[oole.y][oole.x + 2] = " "
possibilities << b
end
end
possibilities
end
def find_and_set_holes
@hole1 = nil
@hole2 = nil
5.times do |y|
4.times do |x|
@hole1 ? @hole2 = [x, y] : @hole1 = [x, y] if self[y][x] == 32
end
end
end
def above(hole); hole.y == 0 ? nil : self[hole.y - 1][hole.x].chr; end # rescue; puts self;
def below(hole); hole.y == 4 ? nil : self[hole.y + 1][hole.x].chr; end
def left(hole); hole.x == 0 ? nil : self[hole.y][hole.x - 1].chr; end
def right(hole); hole.x == 3 ? nil : self[hole.y][hole.x + 1].chr; end
def horizontal_neighbor?(hole, oole)
hole.y == oole.y && (hole.x - oole.x).abs == 1
end
def vertical_neighbor?(hole, oole)
hole.x == oole.x && (hole.y - oole.y).abs == 1
end
end
class Game
attr_accessor :initial_board
def initialize(initial_board)
@initial_board = Board.new(initial_board)
end
def search_board(level, final_board)
@all_boards = []
@counter = 0
@counters = []
@level = level
puts "== #{@initial_board.origin} =="
puts @initial_board
puts
recursive_search_board(level + 1, [@initial_board], final_board)
end
def show_possibilities
@initial_board.show_board_and_possibilities
end
protected
def recursive_search_board(level, boards, final_board)
level -= 1
return if level == 0
possibilities = []
for board in boards
# @counters[@level - level] ? @counters[@level - level] += 1 : @counters[@level - level] = 1
@counter += 1
if board[0][1].chr == "@" && board[0][2].chr == "@"
puts "FOUND! Movements: #{@level - level}"
# board.show_board
parent = board
until parent == nil do
parent.show_board
parent = parent.parent
end
return true # parar quando encontrar o estado final
end
possibilities += board.possibilities unless @all_boards.include?(board)
@all_boards << board
end
puts "Level #{@level - level} :\t #{boards.size} movimentos calculados (#{@counter} no total)"
@found = recursive_search_board(level, possibilities, final_board)
end
end
game = Game.new [
"I I",
"|oo|",
"o==o",
"I@@I",
"|@@|"
]
game.search_board(130, "")