def move_token(self, token_name: str, token_index: int, player_name_dst: Optional[str]): if token_name not in self._tokens: raise CoupException(f"No token with name {token_name}") if player_name_dst is not None and not self.get_player_by_name(player_name_dst): raise CoupException(f"No player with name {player_name_dst}") self._tokens[token_name].set_token_position(token_index, player_name_dst)
def pay_to_bank(player: Player, coins: int): if coins < 0: raise CoupException(f"Can't pay negative amount of coins") if player.coins < coins: raise CoupException(f"Player {player.name} does not have {coins} coins") player.coins -= coins
def transfer_card(self, from_player: Player, player_name_dst: str, card_id: int): dst_player = self.get_player_by_name(player_name_dst) if not dst_player: raise CoupException(f"No player with name {player_name_dst}") if card_id not in from_player.cards: raise CoupException(f"{from_player.name} does not have card id {card_id}") card = from_player.cards[card_id] del from_player.cards[card_id] dst_player.cards[card_id] = card
def transfer(self, player: Player, player_name_dst, coins: int): if coins < 0: raise CoupException(f"Can't transfer negative amount of coins") player_dst = self.get_player_by_name(player_name_dst) if not player_dst: raise CoupException(f"No player with name {player_name_dst}") if player.coins < coins: raise CoupException(f"You don't not have {coins} coins") player.coins -= coins player_dst.coins += coins
def register_player(self, name) -> Player: if self.get_player_by_name(name) is not None: raise CoupException(f"{name} is already in the game") player = Player(name) self.players[player.id] = player return player
def return_card_to_deck(self, player: Player, card_id): card = player.pop_card(card_id) if not card: raise CoupException(f"Player {player.name} does not have card id {card_id}") card.visible = False self.deck.append(card) self.shuffle_deck()
def end_turn(self, player: Player): if self.game_over: return if self.turn is not player: raise CoupException("It's not your turn!") exposed_players_count = 0 self.switch_turn() while self.turn.is_out(): exposed_players_count += 1 if exposed_players_count >= len(self.playing_players) - 1: self.game_over = True break self.switch_turn()
def start(self, card_names, player_to_start): player = self.get_player_by_name(player_to_start) if not player: raise CoupException(f"No player with name {player_to_start}") self.started = True self.game_over = False self.turn = player [token.reset() for token in self._tokens.values()] self.card_names = card_names self.deck = self.create_cards(card_names) self.shuffle_deck() if len(self.deck) < CARDS_FOR_PLAYER * len(self.players): raise CoupException("There are not enough cards for the players") self.playing_players = [] for player in self.players.values(): self.playing_players.append(player) player.reset() for _ in range(CARDS_FOR_PLAYER): for player in self.playing_players: player.add_card(self.deck.pop())
def set_token_position(self, index: int, playerName: Optional[str]): try: self._tokens_positions[index] = playerName except IndexError: raise CoupException(f"Index {index} for token {self._name} is not valid")
def get_token_position(self, index: int) -> str: try: return self._tokens_positions[index] except IndexError: raise CoupException(f"Index {index} for token {self._name} is not valid")
def take_from_bank(player: Player, coins: int): if coins < 0: raise CoupException(f"Can't take negative amount of coins") player.coins += coins
def take_card_from_deck(self, player: Player): if len(self.deck) <= 0: raise CoupException("Deck is empty") card = self.deck.pop() player.add_card(card)
def open_card(player: Player, card_id: int): card = player.get_card(card_id) if not card: raise CoupException(f"Player {player.name} does not have card id {card_id}") card.visible = True
def kick_player(self, player_to_kick): player = self.get_player_by_name(player_to_kick) if player is None: raise CoupException(f"No player with name {player_to_kick}") self.unregister_player(player)