Use codecs.open to pull the README text into setup.py's long_description.
[pyrisk.git] / pyrisk / base.py
index 38d3b2345810d91dd307d109903ed54e8a26dcec..8229f6080e0e9f40519cc93d7d048f7e54972464 100644 (file)
@@ -20,7 +20,8 @@
 import random
 
 from .log import Logger, BeginGame, EndGame, Killed, StartTurn, DealtCards, \
-    EarnsArmies, SelectTerritory, PlaceArmies, PlayCards, Attack, Conquer
+    EarnsArmies, SelectTerritory, PlaceArmies, PlayCards, Attack, Conquer, \
+    Fortify
 
 
 class PlayerError (Exception):
@@ -75,6 +76,11 @@ class Territory (NameMixin, ID_CmpMixin, list):
             if id(t) == id(other):
                 return True
         return False
+    def border(self):
+        for t in self:
+            if t.player != self.player:
+                return True
+        return False
 
 class Continent (NameMixin, ID_CmpMixin, list):
     """A group of Territories.
@@ -400,15 +406,6 @@ class Player (NameMixin, ID_CmpMixin):
         for t in world.territories():
             if t.player == self:
                 yield t
-    def border_territories(self, world):
-        """Iterate through all territories owned by this player which
-        border another player's territories.
-        """
-        for t in self.territories(world):
-            for neighbor in t:
-                if neighbor.player != self:
-                    yield t
-                    break
     def report(self, world, log):
         """Send reports about death and game endings.
 
@@ -453,8 +450,9 @@ class Player (NameMixin, ID_CmpMixin):
 
         Return {territory_name: num_armies, ...}
         """
-        t = random.sample(list(self.border_territories(world)), 1)[0]
-        return {t.name: this_round}
+        terr = random.sample([t for t in self.territories(world)
+                              if t.border()], 1)[0]
+        return {terr.name: this_round}
     def attack_and_fortify(self, world, log, error=None,
                            mode='attack'):
         """Return list of (source, target, armies) tuples.  Place None
@@ -462,15 +460,29 @@ class Player (NameMixin, ID_CmpMixin):
         """
         assert mode != 'fortify', mode
         possible_attacks = []
-        for t in self.border_territories(world):
+        for t in self.territories(world):
+            if not t.border():
+                continue
             if t.armies <= 3: #1: # be more conservative, only attack with 3 dice
                 continue
             targets = [border_t for border_t in t if border_t.player != self]
             for tg in targets:
-                possible_attacks.append((t.name, tg.name, min(3, t.armies-1)))
+                possible_attacks.append(
+                    (t.name, tg.name, min(3, t.armies-1)))
         if len(possible_attacks) == 0:
-            return [None, None] # stop attack phase, then stop fortification phase
-        return random.sample(possible_attacks, 1) # + [None]
+            fortifications = []
+            for t in self.territories(world):
+                if t.border() or t.armies == 1:
+                    continue
+                targets = list(t)
+                for tg in targets:
+                    fortifications.append(
+                        (t.name, tg.name, t.armies-1))
+            if len(fortifications) > 1:
+                fortifications = random.sample(fortifications, 1)
+            # stop attack phase, fortify, stop fortification phase
+            return [None] + fortifications + [None]
+        return random.sample(possible_attacks, 1)
     def support_attack(self, world, log, error,
                        source, target):
         """Follow up on a conquest by moving additional armies.
@@ -673,9 +685,6 @@ class Engine (ID_CmpMixin):
                         target = self.world.territory_by_name(target_name)
                     except KeyError:
                         raise PlayerError('Invalid territory "%s"' % targer_name)
-                    if not source.borders(target):
-                        raise PlayerError('Cannot reach %s from %s to %s'
-                                          % (target, source, mode))
                     if mode == 'attack':
                         tplayer = target.player
                         capture = self.attack(source, target, armies)
@@ -686,6 +695,7 @@ class Engine (ID_CmpMixin):
                     else:
                         assert mode == 'fortify', mode
                         self.fortify(source, target, armies)
+                        return captures # only allow one fortification
             except PlayerError, error:
                 continue
     def attack(self, source, target, armies):
@@ -698,6 +708,9 @@ class Engine (ID_CmpMixin):
         if armies >= source.armies:
             raise PlayerError('%s attacking %s with %d armies, but only %d are available.'
                               % (source, target, armies, source.armies-1))
+        if not source.borders(target):
+            raise PlayerError('Cannot reach %s from %s to attack'
+                              % (target, source))
         s_dice = sorted([random.randint(1, 6) for i in range(armies)],
                         reverse=True)
         t_dice = sorted([random.randint(1, 6) for i in range(min(2, target.armies))],
@@ -738,6 +751,21 @@ class Engine (ID_CmpMixin):
                     continue
             source.armies -= support
             target.armies += support
+    def fortify(self, source, target, armies):
+        if source.player != target.player:
+            raise PlayerError('%s (%s) cannot fortifiy %s (%s).'
+                              % (source, source.player, target, target.player))
+        if armies == 0:
+            return
+        if armies >= source.armies:
+            raise PlayerError('%s fortifying %s with %d armies, but only %d are available.'
+                              % (source, target, armies, source.armies-1))
+        if not source.borders(target):
+            raise PlayerError('Cannot reach %s from %s to fortify'
+                              % (target, source))
+        source.armies -= armies
+        target.armies += armies
+        self.log(Fortify(source, target, armies))
     def player_killed(self, player, killer):
         player.alive = False
         killer.hand.extend(player.hand)