-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.rb
382 lines (300 loc) · 13.6 KB
/
solver.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# input = ARGV.join.tr(',', ' ').gsub(/\[/, ', %w[')
# input[0] = '['
# board = eval input + ']'
board = [%w[W O O O O O O O O W],
%w[O O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[B O O O O O O O O O],
%w[W O O O O O O O O W]]
class Solver
def self.diagonalize board
padding = board.size - 1
padded_matrix = []
board.each do |row|
inverse_padding = board.size - padding
padded_matrix << ([nil] * inverse_padding) + row + ([nil] * padding)
padding -= 1
end
padded_matrix.transpose.map(&:compact)
end
def self.rotate board
board.transpose.map &:reverse
end
attr_accessor :board, :boards, :line_forming_moves
def initialize board
self.board = board
self.boards = {
horizontal: board,
vertical: self.class.rotate(board),
diagonal: self.class.diagonalize(board),
reverse_diagonal: self.class.diagonalize(self.class.rotate(board))
}
self.line_forming_moves = {
'B' => [],
'G' => []
}
find_consecutives
end
def find_consecutives
lines = Hash.new 0
boards.each do |direction, board|
%w[B G R].each do |color|
board.each_with_index do |row, i|
line = row.join
send("detect_#{direction}_winning_move", line, color, i)
if line.include?("W" + color * 8) || line.include?(color * 8 + "W") || line.include?(color * 9)
lines[color] += 2
next
end
if line.include?(color * 5) || line.include?("W" + color * 4) || line.include?(color * 4 + "W")
lines[color] += 1
end
end
end
end
lines
end
def detect_horizontal_winning_move line, color, i
pattern = "W" + color * 3 + "O"
self.line_forming_moves[color] << [i, line.index(pattern) + 4] if line.include? pattern
pattern = "W" + color * 2 + "O" + color
self.line_forming_moves[color] << [i, line.index(pattern) + 3] if line.include? pattern
pattern = "W" + color + "O" + color * 2
self.line_forming_moves[color] << [i, line.index(pattern) + 2] if line.include? pattern
pattern = "W" + "O" + color * 3
self.line_forming_moves[color] << [i, line.index(pattern) + 1] if line.include? pattern
pattern = color + "O" + color * 2 + "W"
self.line_forming_moves[color] << [i, line.index(pattern) + 1] if line.include? pattern
pattern = color * 2 + "O" + color + "W"
self.line_forming_moves[color] << [i, line.index(pattern) + 2] if line.include? pattern
pattern = color * 3 + "O" + "W"
self.line_forming_moves[color] << [i, line.index(pattern) + 3] if line.include? pattern
pattern = color * 4 + "O"
self.line_forming_moves[color] << [i, line.index(pattern) + 4] if line.include? pattern
self.line_forming_moves[color] << [i, line.index(pattern) - 1] if line.include?(pattern) && line.index(pattern) - 1 > 0
pattern = color * 3 + "O" + color
self.line_forming_moves[color] << [i, line.index(pattern) + 3] if line.include? pattern
pattern = color * 2 + "O" + color * 2
self.line_forming_moves[color] << [i, line.index(pattern) + 2] if line.include? pattern
pattern = color + "O" + color * 3
self.line_forming_moves[color] << [i, line.index(pattern) + 1] if line.include? pattern
end
def detect_vertical_winning_move line, color, i
pattern = "W" + color * 3 + "O"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 4, i] if line.include? pattern
pattern = "W" + color * 2 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = "W" + color + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = "W" + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 9 - 1, i] if line.include? pattern
pattern = "O" + color * 3 + "W"
self.line_forming_moves[color] << [line.index(pattern) - 1, i] if line.include? pattern
pattern = color + "O" + color * 2 + "W"
self.line_forming_moves[color] << [line.index(pattern) - 2, i] if line.include? pattern
pattern = color * 2 + "O" + color + "W"
self.line_forming_moves[color] << [line.index(pattern) - 3, i] if line.include? pattern
pattern = color * 3 + "O" + "W"
self.line_forming_moves[color] << [line.index(pattern) - 4, i] if line.include? pattern
pattern = color * 4 + "O"
self.line_forming_moves[color] << [10 - line.index(pattern), i] if line.include? pattern
self.line_forming_moves[color] << [10/2 - line.index(pattern), i] if line.include?(pattern) && line.index(pattern) - 1 > 0 #FIXME: This doesn't seem right
pattern = color * 3 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 2, i] if line.include? pattern
pattern = color * 2 + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 3, i] if line.include? pattern
pattern = color + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 4, i] if line.include? pattern
end
def detect_diagonal_winning_move line, color, i
pattern = "W" + color * 3 + "O"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 4, i] if line.include? pattern
pattern = "W" + color * 2 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = "W" + color + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = "W" + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 9 - 1, i] if line.include? pattern
pattern = color + "O" + color * 2 + "W"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 1, i] if line.include? pattern
pattern = color * 2 + "O" + color + "W"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = color * 3 + "O" + "W"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = color * 4 + "O"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 4, i] if line.include? pattern
self.line_forming_moves[color] << [line.index(pattern) - 9 - 1, i] if line.include?(pattern) && line.index(pattern) - 1 > 0
pattern = color * 3 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = color * 2 + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = color + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 9 - 1, i] if line.include? pattern
end
def detect_reverse_diagonal_winning_move line, color, i
pattern = "W" + color * 3 + "O"
self.line_forming_moves[color] << [line.index(pattern) + 4, i/2 - 1] if line.include? pattern
pattern = "W" + color * 2 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 3, i/2 - 2] if line.include? pattern
pattern = "W" + color + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 2, i/2 - 3] if line.include? pattern
pattern = "W" + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 1, i/2 - 4] if line.include? pattern
pattern = color + "O" + color * 2 + "W"
self.line_forming_moves[color] << [line.index(pattern), i/2 - 3] if line.include? pattern
pattern = color * 2 + "O" + color + "W"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = color * 3 + "O" + "W"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = color * 4 + "O"
self.line_forming_moves[color] << [line.index(pattern) + 9 - 4, i] if line.include? pattern
self.line_forming_moves[color] << [line.index(pattern) - 9 - 1, i] if line.include?(pattern) && line.index(pattern) - 1 > 0
pattern = color * 3 + "O" + color
self.line_forming_moves[color] << [line.index(pattern) + 9 - 3, i] if line.include? pattern
pattern = color * 2 + "O" + color * 2
self.line_forming_moves[color] << [line.index(pattern) + 9 - 2, i] if line.include? pattern
pattern = color + "O" + color * 3
self.line_forming_moves[color] << [line.index(pattern) + 9 - 1, i] if line.include? pattern
end
end
solver = Solver.new board
cons = solver.find_consecutives
print solver.line_forming_moves['B'].to_s + "\n"
print solver.line_forming_moves['G'].to_s + "\n"
print "#{cons['B']}\n#{cons['G']}"
__END__
require 'minitest/autorun'
class SolverTest < MiniTest::Test
def new_board
Array.new(10) { %w[O] * 10 }.tap {|a| a[0][0] = a[0][9] = a[9][0] = a[9][9] = "W" }
end
def rotate board, iterations=1
iterations.times.map do
board =Solver.rotate board
end
board
end
def test_horizontal_WOBBB
board = new_board.tap { |b| b[0] = %w[W O B B B O O O O W] }
solver = Solver.new board
assert_equal [[0, 1]], solver.line_forming_moves['B']
end
def test_horizontal_WBOBB
board = new_board.tap { |b| b[0] = %w[W B O B B O O O O W] }
solver = Solver.new board
assert_equal [[0, 2]], solver.line_forming_moves['B']
end
def test_horizontal_WBBOB
board = new_board.tap { |b| b[0] = %w[W B B O B O O O O W] }
solver = Solver.new board
assert_equal [[0, 3]], solver.line_forming_moves['B']
end
def test_horizontal_WBBBO
board = new_board.tap { |b| b[0] = %w[W B B B O O O O O W] }
solver = Solver.new board
assert_equal [[0, 4]], solver.line_forming_moves['B']
end
def test_horizontal_WOBBBBO
board = new_board.tap { |b| b[0] = %w[W O B B B B O O O W] }
solver = Solver.new board
assert_equal [[0, 1], [0, 6], [0, 1]], solver.line_forming_moves['B']
end
def test_horizontal_WOOBBBBO
board = new_board.tap { |b| b[0] = %w[W O O B B B B O O W] }
solver = Solver.new board
assert_equal [[0, 7], [0, 2]], solver.line_forming_moves['B']
end
def test_horizontal_BOBBB
board = new_board.tap { |b| b[1] = %w[O O O B O B B B O O] }
solver = Solver.new board
assert_equal [[1, 4]], solver.line_forming_moves['B']
end
def test_horizontal_BBOBB
board = new_board.tap { |b| b[1] = %w[O O O B B O B B O O] }
solver = Solver.new board
assert_equal [[1, 5]], solver.line_forming_moves['B']
end
def test_horizontal_BBBOB
board = new_board.tap { |b| b[1] = %w[O O O B B B O B O O] }
solver = Solver.new board
assert_equal [[1, 6]], solver.line_forming_moves['B']
end
def test_horizontal_WOBBBBBO_empty
skip
board = new_board.tap { |b| b[0] = %w[W O B B B B B O O W] }
solver = Solver.new board
assert_equal [[]], solver.line_forming_moves['B']
end
def test_vertical_WOBBB
board = rotate new_board.tap { |b| b[0] = %w[W O B B B O O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[1, 0]], solver.line_forming_moves['B']
end
def test_vertical_WOBBB
board = rotate new_board.tap { |b| b[0] = %w[W O B B B O O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 1].reverse], solver.line_forming_moves['B']
end
def test_vertical_WBOBB
board = rotate new_board.tap { |b| b[0] = %w[W B O B B O O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 2].reverse], solver.line_forming_moves['B']
end
def test_vertical_WBBOB
board = rotate new_board.tap { |b| b[0] = %w[W B B O B O O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 3].reverse], solver.line_forming_moves['B']
end
def test_vertical_WBBBO
board = rotate new_board.tap { |b| b[0] = %w[W B B B O O O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 4]].map(&:reverse), solver.line_forming_moves['B']
end
def test_vertical_WOBBBBO
board = rotate new_board.tap { |b| b[0] = %w[W O B B B B O O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 1], [0, 6], [0, 1]].map(&:reverse), solver.line_forming_moves['B']
end
def test_vertical_WOOBBBBO
board = rotate new_board.tap { |b| b[0] = %w[W O O B B B B O O W].reverse }, 3
solver = Solver.new board
assert_equal [[0, 7], [0, 2]].map(&:reverse), solver.line_forming_moves['B']
end
# def test_vertical_BBBOBW
# board = rotate new_board.tap { |b| b[0] = %w[W O O O O B B O B W].reverse }, 3
# solver = Solver.new board
# assert_equal [[0, 7]].map(&:reverse), solver.line_forming_moves['B']
# end
#
# def test_vertical_BBBBOW
# board = rotate new_board.tap { |b| b[0] = %w[W O O O O B B B O W].reverse }, 3
# solver = Solver.new board
# assert_equal [[0, 8]].map(&:reverse), solver.line_forming_moves['B']
# end
def test_vertical_BOBBB
board = rotate new_board.tap { |b| b[1] = %w[O O O B O B B B O O].reverse }, 3
solver = Solver.new board
assert_equal [[1, 4]].map(&:reverse), solver.line_forming_moves['B']
end
def test_vertical_BBOBB
board = rotate new_board.tap { |b| b[1] = %w[O O O B B O B B O O].reverse }, 3
solver = Solver.new board
assert_equal [[1, 5]].map(&:reverse), solver.line_forming_moves['B']
end
def test_vertical_BBBOB
board = rotate new_board.tap { |b| b[1] = %w[O O O B B B O B O O].reverse }, 3
solver = Solver.new board
assert_equal [[1, 6]].map(&:reverse), solver.line_forming_moves['B']
end
def test_vertical_WOBBBBBO_empty
skip
board = rotate new_board.tap { |b| b[0] = %w[W O B B B B B O O W].reverse }, 3
solver = Solver.new board
assert_equal [[]], solver.line_forming_moves['B']
end
end