Permalink
Browse files

Added in_future(turns) for a planet.

  • Loading branch information...
simu committed Sep 10, 2010
1 parent a0d131a commit 1cef98e23a611d9fb524b50450a82bfbba5fd9c9
Showing with 46 additions and 0 deletions.
  1. +46 −0 planetwars/planet.py
View
@@ -1,6 +1,8 @@
+# vim:ts=4:shiftwidth=4:et
from planetwars.player import PLAYER_MAP
from planetwars.util import Point, TypedSetBase
from math import ceil, sqrt
+from copy import copy
import player
class Planet(object):
@@ -50,5 +52,49 @@ def send_fleet(self, target, ship_count):
if self.ship_count >= ship_count:
self.universe.send_fleet(self, target, ship_count)
+ def in_future(self, turns=1):
+ """Calculates state of planet in `turns' turns."""
+ planet = copy(self)
+
+ arriving_fleets = self.universe.find_fleets(destination=self)
+
+ for i in range(1, turns+1):
+ # account planet growth
+ if planet.owner != player.NOBODY:
+ planet.ship_count = planet.ship_count + self.growth_rate
+
+ # get fleets which will arrive in that turn
+ fleets = [ x for x in arriving_fleets if x.turns_remaining == i ]
+
+ # assuming 2-player scenario!
+ ships = []
+ for id in [1,2]:
+ count = sum( [ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ] )
+ if PLAYER_MAP[id] == planet.owner:
+ count += planet.ship_count
+
+ if count > 0:
+ ships.append({'player':PLAYER_MAP.get(id), 'ships':count})
+
+ # neutral planet has own fleet
+ if planet.owner == player.NOBODY:
+ ships.append({'player':player.NOBODY,'ships':planet.ship_count})
+
+ # calculate outcome
+ if len(ships) > 1:
+ s = sorted(ships, key=lambda s : s['ships'], reverse=True)
+
+ winner = s[0]
+ second = s[1]
+
+ if winner['ships'] == second['ships']:
+ planet.owner=player.NOBODY
+ planet.ship_count=0
+ else:
+ planet.owner=winner['player']
+ planet.ship_count=winner['ships'] - second['ships']
+
+ return planet
+
class Planets(TypedSetBase):
accepts = (Planet, )

0 comments on commit 1cef98e

Please sign in to comment.