Advertisement
Guest User

tenshi

a guest
Sep 12th, 2010
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3.  
  4. """
  5. // The DoTurn function is where your code goes. The PlanetWars object contains
  6. // the state of the game, including information about all planets and fleets
  7. // that currently exist. Inside this function, you issue orders using the
  8. // pw.IssueOrder() function. For example, to send 10 ships from planet 3 to
  9. // planet 8, you would say pw.IssueOrder(3, 8, 10).
  10. //
  11. // There is already a basic strategy in place here. You can use it as a
  12. // starting point, or you can throw it out entirely and replace it with your
  13. // own. Check out the tutorials and articles on the contest website at
  14. // http://www.ai-contest.com/resources.
  15. """
  16.  
  17. from PlanetWars import PlanetWars
  18.  
  19. def DoTurn(pw):
  20.   if not len( pw.MyPlanets() ):
  21.     return
  22.  
  23.   for planet in pw.Planets():
  24.     planet.NumShipz( planet.NumShips() )
  25.     planet.Ownerz( planet.Owner() )
  26.  
  27.   for fleet in pw.Fleets():
  28.     target= pw.GetPlanet( fleet.DestinationPlanet() )
  29.     if fleet.Owner() == target.Ownerz():
  30.       target.AddShipz( fleet.NumShips() )
  31.     else:
  32.       target.RemoveShipz( fleet.NumShips() )
  33.       if target.NumShipz() < 0:
  34.         target.NumShipz( -target.NumShipz() )
  35.         target.Ownerz( fleet.Owner() )
  36.  
  37.   source= None
  38.   for planet in pw.MyPlanets():
  39.     if( planer.Owner() == 1 )and( planet.Ownerz() == 1 ):
  40.       if ( not source )or( planet.NumShipz() > source.NumShipz() ):
  41.         source= planet
  42.  
  43.   target= None
  44.   for planet in sorted( pw.Planets(), key= lambda planet:  pw.Distance( source, planet ) ):
  45.     if planet.NumShipz() * 3 < source.NumShipz():
  46.       if ( not target )or( planet.GrowthRate() > target.GrowthRate() ):
  47.         target= planet
  48.  
  49.   if source and target and ( source != target ):
  50.     num_ships = min( source.NumShips() , source.NumShipz() ) / 3
  51. #    if target.Owner() != 1:
  52. #      num_ships*= 2
  53.     if num_ships > 0:
  54.       pw.IssueOrder( source.PlanetID(), target.PlanetID(), num_ships )
  55.  
  56.  
  57. def main():
  58.   map_data = ''
  59.   while(True):
  60.     current_line = raw_input()
  61.     if len(current_line) >= 2 and current_line.startswith("go"):
  62.       pw = PlanetWars(map_data)
  63.       DoTurn(pw)
  64.       pw.FinishTurn()
  65.       map_data = ''
  66.     else:
  67.       map_data += current_line + '\n'
  68.  
  69.  
  70. if __name__ == '__main__':
  71.   try:
  72.     import psyco
  73.     psyco.full()
  74.   except ImportError:
  75.     pass
  76.   try:
  77.     main()
  78.   except KeyboardInterrupt:
  79.     print 'ctrl-c, leaving ...'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement