def test_deck(): # Setup card = Card(12, "red") special_card = Pickup2Card(0, "red") blue_card = ReverseCard(0, "blue") cards = [card, special_card, blue_card] deck = Deck(cards) # Testing print("Get Cards:", deck.get_cards( )) #== [Card(12, 'red'), Pickup2Card(0, 'red'), ReverseCard(0, 'blue')]) print("Amount:", deck.get_amount() == 3) print("Top:", deck.top()) #ReverseCard(0, blue) print() new_card = SkipCard(0, "green") deck.add_card(new_card) deck.add_cards([card, special_card, blue_card]) print("new amount:", deck.get_amount() == 7) print( "New Deck:", deck.get_cards() ) # [Card(12, red), Pickup2Card(0, red), ReverseCard(0, blue), SkipCard(0, green), Card(12, red), Pickup2Card(0, red),ReverseCard(0, blue)]) print("Pick:", deck.pick()) # [ReverseCard(0, blue)] print("Pick 2:", deck.pick(amount=2)) # [Pickup2Card(0, red), Card(12, red)] deck.shuffle() print( "New Deck", deck.get_cards() ) #[SkipCard(0, green), Card(12, red), Pickup2Card(0, red),ReverseCard(0, blue)]
class UnoGame: """ A game of Uno++. """ def __init__(self, deck, players): """ Construct a game of uno from a pickup pile and list of players. Parameters: deck (Deck): The pile of cards to pickup from. players (list<Player>): The players in this game of uno. """ self.pickup_pile = deck self.players = players self._turns = TurnManager(players) self.putdown_pile = Deck(self.pickup_pile.pick()) self.special_pile = Deck() self._is_over = False self.winner = None def next_player(self): """ Changes to the next player in the game and returns an instance of them. Returns: (Player): The next player in the game. """ return self._turns.next() def current_player(self): """ (Player) Returns the player whose turn it is currently. """ return self._turns.current() def skip(self): """Prevent the next player from taking their turn.""" self._turns.skip() def reverse(self): """Transfer the turn back to the previous player and reverse the order.""" self._turns.reverse() def get_turns(self): """(TurnManager) Returns the turn manager for this game.""" return self._turns def is_over(self): """ (bool): True iff the game has been won. Assigns the winner variable. """ for player in self.players: if player.has_won(): self.winner = player self._is_over = True return self._is_over def select_card(self, player, card): """Perform actions for a player selecting a card Parameters: player (Player): The selecting player. card (Card): The card to select. """ card.play(player, self) if card.__class__ in SPECIAL_CARDS: self.special_pile.add_card(card) else: self.putdown_pile.add_card(card) def take_turn(self, player): """ Takes the turn of the given player by having them select a card. Parameters: player (Player): The player whose turn it is. """ card = player.pick_card(self.putdown_pile) if card is None: player.get_deck().add_cards(self.pickup_pile.pick()) return if card.matches(self.putdown_pile.top()): self.select_card(player, card) def take_turns(self): """ Plays an entire round by taking the turn for each player in the game. """ for player in self.players: self.take_turn(player) if player.has_won(): return
class CodersGame: """ A game of Sleeping Coders. """ def __init__(self, deck, coders, players): """ Construct a game of Sleeping Coders from a pickup pile, a list of coder cards and a list of players. Parameters: deck (Deck): The pile of cards to pickup from. coders (List<Card>): The list of sleeping coder cards. players (list<Player>): The players in this game. """ self._pickup_pile = deck self._coders = coders self.players = players self._turns = TurnManager(players) self.putdown_pile = Deck() self._is_over = False self.winner = None self._action = None def get_pickup_pile(self): """(Deck): Returns the pickup file of a game.""" return self._pickup_pile def get_sleeping_coders(self): """(List[Card]): Returns the list of coders who are still asleep in the game.""" return self._coders def get_sleeping_coder(self, slot): """(Card): Returns the sleeping coder card at the given slot. Parameters: slot (int): The slot within the sleeping coders collection to get. """ return self._coders[slot] def set_sleeping_coder(self, slot, card): """Set the coder at the given slot in the sleeping coders collection Parameters: slot (int): The slot within the sleeping coders collection to set. card (Card): The card to place in the given slot. """ self._coders[slot] = card def pick_card(self, blocked_classes=()): """ Pick a the first card from the pickup pile that is not an instance of any of the classes in the blocked_classes. Parameters: blocked_classes (tuple<Card>): The classes that the card cannot be an instance of. Returns: (List[Card]): The card picked from the top of the pickup pile. """ while True: picked_card = self._pickup_pile.pick() if not any((isinstance(picked_card, card_class) for card_class in blocked_classes)): return picked_card def next_player(self): """ Changes to the next player in the game and returns an instance of them. Returns: (Player): The next player in the game. """ return self._turns.next() def current_player(self): """ (Player) Returns the player whose turn it is currently. """ return self._turns.current() def skip(self): """Prevent the next player from taking their turn.""" self._turns.skip() def reverse(self): """Transfer the turn back to the previous player and reverse the order.""" self._turns.reverse() def get_turns(self): """(TurnManager) Returns the turn manager for this game.""" return self._turns def is_over(self): """ (bool): True iff the game has been won. Assigns the winner variable. """ if self._pickup_pile.get_amount() == 0: return True for player in self.players: if player.has_won(): self.winner = player self._is_over = True return self._is_over def select_card(self, player, card): """Perform actions for a player selecting a card Parameters: player (Player): The selecting player. card (Card): The card to select. """ card.play(player, self) self.putdown_pile.add_card(card) def get_action(self): """ Get the current action being performed. """ return self._action def set_action(self, action): """ Set the current action being performed to action. Possible Actions: NO_ACTION: No action is being performed. PICKUP_CODER: Tutor card played to pickup a coder. STEAL_CODER: Keyboard Kidnapper played to steal another players coder. SLEEP_CODER: All-nighter played to put another players coder to sleep. Parameters: action (str): The current action. """ self._action = action def get_last_card(self): """ (Card): Get the last card that was played. """ return self.putdown_pile.top()
print(special_card.matches(blue_card)) print('-' * 5) print('*' * 40) # Deck cards = [card, special_card, blue_card] deck = Deck(cards) print('deck.get_cards()') print(deck.get_cards()) print('-' * 5) print('deck.get_amount()') print(deck.get_amount()) print('-' * 5) print('deck.top()') print(deck.top()) print('-' * 5) new_card = SkipCard(0, "green") deck.add_card(new_card) from os import system system('pause') deck.add_cards([card, special_card, blue_card]) print(deck.get_cards()) print(deck.get_amount()) print(deck.pick()) print(deck.get_amount()) print(deck.pick(amount=2)) deck.shuffle() print(deck.get_cards()) print("*" * 40)