-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlanetWars.py
298 lines (246 loc) · 7.93 KB
/
PlanetWars.py
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
#!/usr/bin/env python
#
import re
from math import ceil, sqrt
from sys import stdout
class Message:
def __init__(self, nickname, number):
self._nickname = nickname
self._number = number
def Nickname(self):
return self._nickname
def Number(self):
return self._number
class Fleet:
def __init__(self, owner, num_ships, source_planet, destination_planet, \
total_trip_length, turns_remaining):
self._owner = owner
self._num_ships = num_ships
self._source_planet = source_planet
self._destination_planet = destination_planet
self._total_trip_length = total_trip_length
self._turns_remaining = turns_remaining
def Owner(self):
return self._owner
def NumShips(self):
return self._num_ships
def SourcePlanet(self):
return self._source_planet
def DestinationPlanet(self):
return self._destination_planet
def TotalTripLength(self):
return self._total_trip_length
def TurnsRemaining(self):
return self._turns_remaining
def __str__(self):
message = "F " + str(self._owner) + " " + str(self._num_ships) + " " + \
str(self._source_planet) + " " + str(self._destination_planet) + " " + \
str(self._total_trip_length) + " " + str(self._turns_remaining)
return message.replace(".0 ", " ")
def __repr__(self):
message = "F " + str(self._owner) + " " + str(self._num_ships) + " " + \
str(self._source_planet) + " " + str(self._destination_planet) + " " + \
str(int(self._total_trip_length)) + " " + str(int(self._turns_remaining))
return message.replace(".0 ", " ")
class Planet:
def __init__(self, planet_id, owner, num_ships, growth_rate, x, y):
self._planet_id = planet_id
self._owner = owner
self._num_ships = num_ships
self._growth_rate = growth_rate
self._x = x
self._y = y
def PlanetID(self):
return self._planet_id
def Owner(self, new_owner=None):
if new_owner == None:
return self._owner
self._owner = new_owner
def NumShips(self, new_num_ships=None):
if new_num_ships == None:
return self._num_ships
self._num_ships = new_num_ships
def GrowthRate(self):
return self._growth_rate
def X(self):
return self._x
def Y(self):
return self._y
def AddShips(self, amount):
self._num_ships += amount
def RemoveShips(self, amount):
self._num_ships -= amount
def __str__(self):
message = "P " + str(self._x) + " " + str(self._y) + " " + str(self._owner) + \
" " + str(int(self._num_ships)) + " " + str(int(self._growth_rate))
return message.replace(".0 ", " ")
def __repr__(self):
# return 'MyClass #%d' % (self.id,)
message = "P " + str(self._x) + " " + str(self._y) + " " + str(self._owner) + \
" " + str(int(self._num_ships)) + " " + str(int(self._growth_rate))
return message.replace(".0 ", " ")
class PlanetWars:
def __init__(self, gameState):
self._planets = []
self._fleets = []
self._messages = []
self.ParseGameState(gameState)
def NumPlanets(self):
return len(self._planets)
def GetPlanet(self, planet_id):
return self._planets[planet_id]
def NumFleets(self):
return len(self._fleets)
def GetFleet(self, fleet_id):
return self._fleets[fleet_id]
def Planets(self):
return self._planets
def Messages(self):
return self._messages
def MyPlanets(self):
r = []
for p in self._planets:
if p.Owner() != 1:
continue
r.append(p)
return r
def NeutralPlanets(self):
r = []
for p in self._planets:
if p.Owner() != 0:
continue
r.append(p)
return r
def EnemyPlanets(self):
r = []
for p in self._planets:
if p.Owner() <= 1:
continue
r.append(p)
return r
def NotMyPlanets(self):
r = []
for p in self._planets:
if p.Owner() == 1:
continue
r.append(p)
return r
def Fleets(self):
return self._fleets
def MyFleets(self):
r = []
for f in self._fleets:
if f.Owner() != 1:
continue
r.append(f)
return r
def EnemyFleets(self):
r = []
for f in self._fleets:
if f.Owner() <= 1:
continue
r.append(f)
return r
# Returns the number of ships that the current player has, either located
# on planets or in flight.
def NumShips(self, playerID):
numShips = 0
for p in self._planets:
if p.Owner() == playerID:
numShips += p.NumShips()
for f in self._fleets:
if f.Owner() == playerID:
numShips += f.NumShips()
return int(numShips)
# Returns the number of income ships that the current player has
def Production(self, playerID):
numShips = 0
for p in self._planets:
if p.Owner() == playerID:
numShips += p.GrowthRate()
return int(numShips)
def ToString(self):
s = ''
for p in self._planets:
s += "P %f %f %d %d %d\n" % \
(p.X(), p.Y(), p.Owner(), p.NumShips(), p.GrowthRate())
for f in self._fleets:
s += "F %d %d %d %d %d %d\n" % \
(f.Owner(), f.NumShips(), f.SourcePlanet(), f.DestinationPlanet(), \
f.TotalTripLength(), f.TurnsRemaining())
return s
def Distance(self, source_planet, destination_planet):
source = self._planets[source_planet]
destination = self._planets[destination_planet]
dx = source.X() - destination.X()
dy = source.Y() - destination.Y()
return int(ceil(sqrt(dx * dx + dy * dy)))
def IssueOrder(self, source_planet, destination_planet, num_ships):
stdout.write("%d %d %d\n" % \
(source_planet, destination_planet, num_ships))
stdout.flush()
def IsAlive(self, player_id):
for p in self._planets:
if p.Owner() == player_id:
return True
for f in self._fleets:
if f.Owner() == player_id:
return True
return False
def ParseGameState(self, s):
self._planets = []
self._fleets = []
self._messages = []
lines = s.split("\n")
planet_id = 0
for line in lines:
line = line.split("#")[0] # remove comments
tokens = line.split(" ")
if len(tokens) == 1:
continue
if tokens[0] == "P":
# if len(tokens) != 6: # for prev map format
if len(tokens) != 7:
return 0
# p = Planet(planet_id, # The ID of this planet
# int(tokens[3]), # Owner
# int(tokens[4]), # Num ships
# int(tokens[5]), # Growth rate
# float(tokens[1]), # X
# float(tokens[2])) # Y
#
# updated map format
p = Planet(int(tokens[1])-1, # The ID of this planet
int(tokens[5]), # Owner
int(tokens[6]), # Num ships
int(tokens[4]), # Growth rate
float(tokens[2]), # X
float(tokens[3])) # Y
planet_id += 1
self._planets.append(p)
elif tokens[0] == "F":
if len(tokens) != 7:
return 0
f = Fleet(int(tokens[1]), # Owner
int(tokens[2]), # Num ships
int(tokens[3]), # Source
int(tokens[4]), # Destination
int(tokens[5]), # Total trip length
int(tokens[6])) # Turns remaining
self._fleets.append(f)
# elif re.match( r'^[A-Z][0-9]', tokens[0], re.M):
elif re.match( r'^[A-Za-z]\s[-]{0,1}[0-9]{1,10}$', line, re.M):
if len(tokens) != 2:
return 0
m = Message(str(tokens[0]), # Nickname
int(tokens[1])) # Number
self._messages.append(m)
else:
return 0
return 1
def SendMessage(self, nick, mes):
stdout.write(str(nick) + ' ' + str(mes) + "\n")
stdout.flush()
def FinishTurn(self):
stdout.write(".\n")
stdout.flush()