示例#1
0
class TestPlayerClass(unittest.TestCase):
    def test_score_aces(self):
        self.shoe = Shoe(1)
        self.hand = Player(self.shoe)
        self.card1 = Card(("A", "S", 1))
        self.card2 = Card(("A", "H", 2))
        self.hand.hand_of_cards.append(self.card1)
        self.hand.hand_of_cards.append(self.card2)
        self.assertEqual(self.hand.score(), 12)

    def test_score_(self):
        self.shoe = Shoe(1)
        self.hand = Dealer(self.shoe)
        self.card1 = Card(("K", "S", 1))
        self.card2 = Card(("3", "H", 2))
        self.hand.hand_of_cards.append(self.card1)
        self.hand.hand_of_cards.append(self.card2)
        self.assertEqual(self.hand.score(), 13)

    def test_get_card(self):
        self.shoe = Shoe(1)
        self.hand = Dealer(self.shoe)
        self.assertEqual(self.hand.get_card(), True)
        self.assertEqual(len(self.hand.hand_of_cards), 1)
        self.assertEqual(self.hand.hits(), True)

    def test_pretty_output(self):
        self.shoe = Shoe(1)
        self.hand = Dealer(self.shoe)
        self.card1 = Card(("K", "Spades", 1))
        self.card2 = Card(("K", "Hearts", 2))
        self.hand.hand_of_cards.append(self.card1)
        self.hand.hand_of_cards.append(self.card2)
        self.assertEqual(self.hand.pretty_output(), "\nK of Spades\nK of Hearts\n")

    def test_update_status(self):
        self.shoe = Shoe(1)
        self.hand = Dealer(self.shoe)
        self.card1 = Card(("K", "S", 1))
        self.card2 = Card(("6", "H", 2))
        self.card3 = Card(("8", "H", 3))
        self.hand.hand_of_cards.append(self.card1)
        self.hand.hand_of_cards.append(self.card2)
        self.hand.update_status()
        self.assertEqual(self.hand.status["bj"], False)
        self.assertEqual(self.hand.status["interested"], True)
        self.assertEqual(self.hand.status["bust"], False)
        self.hand.hand_of_cards.append(self.card3)
        self.hand.hits()
        self.hand.update_status()
        self.assertEqual(self.hand.status["bj"], False)
        self.assertEqual(self.hand.status["interested"], False)
        self.assertEqual(self.hand.status["bust"], True)
示例#2
0
class Game:
    """ Initialize game of blackjack

    Parameters
    ---------
    shoe : Instance of Shoe Class
        The current shoe initialized in Blackjack.sitting()


    Attributes
    ----------
    dealer : instance of Player class
    player : instance of Player class
    initial_hand_size : int
        Number of cards initially dealt
    max_target_score : int
        A constant set to 21 per the rules of blackjack
    dealer_trigger : int
        A constant set to 17, the score at which the dealer will stop accepting
        additional cards, per the rules of blackjack
    """
    def __init__(self, shoe):
        self.shoe = shoe
        self.dealer = Dealer(shoe)
        self.player = User(shoe)
        self.initial_hand_size = 2
        self.max_target_score = 21
        self.dealer_trigger = 17

    def deal(self):
        """ Deals the set # of starting cards (defined in the init)"""
        player_list = [self.dealer, self.player]
        for elem in player_list:
            for x in range(self.initial_hand_size):
                if self.shoe.cards:
                    elem.hand_of_cards.append(self.shoe.hand_out_card())
                else:
                    return False
        return True

    def report(self, finished=False):
        """ Reports the status of the game to the stdout on command line """
        
        if finished is False:
            verb = "has"
        else:
            verb = "had"
        print(chr(27) + "[2J")
        print("Dealer {}: {}For a total of {}"
              .format(verb, self.dealer.pretty_output(), self.dealer.score()))
        print("\n")
        print("Player {}: {}For a total of {}"
              .format(verb, self.player.pretty_output(), self.player.score()))
        print("\n")

    def eval_state(self):
        """ Evaluates the status of the game to the stdout on command line

        Returns
        -------
        string
            "d", "p", or None (if out of cards or unresolved)
        """
        winner = None
        if self.dealer.status["bj"] or self.player.status["bust"]:
            winner = "d"
        elif self.player.status["bj"] or self.dealer.status["bust"]:
            winner = "p"
        return winner

    def get_winner(self):
        """ Determines the winner if both players pass

        Returns
        -------
        string 
            "d" (dealer) or "p" (player) for victor

        """
        if self.player.score() <= self.dealer.score():
            return "d"
        else:
            return "p"


    def report_results(self):
        """ Report final outcome of the hand

        Returns
        -------
        True
            As notification of execution

        """
        print("Dealer finished with: ", self.dealer.score())
        print("You had: ", self.player.score())
        print("\n")
        return True

    def play(self):
        """ Plays a single round of blackjack

        Returns
        -------
        instance of Player object
        
        """

        if not self.deal():
            return None  # .hand_out_card reports back no more cards
        if not self.dealer.update_status() or\
               not self.player.update_status():
                print("Something went wrong.")
        winner = self.eval_state()        
        while not winner:
            self.report()
            if self.dealer.status["interested"] and self.dealer.hits() and\
               not self.dealer.get_card():
                return None  # .get_card reports back no more cards
            elif self.player.status["interested"] and self.player.hits() and\
                 not self.player.get_card():
                return None  # .get_card reports back no more cards
            
            if not self.dealer.update_status() or\
               not self.player.update_status():
                print("Something went wrong.")  # If update fails 
            winner = self.eval_state()
            if winner:
                break
            elif not self.dealer.status["interested"] and\
                 not self.player.status["interested"]:
                winner = self.get_winner()
                break
        self.report(finished=True)
        if self.report_results():
            
            return winner 
        else:
            print("Something went wrong.")
            return None