-
Notifications
You must be signed in to change notification settings - Fork 0
/
zFoam2.rb
85 lines (61 loc) · 1.26 KB
/
zFoam2.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
$:.unshift File.dirname($0)
require 'AI.rb'
require 'BaseStrategy'
#
# main routine
#
$logger.log = false
$logger.log_status = false
strategy = BaseStrategy.new
def move_away list
ant = list[-1]
return if ant.moved?
dirs = [ :N, :E, :S, :W ].sort_by! { rand }
if ant.stuck?
$logger.info "#{ ant } stuck!"
# Signal other ants to move away
dirs.each do |dir|
ant2 = ant.square.neighbor( dir ).ant
if ant2 and
ant2.mine? and
not ant2.moved? and
not list.include? ant2
move_away list + [ ant2 ]
end
end
end
if not ant.stuck?
sq = ant.square
dirs.each do |dir|
if sq.neighbor(dir).passable?
$logger.info "Moving #{ ant} to #{ dir }"
ant.move dir
break
end
end
return
end
end
def num_neighbors ant
count = 0
[ :N, :E, :S, :W ].each do |dir|
count += 1 if ant.square.neighbor(dir).ant?
end
count
end
$ai.run do |ai|
strategy.turn ai
# Attempt to move away if too many neighbors
ai.my_ants.each do |ant|
next if ant.moved?
if num_neighbors( ant) > 0
move_away [ ant]
end
end
# Attempt to move off the hill if there
ai.hills.each_friend do |sq|
if sq.ant? and sq.ant.mine? and not sq.ant.moved?
move_away [ sq.ant]
end
end
end