Beispiel #1
0
    def test_pop(self):
        """
        create card stack
        call pop()
        check if the correct number of cards were popped

        test cases:
        0 cards in stack
            ask for 0
            ask for more
        1 card in stack
            ask for less than 1
            ask for 1
            ask for more
        many cards in stack
            ask for less
            ask for the stack
            ask for more

        checks:
        :return:
        """
        # try to pop zero cards from an empty stack
        card_stack = CardStack()
        self.assertEqual(len(card_stack.cards), 0)
        cards = card_stack.pop(0)
        self.assertEqual(len(cards), 0)

        # try to pop a card from an empty stack
        self.assertEqual(len(card_stack.cards), 0)
        self.assertRaises(RuntimeError, card_stack.pop, 1)

        # try to pop zero cards from stack with one card
        card_stack = CardStack([Copper()])
        self.assertEqual(len(card_stack.cards), 1)
        cards = card_stack.pop(0)
        self.assertEqual(len(cards), 0)
        self.assertEqual(len(card_stack.cards), 1)

        # try to pop one card from stack with one card
        self.assertEqual(len(card_stack.cards), 1)
        cards = card_stack.pop(1)
        self.assertEqual(len(cards), 1)
        self.assertEqual(len(card_stack.cards), 0)
        self.assertTrue(isinstance(cards[0], Copper))

        # try to pop more than one card from a stack with one card
        card_stack = CardStack([Copper()])
        self.assertEqual(len(card_stack.cards), 1)
        self.assertRaises(RuntimeError, card_stack.pop, 2)

        # try to pop less than the amount of cards from a stack with many cards
        card_stack = CardStack([Copper(), Silver(), Gold()])
        self.assertEqual(len(card_stack.cards), 3)
        cards = card_stack.pop(2)
        self.assertEqual(len(cards), 2)
        self.assertEqual(len(card_stack.cards), 1)
        self.assertTrue(isinstance(cards[0], Copper))
        self.assertTrue(isinstance(cards[1], Silver))
        self.assertTrue(isinstance(card_stack.cards[0], Gold))

        # try to pop the same amount of cards from a stack with many cards
        card_stack = CardStack([Copper(), Silver(), Gold()])
        self.assertEqual(len(card_stack.cards), 3)
        cards = card_stack.pop(3)
        self.assertEqual(len(cards), 3)
        self.assertEqual(len(card_stack.cards), 0)
        self.assertTrue(isinstance(cards[0], Copper))
        self.assertTrue(isinstance(cards[1], Silver))

        # try to pop more cards from a stack with many cards
        card_stack = CardStack([Copper(), Silver(), Gold()])
        self.assertEqual(len(card_stack.cards), 3)
        self.assertRaises(RuntimeError, card_stack.pop, 4)
        self.assertTrue(isinstance(card_stack.cards[0], Copper))
        self.assertTrue(isinstance(card_stack.cards[1], Silver))
        self.assertTrue(isinstance(card_stack.cards[2], Gold))
Beispiel #2
0
class PlayerState:
    """The Player is a base class for the HumanPlayer and ComputerPlayer classes

    """
    def __init__(self, name, supply):
        self.name = name
        self.hand = []
        self.play_area = []
        self.draw_deck = CardStack()
        self.discard_pile = CardStack()
        self.initialize_draw_deck()
        self.draw_cards(5)

        # How many action cards can the player play
        self.actions = 1

        # How many cards can the player buy
        self.buys = 1

        # How much money did the player used in the current turn
        self.used_money = 0

    def dump(self):
        print('Name:', self.name)
        print(self.hand)
        print(repr(self.discard_pile))
        print(repr(self.draw_deck))

    @property
    def all_cards(self):
        return CardStack(self.hand + self.draw_deck.cards +
                         self.discard_pile.cards)

    def _cleanup(self):
        """Discard hand and play area
        """
        self.discard_pile.add_to_top(self.hand + self.play_area)
        self.hand = []
        self.play_area = []

    def initialize_draw_deck(self):
        """Add 7 coppers and 3 estates to the draw deck and shuffle it"""
        for i in range(7):
            self.draw_deck.cards.append(Copper())
        for i in range(3):
            self.draw_deck.cards.append(Estate())
        self.draw_deck.shuffle()

    def reload_deck(self, n):
        if self.draw_deck.count < n:
            self.discard_pile.shuffle()
            self.draw_deck.cards += self.discard_pile.cards
            self.discard_pile = CardStack()

    def draw_cards(self, n):
        """Draw the top n cards from the draw deck and add them to the hand"""
        if n <= 0:
            return
        self.reload_deck(n)
        if self.draw_deck.count < n:
            n = self.draw_deck.count
        self.hand += self.draw_deck.pop(n)

    def done(self):
        """Perform the following:
            - cleanup
            - reset buys to 1
            - reset actions to 1
        """
        self._cleanup()
        self.draw_cards(5)
        self.buys = 1
        self.actions = 1
        print(f'{self.name}: done')

    def get_personal_state(self, supply) -> PersonalState:
        ps = PersonalState(hand=copy.deepcopy(self.hand),
                           discard_pile=copy.deepcopy(self.discard_pile),
                           draw_deck=self.draw_deck.as_dict(),
                           supply=copy.deepcopy(supply),
                           play_area=self.play_area[:],
                           actions=self.actions,
                           buys=self.buys,
                           used_money=self.used_money)
        return ps