-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hills.rb
175 lines (131 loc) · 2.73 KB
/
Hills.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
class Hill
COUNTER_LIMIT = 5
attr_accessor :active
attr_reader :owner
def initialize owner
$logger.info "initialized hill owner #{ owner}"
@owner = owner
@active = true
@counter = 0
end
def set_dead
$logger.info "Hill died"
@owner = -1
end
def should_raze?
# Skip self and dead hills
unless @owner != 0 and @owner != -1
false
else
if @counter <= 0
$logger.info { "Resetting hill counter owner #{ @owner }" }
@counter = COUNTER_LIMIT
true
else
@counter -= 1
false
end
end
end
end
class Hills
def initialize
@list = {}
end
def start_turn
@list.each_value {|v| v.active = false }
end
#
# Add new hill coord
#
# Return: true if added, false if already present
#
def add owner, coord
key = coord[0].to_s + "_" + coord[1].to_s
if @list[key].nil?
$logger.info { "Adding hill at #{ key }." }
@list[key] = Hill.new owner
true
else
$logger.info { "hill at #{ key } already present." }
@list[key].active = true
false
end
end
def active? coord
key = coord[0].to_s + "_" + coord[1].to_s
unless @list[key].nil?
@list[key].active
else
nil
end
end
#
# Declare a hill as dead.
#
# It is not removed, because it could possible reappear in the input.
# instead the owner is set to -1.
#
def remove coord
key = coord[0].to_s + "_" + coord[1].to_s
if @list[key].nil?
$logger.info { "Hill at #{ key } not present, can't remove." }
else
$logger.info { "Removing hill on #{ key } from list" }
@list[key].set_dead
end
end
def my_hill? coord
key = coord[0].to_s + "_" + coord[1].to_s
if @list[key].nil?
false
else
@list[key].owner == 0
end
end
def hill? square
key = square.row.to_s + "_" + square.col.to_s
not @list[key].nil?
end
def key_to_coord key
coord = key.split "_"
coord[0] = coord[0].to_i
coord[1] = coord[1].to_i
coord
end
def each_enemy
@list.clone.each_pair do |key, item|
next unless item.should_raze?
owner = item.owner
$logger.info { "hill owner #{ owner }" }
yield owner, key_to_coord( key )
end
end
def my_hill_region? region
@list.clone.each_pair do |key, item|
owner = item.owner
# Don't skip dead hills
next if owner != 0
sq = Square.coord_to_square( key_to_coord( key ) )
return true if sq.region == region
end
false
end
def each_friend
@list.clone.each_pair do |key, item|
owner = item.owner
# Skip enemies and dead hills
next if owner == -1
next if owner != 0
yield Square.coord_to_square( key_to_coord( key ) )
end
end
def each_pair
# Adding clone allows to change the @hills
# within the called block
@list.clone.each_pair do |key, item|
owner = item.owner
yield key, owner
end
end
end