コード例 #1
0
class DeckTests(unittest.TestCase):

    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        '''deck should have 52 cards'''
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_repr(self):
        """repr should return a string in the form of 'deck of cards'"""
        self.assertEqual(repr(self.deck), "Deck of 52 cards")

    def test_count(self):
        """count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards),20)
        self.assertEqual(self.deck.count(),32)

    def test_shuffle_full_deck(self):
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
コード例 #2
0
class TestingDeckClass(unittest.TestCase):
    def setUp(self):
        self.deck = Deck()

    def test_init(self):
        """decks should have a cards attibute which is a list"""
        self.assertTrue(isinstance(self.deck.cards, list))
        self.assertEqual(len(self.deck.cards), 52)

    def test_deck_repr(self):
        """Test that repr returns a deck of 52 cards when first initialized, no more no less"""
        self.assertEqual(str(self.deck), "Deck of 52 cards")

    def test_count(self):
        """ count should return a count of the number of cards"""
        self.assertEqual(self.deck.count(), 52)
        self.deck.cards.pop()
        self.assertEqual(self.deck.count(), 51)

    def test_deal_sufficient_cards(self):
        """_deal should deal the number of cards specified"""
        cards = self.deck._deal(10)
        self.assertEqual(len(cards), 10)
        self.assertEqual(self.deck.count(), 42)

    def test_deal_insufficient_cards(self):
        """_deal should deal the number of cards left in the deck"""
        cards = self.deck._deal(100)
        self.assertEqual(len(cards), 52)
        self.assertEqual(self.deck.count(), 0)

    def test_deal_no_cards(self):
        """_deal should throw a ValueError if the deck is empty"""
        self.deck._deal(self.deck.count())
        with self.assertRaises(ValueError):
            self.deck._deal(1)

    def test_deal_card(self):
        """ deal_card whould deal a single card from the deck"""
        card = self.deck.cards[-1]
        dealt_card = self.deck.deal_card()
        self.assertEqual(card, dealt_card)
        self.assertEqual(self.deck.count(), 51)

    def test_deal_hand(self):
        """deal_hand should deal the number of cards passed"""
        cards = self.deck.deal_hand(20)
        self.assertEqual(len(cards), 20)
        self.assertEqual(self.deck.count(), 32)

    def test_shuffle_full_deck(self):
        """shuffle should shuffle the deck if the deck is full"""
        cards = self.deck.cards[:]
        self.deck.shuffle()
        self.assertNotEqual(cards, self.deck.cards)
        self.assertEqual(self.deck.count(), 52)

    def test_shuffle_not_full_deck(self):
        """shuffle should throw a ValueError if the deck isn't full"""
        self.deck._deal(1)
        with self.assertRaises(ValueError):
            self.deck.shuffle()
コード例 #3
0
class blackjack_game:
    def __init__(self, purse):

        #store the new deck
        self.game_deck = Deck()

        #initialize player with a purse
        self.Player = Player(purse)

        #initialize computer player with a purse
        self.Computer = Player(purse)

    def play_game(self, user_ante):
        """
        function used to implement game play.
        """

        #flag indicating game has started
        user_play = 1

        while ((self.Player.get_purse() > 0) and
               (self.Computer.get_purse() > 0)) and (user_play != 0):

            #create deck
            self.game_deck.create_deck()

            #shuffle deck
            self.game_deck.shuffle_deck()

            #add bet before game start
            self.Player.add_bet(user_ante)
            self.Computer.add_bet(user_ante)

            #deal first 2 card to player and computer
            self.deal_hand_initial(self.Player)
            self.deal_hand_initial(self.Computer)

            print("Player: ")
            self.Player.player_show_hand()
            print("CPU: ")
            self.Computer.player_show_hand()

            #print current purse and ante
            self.Player.player_stats()
            self.Computer.player_stats()

            #determine if we have a black jack winner
            if self.blackjack_payout(self.Player, self.Computer) == True:
                continue

            user_raise = int(input("Player, how much do you want to raise?"))

            #add ante to the current bet
            self.Player.add_bet(user_raise)
            self.Computer.add_bet(user_raise)

            user_choice = int(input("Player, hit (1) or stay (0)?"))

            while user_choice == 1:

                self.Player.draw_card(self.game_deck.deal_card())
                print("Player: ")
                self.Player.player_show_hand()

                user_choice = int(input("Player, hit (1) or stay (0)?"))

            #cpu keeps drawing cards until hand score > 16
            cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            while cpu_hand_score < 16:

                #draw card
                self.Computer.draw_card(self.game_deck.deal_card())
                print("CPU: ")
                self.Computer.player_show_hand()

                #update hand score
                cpu_hand_score = self.calc_hand_score(self.Computer.get_hand())

            #calculate total value of hand
            player_hand_score = self.calc_hand_score(self.Player.get_hand())
            self.Player.set_hand_score(player_hand_score)
            self.Computer.set_hand_score(cpu_hand_score)

            #identify the winner of the round
            self.calc_winner_round()

            user_play = int(
                input("Do you want to continue? 1 = Continue, 0 = Quit"))

    def blackjack_payout(self, player, computer):
        """Calculates payout for player if the first 2 cards is 21."""
        if self.determine_black_jack(player) == True:

            if self.determine_black_jack(computer) == False:

                #add the bet to the game
                computer.add_bet(computer.get_current_bet() * 1.5)

                #add computers bet to player
                player.add_to_purse(computer.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

            return True

        else:
            if self.determine_black_jack(computer) == True:

                #add the bet to the game
                player.add_bet(computer.get_current_bet() * 1.5)

                #add players bet to computer
                computer.add_to_purse(player.get_current_bet())

                player.reset_bet()
                computer.reset_bet()

                player.reset_hand()
                computer.reset_hand()

                return True

    def determine_black_jack(self, player):
        """determines if player or computer gets 21 with first 2 cards."""
        return self.calc_hand_score(player.get_hand()) == 21

    def deal_hand_initial(self, Player):
        """Deal the first 2 cards to the player."""
        Player.set_hand(self.game_deck.deal_card())
        Player.set_hand(self.game_deck.deal_card())

    def calc_winner_round(self):
        """
        Determine the winner for each round.
        """
        player_score = self.Player.get_hand_score()
        cpu_score = self.Computer.get_hand_score()

        print("Player Hand: ", player_score)
        print("CPU hand: ", cpu_score)

        if player_score > 21:

            print("Player 1 Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Computer.add_to_purse(self.Player.get_current_bet())

        elif cpu_score > 21:

            print("Computer Loss: ", self.Player.get_current_bet())

            #add to Players purse
            self.Player.add_to_purse(self.Computer.get_current_bet())

        #if player and cpu does not bankrupt
        else:

            if player_score > cpu_score:
                print("Computer Loss: ", player_score, cpu_score,
                      self.Computer.get_current_bet())

                #add to Players purse
                self.Player.add_to_purse(self.Computer.get_current_bet())

            else:
                print("Player 1 Loss: ", player_score, cpu_score,
                      self.Player.get_current_bet())

                #add to Players purse
                self.Computer.add_to_purse(self.Player.get_current_bet())

        #reset bet for new round
        self.Player.reset_bet()
        self.Computer.reset_bet()

        #reset hand for new round
        self.Player.reset_hand()
        self.Computer.reset_hand()

    def calc_hand_score(self, list_of_cards):

        hand_score = 0

        ace = " "

        for card in list_of_cards:

            #get value of card
            current_card = card.get_card()[3]

            #check if adding an ace makes the player bust
            if current_card == 'A':

                if hand_score + 11 < 22:

                    hand_score += 11

                    #skip everything else in the loop
                    continue

            #this case looks at all face cards including A, A is treated as 1
            if type(current_card) == str:
                #convert K,Q,J,A
                hand_score += self.facecard_to_int(current_card)
            else:
                hand_score += current_card

        return hand_score

    def facecard_to_int(self, current_card):

        face_cards = {"K": 10, "Q": 10, "J": 10, "A": 1}
        try:

            #if K,Q,J or A
            return face_cards.get(current_card)

        except:

            #if numeric card
            return current_card
コード例 #4
0
class DeckTests(unittest.TestCase):
    def setUp(self):
        self.sample_deck = Deck()

    def test_init(self):
        self.assertIsInstance(self.sample_deck.cards, list)
        self.assertEqual(len(self.sample_deck.cards), 52)

    def test_repr(self):
        self.assertEqual(repr(self.sample_deck), "Deck of 52 cards")

    def test_iter(self):
        self.assertTrue(iter(self.sample_deck))

    def test_count(self):
        self.assertEqual(self.sample_deck.count(), 52)
        self.sample_deck.cards.pop()
        self.assertEqual(self.sample_deck.count(), 51)

    def test_deal_cards(self):
        self.sample_deck._deal(10)
        self.assertEqual(len(self.sample_deck.cards), 42)
        self.assertEqual(len(self.sample_deck._new_hand), 10)

    def test_deal_last(self):
        self.sample_deck._deal(56)
        self.assertEqual(len(self.sample_deck.cards), 0)
        self.assertEqual(len(self.sample_deck._new_hand), 52)

    def test_deal_no_cards(self):
        self.sample_deck.cards.clear()
        with self.assertRaises(ValueError):
            self.sample_deck._deal(2)

    def test_deal_card(self):
        result = self.sample_deck.deal_card()
        self.assertIsInstance(result, Card)
        self.assertEqual(result, self.sample_deck._new_hand[0])
        self.assertEqual(len(self.sample_deck.cards), 51)
        self.assertEqual(len(self.sample_deck._new_hand), 1)

    def test_deal_hand(self):

        result = self.sample_deck.deal_hand(15)
        self.assertIsInstance(result, list)
        self.assertEqual(len(self.sample_deck.cards), 37)
        self.assertEqual(len(self.sample_deck._new_hand), 15)

    def test_shuffle_invalid(self):
        """shuffle raises a ValueError if the card deck is not full"""
        self.sample_deck.cards.pop()
        with self.assertRaises(ValueError):
            self.sample_deck.shuffle()

    def test_shuffle_valid(self):
        """shuffle shuffles a full deck in place"""
        original = self.sample_deck.cards[:]  #making a new copy b/c lists are mutable!
        self.sample_deck.shuffle()
        result = self.sample_deck.cards
        self.assertEqual(len(result), 52)
        self.assertFalse(original == result)