def user_play(play_shoe, player, dealer): """ Function that allows the user to play """ print("\nDealer shows:" + dealer.get_viewable_hand()) hit = True while hit == True: decision = " " if len(player.get_hand()) == 2: print("\nPlayer " + player.get_name() + " your hand is:" + player.get_viewable_hand()) else: print("\nYour hand is now:" + str(player.get_viewable_hand())) decide_soft_score_print(player) if not (check_blackjack(player.get_score(), player.get_hand())): if not (player.check_bust()) and player.get_score() < 21: while not (decision[0] == "h") and not (decision[0] == "s"): decision = input( "Would you like to Hit or Stand? ").lower() if decision[0] == "h": player.hit_hand(play_shoe) else: hit = False else: hit = False else: hit = False check_stand(player)
def check_results(player_list, dealer): """ Function that checks if each user has more or less than the dealer and decides if they have won or lost then adds them to a list accordingly. """ dealer_score = dealer.get_score() dealer_hand = dealer.get_hand() blackjack_winners = [] winners = [] losers = [] pushers = [] dealer_plays = True if dealer_score > 21: dealer_plays = False for player in player_list: player_score = player.get_score() player_hand = player.get_hand() if dealer_plays and check_blackjack(dealer_score, dealer_hand): if check_blackjack(player_score, player_hand): pushers.append(player) else: losers.append(player) elif dealer_plays: if player_score > dealer_score and not (player.check_bust()): if check_blackjack(player_score, player_hand): blackjack_winners.append(player) else: winners.append(player) elif player_score == dealer_score: pushers.append(player) else: losers.append(player) else: if check_blackjack(player_score, player.get_hand()): blackjack_winners.append(player) break elif player_score <= 21: winners.append(player) else: losers.append(player) return winners, losers, pushers, blackjack_winners
def test_user_stands_with_blackjack(self): """ Test that a user automatically stands when they have blackjack """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(10, "H")) test_shoe.cards.insert(2, card(1, "H")) initial_deal(test_shoe, player_list, dealer) self.assertEqual(player.get_score(), 21) self.assertTrue(check_blackjack(player.get_score(), player.get_hand()))
def test_user_wins_with_blackjack(self): """ Test that the user wins when they get blackjack and they get the custom message """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(10, "H")) test_shoe.cards.insert(2, card(1, "H")) initial_deal(test_shoe, player_list, dealer) self.assertEqual(player.get_score(), 21) self.assertTrue(check_blackjack(player.get_score(), player.get_hand())) dealer_play(test_shoe, dealer) results = check_results(player_list, dealer) blackjack_winners = results[3] self.assertIn(player, blackjack_winners)
def test_user_stands_on_21(self): """ Test that a user automatically stands when they hit 21 """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(10, "H")) test_shoe.cards.insert(2, card(3, "H")) test_shoe.cards.insert(4, card(8, "D")) initial_deal(test_shoe, player_list, dealer) with mock.patch('builtins.input', return_value="hit"): user_play(test_shoe, player, dealer) self.assertEqual(player.get_score(), 21) self.assertFalse(check_blackjack(player.get_score(), player.get_hand()))
def test_user_pushes_with_blackjack(self): """ Test that the user pushes with blackjack when the dealer also has a blackjack """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(10, "H")) test_shoe.cards.insert(1, card(10, "C")) test_shoe.cards.insert(2, card(1, "H")) initial_deal(test_shoe, player_list, dealer) self.assertEqual(player.get_score(), 21) dealer_play(test_shoe, dealer) self.assertTrue(check_blackjack(dealer.get_score(), dealer.get_hand())) self.assertTrue(check_blackjack(player.get_score(), player.get_hand())) results = check_results(player_list, dealer) pushers = results[2] self.assertIn(player, pushers)
def test_user_pushes(self): """ Test that when a user's score and the dealer's score are the same they push """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(8, "H")) test_shoe.cards.insert(1, card(10, "C")) test_shoe.cards.insert(2, card(9, "C")) test_shoe.cards.insert(3, card(6, "D")) initial_deal(test_shoe, player_list, dealer) dealer_play(test_shoe, dealer) self.assertEqual(player.get_score(), dealer.get_score()) self.assertFalse(check_blackjack(player.get_score(), player.get_hand())) results = check_results(player_list, dealer) pushers = results[2] self.assertIn(player, pushers)
def test_user_loses(self): """ Test that a user loses when the dealer's score exceeds theirs and the dealer doesn't bust """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(8, "H")) test_shoe.cards.insert(1, card(10, "C")) test_shoe.cards.insert(2, card(8, "C")) test_shoe.cards.insert(3, card(6, "D")) initial_deal(test_shoe, player_list, dealer) dealer_play(test_shoe, dealer) self.assertLess(player.get_score(), dealer.get_score()) self.assertFalse(check_blackjack(player.get_score(), player.get_hand())) results = check_results(player_list, dealer) losers = results[1] self.assertIn(player, losers)
def test_user_wins_when_dealer_busts(self): """ Test that a user wins when the dealer exceeds 21 as long as they have not also bust themselves """ test_shoe = self.test_shoe player_list = self.test_player_list dealer = self.dealer player = player_list[0] test_shoe.cards.insert(0, card(8, "H")) test_shoe.cards.insert(1, card(10, "C")) test_shoe.cards.insert(2, card(8, "C")) test_shoe.cards.insert(3, card(6, "D")) test_shoe.cards.insert(4, card(10, "D")) initial_deal(test_shoe, player_list, dealer) dealer_play(test_shoe, dealer) self.assertTrue(dealer.check_bust()) self.assertFalse(check_blackjack(player.get_score(), player.get_hand())) results = check_results(player_list, dealer) winners = results[0] self.assertIn(player, winners)