Use codecs.open to pull the README text into setup.py's long_description.
[pyrisk.git] / pyrisk / base.py
index 1cdd30f155f143560ee7f7183f93b5f91f238214..8229f6080e0e9f40519cc93d7d048f7e54972464 100644 (file)
@@ -19,7 +19,9 @@
 
 import random
 
-from .log import Logger
+from .log import Logger, BeginGame, EndGame, Killed, StartTurn, DealtCards, \
+    EarnsArmies, SelectTerritory, PlaceArmies, PlayCards, Attack, Conquer, \
+    Fortify
 
 
 class PlayerError (Exception):
@@ -74,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.
@@ -399,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.
 
@@ -422,7 +420,7 @@ class Player (NameMixin, ID_CmpMixin):
         draw - another notification-only method
         """
         print 'Reporting for %s:\n  %s' \
-            % (self, '\n  '.join(log[self._message_index:]))
+            % (self, '\n  '.join([str(e) for e in log[self._message_index:]]))
         self._message_index = len(log)
     def draw(self, world, log, cards=[]):
         """Only called if you earned a new card (or cards).
@@ -452,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
@@ -461,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.
@@ -510,7 +523,7 @@ class Engine (ID_CmpMixin):
         for p in self.players:
             p.alive = True
         random.shuffle(self.players)
-        self.log('Game started with %s' % self.players)
+        self.log(BeginGame(self.players))
         self.deck.shuffle()
         self.select_territories()
         self.place_initial_armies()
@@ -535,15 +548,13 @@ class Engine (ID_CmpMixin):
 
         Currently just a notification hook.
         """
-        self.log('Game over.')
+        self.log(EndGame(self.players))
         for p in self.players:
             p.report(self.world, self.log)
     def play_turn(self, player):
         """Work through the phases of player's turn.
         """
-        self.log("%s's turn (territory score: %s)"
-                 % (player, [(p,len(list(p.territories(self.world))))
-                             for p in self.players]))
+        self.log(StartTurn(player, self.players, self.world))
         self.play_cards_and_place_armies(player)
         captures = self.attack_and_fortify(player)
         self.end_of_turn_cards(player, captures)
@@ -567,7 +578,7 @@ class Engine (ID_CmpMixin):
                     break
                 except PlayerError, error:
                     continue
-            self.log('%s selects %s' % (p, t))
+            self.log(SelectTerritory(p, t))
             t.player = p
             t.armies = 1
         # last player has no choice.
@@ -611,7 +622,7 @@ class Engine (ID_CmpMixin):
                 break
             except PlayerError, error:
                 continue
-        self.log('%s places %s' % (player, placements))
+        self.log(PlaceArmies(player, placements))
         for terr_name,armies in placements.items():
             t = self.world.territory_by_name(terr_name)
             t.armies += armies
@@ -621,7 +632,7 @@ class Engine (ID_CmpMixin):
             cards.append(self.deck.pop())
         player.hand.extend(cards)
         player.draw(self.world, self.log, cards)
-        self.log('%s dealt %d cards' % (player, number))
+        self.log(DealtCards(player, number, len(self.deck)))
     def play_cards_and_place_armies(self, player, additional_armies=0):
         cards_required = len(player.hand) >= 5
         error = None
@@ -637,21 +648,17 @@ class Engine (ID_CmpMixin):
             except PlayerError, error:
                 continue
         w_prod,w_terr_prod = self.world.production(player)
-        self.log('%s earned %d armies from territories' % (player, w_prod))
-        if c_prod > 0:
-            self.log('%s played %s, earning %d armies'
-                     % (player, cards, c_prod+sum(c_terr_prod.values())))
+        self.log(EarnsArmies(player, w_prod, w_terr_prod))
         if cards != None:
             for c in cards:
                 player.hand.remove(c)
+            self.log(PlayCards(player, cards, c_prod, c_terr_prod))
         for terr,prod in c_terr_prod.items():
             if terr in w_terr_prod:
                 w_terr_prod[terr] += prod
             else:
                 w_terr_prod[terr] = prod
         self.world.place_territory_production(w_terr_prod)
-        if len(w_terr_prod) > 0:
-            self.log('%s was required to place %s' % (player, w_terr_prod))
         armies = w_prod + c_prod
         self.player_place_armies(player, armies, armies)
     def attack_and_fortify(self, player):
@@ -678,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)
@@ -691,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):
@@ -703,29 +708,28 @@ 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))
-        a_dice = sorted([random.randint(1, 6) for i in range(armies)],
+        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))],
                         reverse=True)
-        a_dead = 0
+        s_dead = 0
         t_dead = 0
-        for a,d in zip(a_dice, t_dice):
+        for a,d in zip(s_dice, t_dice):
             if d >= a:
-                a_dead += 1
+                s_dead += 1
             else:
                 t_dead += 1
-        source.armies -= a_dead
+        source.armies -= s_dead
         target.armies -= t_dead
         if target.armies == 0:
-            self.takeover(source, target, remaining_attackers=armies-a_dead)
-            self.log('%s conquered %s from %s with %d:%d.  Deaths %d:%d.  Remaining %d:%d'
-                     % (source.player, target, source, armies, len(t_dice),
-                        a_dead, t_dead, source.armies, target.armies))
+            self.takeover(source, target, remaining_attackers=armies-s_dead)
+            self.log(Conquer(source, target, s_dice, t_dice, s_dead, t_dead))
             assert target.armies > 0, target
             return True
-        self.log('%s attacked %s from %s with %d:%d.  Deaths %d:%d.  Remaining %d:%d' \
-                     % (source.player, target, source, armies, len(t_dice),
-                        a_dead, t_dead, source.armies, target.armies))
+        self.log(Attack(source, target, s_dice, t_dice, s_dead, t_dead))
         assert target.armies > 0, target
         return False
     def takeover(self, source, target, remaining_attackers):
@@ -747,13 +751,28 @@ 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)
         if len(self.living_players()) > 1:
             while len(killer.hand) > 5:
                 self.play_cards_and_place_armies(killer)
-        self.log('%s killed by %s' % (player, killer))
+        self.log(Killed(player, killer))
         if len(self.living_players()) > 1:
             player.report(self.world, self.log)
             # else the game is over, and killed will hear about this then.