def play(self, player): """A player tries to play a card.""" if len(player.hand) == 0: raise events.InvalidActionError( 'Player {0}: no more cards in hand.'.format(player.name)) if self.play_strategy == 'best': # Choose to play a playable card found_card = False for idx, card in enumerate(player.hand.cards): if self.piles.is_playable(card): logging.debug( 'Player {0}: found playable card {1}.'.format(player.name, card)) card = player.remove_card(idx) found_card = True break if self.play_strategy == 'random' or not found_card: # Play a random card card = player.remove_card() logging.info('Player {0}: playing card {1} randomly (strategy: {2}).'.format( player.name, card, self.play_strategy)) else: logging.info('Player {0}: playing card {1} (strategy: {2}).'.format( player.name, card, self.play_strategy)) self.piles.append(card)
def remove_card(self, idx=None): try: if idx is None: card = self.hand.pop() else: card = self.hand.pop(idx) return card except IndexError: # Index out of bounds raise events.InvalidActionError('Wrong action: card not in hand.') except events.CardError: # The player hand is empty raise events.CardError( 'Player {0}: no more cards in hand.'.format(self.name))
def discard(self, player): """A player tries to discard a card.""" if self.hints >= 7: raise events.InvalidActionError( 'Cannot discard: already full hints') try: # TODO: implement intelligent discard card = player.remove_card() logging.debug( 'Player {0} discards a {1}'.format(player.name, card)) self.hints = self.hints + 1 except events.CardError as e: logging.debug(e) raise events.CardError( 'Cannot discard: {0}\'s hand is empty'.format(player.name))
def hint(self): """Give hint. Actually decrements only the hint counter.""" if self.hints > 0: self.hints = self.hints - 1 else: raise events.InvalidActionError('Cannot hive hint: no more hints.')
def append(self, card): if card.color != self.color: raise events.InvalidActionError('Wrong card added!') super().append(card)