/
homo_habilis.rb
351 lines (274 loc) · 8.52 KB
/
homo_habilis.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
require "rubygems"
require "ruby-debug"
class HomoHabilis
attr_reader :board
def name
"Homo Habilis"
end
def new_game
@board = Board.new
[2, 3, 3, 4, 5].inject([]) { |placed, length| placed.push(Placement.for_length_and_context(length, placed)) }
end
def take_turn(state, ships_remaining)
board.push_state(state, ships_remaining)
board.next_move
end
module Placement
def self.for_length_and_context(length, context)
direction = [:across, :down].sample
generate_coords(length, direction, context) + [length, direction]
end
private
def self.generate_coords(length, direction, context)
try = [rand(10-length), rand(10)]
try.reverse! if direction == :down
succcessful_attempt?(try, length, direction, context) ? try : generate_coords(length, direction, context)
end
def self.succcessful_attempt?(try, length, direction, context)
context.all? { |placement|
(expand(*try, length, direction) & expand(*placement)).empty?
}
end
def self.expand(x, y, length, direction)
xy = "#{x}#{y}".to_i
multiplier = direction == :down ? 1 : 10
coords = (0...length).map{ |i| xy + i * multiplier }
end
end#Placement
class Move
attr_reader :x, :y, :edges
def self.find(x, y)
self.new(x, y)
end
def self.new(x,y)
@identity_map ||= {}
new_key = "#{x}#{y}"
@identity_map[new_key] = super(x,y) unless @identity_map.has_key?(new_key)
@identity_map[new_key]
end
def self.clear_identity_map!
@identity_map = nil
end
def self.board=(board)
@board = board
end
def self.board
@board
end
def initialize(x,y)
@x = x
@y = y
end
def to_s
inspect
end
def inspect
"#<HomoGeorgicus::Move:#{self.object_id} coords=[#{x},#{y}] base_probability=#{base_probability} accumulated_probability=#{accumulated_probability}>"
end
def ==(other)
x == other.x && y == other.y
end
def to_a
[x,y]
end
def value
self.class.board.value_at(x, y)
end
def sunk!
@sunk = true
end
def sunk?
@sunk ||= false
end
def floating?
!sunk?
end
def start_sinking!(ship_lengths=[])
ship_lengths.sort.each do |length|
up = select(:type => [:hit?, :floating?], :axis => :vertical, :direction => :up)
down = select(:type => [:hit?, :floating?], :axis => :vertical, :direction => :down)
left = select(:type => [:hit?, :floating?], :axis => :horizontal, :direction => :left)
right = select(:type => [:hit?, :floating?], :axis => :horizontal, :direction => :right)
following_y = up + [self] + down
following_x = left + [self] + right
if following_x.count >= length
if following_x.count == length
following_x.map(&:sunk!)
else
if up.empty?
down[0, length].map(&:sunk!)
elsif down.empty?
up[0, length].map(&:sunk!)
end
end
elsif following_y.count >= length
if following_y.count == length
following_y.map(&:sunk!)
else
if left.empty?
right[0, length].map(&:sunk!)
elsif right.empty?
left[0, length].map(&:sunk!)
end
end
else
puts "Sinking edge case"
end
end#each
end#start_sinking!
def select(opts={})
type = opts[:type] || []
type = type.is_a?(Array) ? type : [type]
axis = opts[:axis]
direction = opts[:direction] || :both
selected = []
if :both == direction
directions = axis == :horizontal ? [:left, :right] : [:up, :down]
selected << directions.collect { |d| do_select(type, axis, d) }
else
selected << do_select(type, axis, direction)
end
selected.flatten.compact
end
def unknown?
value == :unknown
end
def hit?
value == :hit
end
def miss?
value == :miss
end
def made?
!unknown?
end
def probability
return 0 if sunk?
base_probability + accumulated_probability
end
def accumulated_probability
return 0 if self.class.board.state.nil? || neighbours.all?(&:unknown?)
acc_left + acc_right + acc_up + acc_down
end
def acc_left
return 0 if 0 == x
left = Move.find(x-1, y)
(left.hit? && left.floating?) ? (5 + left.acc_left) : 0
end
def acc_right
return 0 if 9 == x
right = Move.find(x+1, y)
(right.hit? && right.floating?) ? (5 + right.acc_right) : 0
end
def acc_up
return 0 if 0 == y
up = Move.find(x, y-1)
(up.hit? && up.floating?) ? (5 + up.acc_up) : 0
end
def acc_down
return 0 if 9 == y
down = Move.find(x, y+1)
(down.hit? && down.floating?) ? (5 + down.acc_down) : 0
end
def base_probability
left = select(:type => :unknown?, :axis => :horizontal, :direction => :left).length
right = select(:type => :unknown?, :axis => :horizontal, :direction => :right).length
up = select(:type => :unknown?, :axis => :vertical, :direction => :up).length
down = select(:type => :unknown?, :axis => :vertical, :direction => :down).length
smallest_boat = self.class.board.smallest_remaining_boat_length
highly_productive = (left >= smallest_boat && right >= smallest_boat) ? 2 :0
highly_productive += (up >= smallest_boat && down >= smallest_boat) ? 2 :0
kind_productive = [left, right].inject(&:+) >= smallest_boat ? 1 : 0
kind_productive += [up, down].inject(&:+) >= smallest_boat ? 1 : 0
base_probability = self.class.board.sunk_any? ? 1 : (x.even? == y.even?) ? 2 : 1
base_probability = (base_probability * (1 + (highly_productive / 5.0))) + (base_probability * (1 + (kind_productive / 5.0)))
base_probability
end
def neighbours
calculate_y_neighbours + calculate_x_neighbours
end
private
def do_select(type, axis, direction)
selected = []
coords = if axis == :vertical
direction == :up ? [x, y-1] : [x, y+1]
else
direction == :left ? [x-1, y] : [x+1, y]
end
unless coords[0] < 0 || coords[0] > 9 || coords[1] < 0 || coords[1] > 9
move = Move.find(*coords)
if type.all? { |t| move.send(t) } || type.empty?
selected = [move] | move.select(:type => type, :axis => axis, :direction => direction)
end
end
selected
end
def calculate_y_neighbours
moves = []
moves << Move.find(x, y+1) if y+1 < 10
moves << Move.find(x, y-1) if y-1 >= 0
moves
end
def calculate_x_neighbours
moves = []
moves << Move.find(x+1, y) if x+1 < 10
moves << Move.find(x-1, y) if x-1 >= 0
moves
end
end#Move
class Board
attr_reader :state, :ships_remaining, :moves, :edges, :moves_made
def initialize
@state = nil
@ships_remaining = []
@moves = []
@edges = []
@moves_made = []
Move.board = self
Move.clear_identity_map!
0.upto(9) { |x| 0.upto(9) { |y| moves << Move.new(x,y) } }
end
def push_state(state, ships_remaining)
@state = state
@ships_remaining << ships_remaining
last_move.start_sinking!(sunk) if sunk_ship?
end
def value_at(x, y)
state.nil? ? nil : state[y][x]
end
def next_move
moves_made << get_move
last_move.to_a
end
def last_move
moves_made.last
end
def sunk_ship?
!sunk.empty?
end
def sunk_any?
ships_remaining.empty? ? false : ships_remaining[-1].count < 5
end
def sunk
sunk = []
if ships_remaining.count > 2
sunk = ships_remaining[-2] - ships_remaining[-1]
# [3,3] - [3] == [] .. in battle ships [3,3] - [3] == [3]
sunk << 3 if (ships_remaining[-2].count - ships_remaining[-1].count) != sunk.count
end
sunk
end
def average_remaining_boat_length
ships_remaining[-1].inject(&:+) / ships_remaining[-1].length
end
def smallest_remaining_boat_length
ships_remaining[-1].min
end
private
def get_move
possible_moves = moves.find_all(&:unknown?)
possible_moves = possible_moves.sort { |a,b| a.probability <=> b.probability }
possible_moves.last
end
end
end