def execute(self, C, player=None, action=None): # global counterOfLogs if action is not None: # update map loc, = action assert loc._val == 0, 'this should not happen' loc._val = player.val loc.symbol = player.symbol loc.player = player # C.log.write(player, 'places at: {}, {}'.format(*action)) print('log', '{} chooses {}'.format(player, loc)) # C.log.writef('hallo {}',counterOfLogs) #'{} chooses {}', player, loc) # counterOfLogs += 1 C.log.writef('{} chooses {}', player, loc) # check for victory winner = C.state.board.check() if winner != 0: C.state.winner = winner raise GameOver raise SwitchPhase('tic', stack=False)
def execute(self, C, player=None, action=None): if action is None: # self.neutrals = tset(C.deck.draw(C.config.rules.market_cards)) self.num = len(self.market[self.player]) # del self.sel[self.player] C.log.writef('{} may take {} action{}', self.player, self.num, 's' if self.num > 1 else '') return assert player == self.player, 'wrong player: {}'.format(player) obj, *other = action if 'trade' in self: pass else: self.num -= 1 pass if self.num == 0: nxt = get_next_market(self.sel) if nxt is None: raise PhaseComplete else: raise SwitchPhase('market', stack=False, player=nxt, market=self.market[nxt]) raise NotImplementedError
def post(self, C, player, action=None): for player in C.players: income = len(player.buildings.estate) + len(player.buildings.palace) if income > 0: player.money += income C.log.writef('{} earns {} from their buildings', player, util.format_quantity('coin', income)) raise SwitchPhase('jack')
def execute(self, C, player=None, action=None): if 'counter' not in C.state: C.state.counter = 0 C.state.counter += 1 if C.state.counter > 3: C.state.winner = 'Player1' raise GameOver raise SwitchPhase('phase1', stack=False)
def post_phase(self, C, player, action=None): if 'candidates' not in self: self.candidates = tlist( p for p in C.players if satisfies_vic_req(p, C.config.rules.victory_conditions)) if len(self.candidates): raise stg.Switch('claim') raise SwitchPhase('queen')
def post(self, C, player, action=None): for p in C.players: for farm in p.buildings.farm: if farm.harvest is None: card = C.state.deck.draw() card.visible.clear() farm.harvest = card C.log.writef('{} is harvestable on {}\'s {}', card, p, farm) raise SwitchPhase('king')
def pre(self, C): # increment herald for player in C.players: player.order += 1 player.order %= len(C.players) if player.order == 0: C.state.herald = player C.log.writef('{} becomes herald', player) for player in C.players: if len(player.hand) > player.hand_limit: raise SwitchPhase('tax')
def execute(self, C, player=None, action=None): if self.available is None: self.available = tset(C.state.world.corners) if action is not None: loc, = action if loc.obj_type == 'Edge': build(C, 'road', player, loc) self.settled = None self.player_order.pop() if len(self.player_order) == 0: raise SwitchPhase('main', stack=False) if len(self.player_order) == len(C.players): self.on_second = True elif loc.obj_type == 'Corner': build(C, 'settlement', player, loc) self.settled = loc for e in loc.edges: if e is not None: for c in e.corners: self.available.discard(c) if self.on_second: res = tlist() for f in loc.fields: if f is not None and f.res != 'desert': res.append(f.res) for r in res: gain_res(r, C.state.bank, player, 1) if len(res) == 3: s = '{}, {}, and {}'.format(*res) elif len(res) == 2: s = '{} and {}'.format(*res) elif len(res) == 1: s = '{}'.format(*res) else: s = 'no resources' C.log.writef('{} gains: {}', player, s)
def set_road(self, C, player, action=None): if action is None: raise Decide('road') loc, = action assert_(loc.obj_type == 'Edge', f'{loc} should be an edge') build(C, 'road', player, loc) if len(self.player_order) == 0: raise SwitchPhase('main', stack=False) if len(self.player_order) == len(C.players): self.on_second = True raise Switch('settlement')
def execute(self, C, player=None, action=None): if action is not None: # update map loc, = action assert loc._val == 0, 'this should not happen' loc._val = player.val loc.symbol = player.symbol loc.player = player # C.log.write(player, 'places at: {}, {}'.format(*action)) C.log.writef('{} chooses {}', player, loc) # check for victory winner = C.state.board.checkLimited(C.state.limit) if winner != 0: C.state.winner = winner raise GameOver raise SwitchPhase('tic', stack=False)
def execute(self, C, player=None, action=None): if action is None: if self.pre_check == 'check': self.pre_check = get_knight(self.player.devcards) \ if len(self.player.devcards) else None else: # coming back from robber or trade phase return elif action[0] == 'continue': self.pre_check = None if self.roll is None and self.pre_check is None: self.roll = roll_dice(C.RNG) if len(C.state.rolls): self.roll = C.state.rolls.pop() C.log.zindent() C.log.writef('{} rolled: {}.', self.player, self.roll) C.log.iindent() if self.roll == 7: C.stack.push('main') raise SwitchPhase('robber', send_action=False, stack=False, player=self.player) hexes = C.state.numbers[self.roll] for hex in hexes: if hex != C.state.robber.loc: for c in hex.corners: if 'building' in c and c.building.obj_type in C.state.production: gain = C.state.production[c.building.obj_type] gain_res(hex.res, C.state.bank, c.building.player, gain, C.log) return if action is None: return obj, *rest = action if obj == 'pass' or obj == ('pass', ): raise SwitchPhase('main', stack=False) if obj == 'cancel': if self.devcard is not None: if self.devcard.name == 'Road Building': unbuild(C, self.card_info.building) self.devcard = None self.card_info = None return if obj == 'continue': return # trade if obj == 'offer' or obj == 'demand': C.log[self.player].write('You start a trade') C.log.iindent() raise SwitchPhase('trade', send_action=True, stack=True, player=self.player, bank_trades=bank_trade_options( self.player, C.state.bank_trading) if 'maritime' in action.get_type() else None) if self.devcard is not None: if self.devcard.name == 'Road Building': if self.card_info is None: bld = build(C, 'road', self.player, obj) self.card_info = bld else: bld = build(C, 'road', self.player, obj) play_dev(self.player, self.devcard) C.log.writef('{} plays {}, and builds: {} and {}', self.player, self.devcard, self.card_info, bld) self.devcard = None self.card_info = None elif self.devcard.name == 'Year of Plenty': res, = obj gain_res(self.card_info, C.state.bank, self.player, 1, log=C.log) gain_res(res, C.state.bank, player, 1, log=C.log) play_dev(self.player, self.devcard) C.log.writef('{} plays {}, and receives: {} and {}', self.player, self.devcard, self.card_info, res) self.card_info = None self.devcard = None else: raise Exception('unknown dev card: {} {}'.format( self.devcard, self.devcard.name)) obj_type = obj.get_type() if 'build' in action.get_type(): if obj_type == 'settlement': unbuild(C, obj, silent=False) bld = build(C, 'city', self.player, obj.loc) else: bld = build(C, 'settlement' if obj_type == 'Corner' else 'road', self.player, obj) pay_cost(self.player, C.state.costs[bld.get_type()], C.state.bank) elif obj_type == 'devcard': if obj.name == 'Victory Point': raise Exception('Shouldnt have played a Victory point card') elif obj.name == 'Knight': raise SwitchPhase('robber', send_action=False, stack=True, knight=obj, player=self.player) elif obj.name == 'Monopoly': res, = rest for opp in C.players.values(): if opp != self.player and opp.resources[res] > 0: self.player.resources[res] += opp.resources[res] C.log.writef('{} receives {} {} from {}', self.player, opp.resources[res], res, opp) C.log.writef('{} plays {}, claiming all {}', self.player, obj, res) play_dev(self.player, obj) elif obj.name == 'Year of Plenty': res, = rest self.devcard = obj self.card_info = res elif obj.name == 'Road Building': self.devcard = obj else: raise Exception('unknown card: {} {}'.format(obj, obj.name)) elif obj_type == 'devdeck': card = C.state.dev_deck.draw() card.visible = tset([player]) self.player.devcards.add(card) self.bought_devcards.add(card) C.log.writef('{} buys a development card', self.player) msg = '' if card.name == 'Victory Point': msg = ' (gaining 1 victory point)' self.player.vps += 1 C.log[self.player].writef('You got a {}{}', card, msg) pay_cost(self.player, C.state.costs.devcard, C.state.bank) else: raise Exception('Unknown obj {}: {}'.format(type(obj), obj)) if check_victory(C): raise GameOver
def execute(self, C, player=None, action=None): if action is not None: loc, = action C.log.writef('{} chooses {}', player, loc) raise SwitchPhase('ExamplePhase', stack=False)
def execute(self, C, player=None, action=None): if 'pre_complete' not in self: C.log.writef('{} phase begins', self.name.capitalize()) C.log.iindent() self.pre_complete = True self.sel = tdict({p: tset() for p in C.players}) self.active = tset(C.players) # create neutral market num = 5 #C.config.rules.neutral_market[self.name] if num > 0: cards = C.state.deck.draw(num) # draw cards num = 3 if num > 0: C.log.write('Everybody draws {} card{}'.format( num, 's' if num > 1 else '')) for p in C.players: cards = C.state.deck.draw(num, player=player) C.log[p].writef( 'You draw: {}'.format(', '.join(['{}'] * num)), *cards) p.hand.update(cards) if action is not None: obj, = action if 'ready' == obj: self.active.remove(player) if len(self.active) == 0: self.market_complete = True nxt = 1 if nxt is not None: for p, cards in self.sel: for card in cards: p.hand.remove(card) p.market = cards raise SwitchPhase('phase2', player=nxt, market=self.sel) else: C.log.write('No one goes to the market') elif obj in self.sel[player]: C.log[player].iindent() C.log[player].writef('You unselect {}', obj) C.log[player].dindent() self.sel[player].remove(obj) else: C.log[player].iindent() C.log[player].writef('You select {}', obj) C.log[player].dindent() self.sel[player].add(obj) if 'market_complete' in self: self.post_complete = True if 'post_complete' in self: C.state.market.clear() if len(C.stack) == 0: C.stack.extend(C.state.royal_phases) C.log.zindent() raise PhaseComplete
def main_turn(self, C, player, action=None): if action is None: raise stg.Decide('main') # self.null_op(2) # TESTING obj, *rest = action if obj == 'pass': raise SwitchPhase('main') if obj == 'offer' or obj == 'demand': C.log[self.player].write('You start a trade') C.log.iindent() raise SubPhase('trade', send_action=True, player=self.player, bank_trades=bank_trade_options( self.player, C.state.bank_trading) if 'maritime' in action.get_type() else None) obj_type = obj.get_type() if 'build' in action.get_type(): if obj_type == 'settlement': # replace this settlement unbuild(C, obj, silent=False) bld = build(C, 'city', self.player, obj.loc) else: bld = build(C, 'settlement' if obj_type == 'Corner' else 'road', self.player, obj) pay_cost(self.player, C.state.costs[bld.get_type()], C.state.bank) elif obj_type == 'devcard': if obj.name == 'Victory Point': raise Exception('Shouldnt have played a Victory point card') elif obj.name == 'Knight': raise SubPhase('robber', send_action=False, knight=obj, player=self.player) elif obj.name == 'Monopoly': res, = rest for opp in C.players.values(): if opp != self.player and opp.resources[res] > 0: self.player.resources[res] += opp.resources[res] C.log.writef('{} receives {} {} from {}', self.player, opp.resources[res], res, opp) C.log.writef('{} plays {}, claiming all {}', self.player, obj, res) play_dev(self.player, obj) self.devcard = obj elif obj.name == 'Year of Plenty': res, = rest self.devcard = obj raise stg.Switch('year-plenty', res=res) elif obj.name == 'Road Building': self.devcard = obj raise stg.Switch('road-building') else: raise Exception('unknown card: {} {}'.format(obj, obj.name)) elif obj_type == 'devdeck': card = C.state.dev_deck.draw() self.player.devcards.add(card) self.bought_devcards.add(card) C.log.writef('{} buys a development card', self.player) msg = '' if card.name == 'Victory Point': msg = ' (gaining 1 victory point)' self.player.vps += 1 C.log[self.player].writef('You got a {}{}', card, msg) pay_cost(self.player, C.state.costs.devcard, C.state.bank) else: raise Exception('Unknown obj {}: {}'.format(type(obj), obj)) if check_victory(C): raise GameOver raise stg.Decide('main')
def pre(self, C): raise SwitchPhase('auction')
def pre(self, C): raise SwitchPhase('ball')