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):
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.
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.
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
"""
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.
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):