def test_reset_hands(self): deck = deck_of_cards.Deck() player1 = player.Player('Lasse', player.Hand(), player.Chips()) computer1 = player.Player('Lasse', player.Hand(), player.Chips()) blackjack.deal_initial_cards(deck, player1, computer1) blackjack.reset_hands(player1, computer1) result = len(player1.hand.cards) == 0 and len( computer1.hand.cards) == 0 self.assertEqual(result, True)
def test_check_winner_push(self): captured_output = StringIO() sys.stdout = captured_output # Redirects the output (print) player1 = player.Player('Lasse', player.Hand(), player.Chips()) dealer1 = player.Player('Lasse', player.Hand(), player.Chips()) player1.hand.value, dealer1.hand.value = 18, 18 blackjack.check_winner(player1, dealer1) result = captured_output.getvalue() expected_result = f'Dealer ({dealer1.name}) and player ({player1.name}) tie! PUSH\n' sys.stdout = sys.__stdout__ # Resets the redirect self.assertEqual(result, expected_result)
def test_take_bet_valid_int_equal_to_total_chips(self, input): captured_output = StringIO() sys.stdout = captured_output # Redirects the output (print) chips = player.Chips(total=100) blackjack.take_bet(chips) sys.stdout = sys.__stdout__ # Resets the redirect self.assertEqual(chips.bet, 100)
def test_hit_or_stand_case_stand(self, input): captured_output = StringIO() sys.stdout = captured_output # Redirects the output (print) deck = deck_of_cards.Deck() player1 = player.Player('Lasse', player.Hand(), player.Chips()) blackjack.hit_or_stand(deck, player1) sys.stdout = sys.__stdout__ # Resets the redirect self.assertEqual(len(player1.hand.cards), 0)
def main(): global playing # Prints a welcome message ascii_msg.welcome() # Asks for the player's desired display name player_name = input('\nEnter your display name: ') # Deposit Funds funds = deposit() # Asks if the the player would like to play as the dealer or the player choice = play_as_dealer() # Creates an object for the player and the computer bj_player = player_module.Player(player_name, player_module.Hand(), player_module.Chips(funds)) bj_computer = player_module.Player('Computer', player_module.Hand(), player_module.Chips()) while True: # if choice == False - Want to play as player if not choice: print('+-----------------------------------+') print('| You are now playing as the Player |') print('+-----------------------------------+') reset_hands(bj_player, bj_computer) deck = deck_of_cards.Deck() deck.shuffle() deal_initial_cards(deck, bj_player, bj_computer) take_bet(bj_player.chips) show_some_cards(bj_player, bj_computer) # Global variable set to True - False if the player stands while playing: hit_or_stand(deck, bj_player) show_some_cards(bj_player, bj_computer) # If player's hand exceeds 21, player_busts() is called and we break the while loop if bj_player.hand.value > 21: player_busts(bj_player, bj_computer) break # If the player hasn't busted, the dealer's hand is played until it reaches 17 or above # Dealer will Stand on 17 and above if bj_player.hand.value <= 21: while bj_computer.hand.value < 17: hit(deck, bj_computer.hand) show_all_cards(bj_player, bj_computer) check_winner(bj_player, bj_computer) # Informs the player of their total amount of chips print(f'\nYour total chips are at: {bj_player.chips.total}') new_game = input('\nWould you like to play again? y/n: ') if new_game.lower() == 'y': playing = True print('\n' * 100) continue else: print('\n' * 100) ascii_msg.goodbye() break # else (choice == True) - Want to play as dealer else: print('+-----------------------------------+') print('| You are now playing as the Dealer |') print('+-----------------------------------+') reset_hands(bj_player, bj_computer) deck = deck_of_cards.Deck() deck.shuffle() deal_initial_cards(deck, bj_player, bj_computer) take_bet(bj_player.chips) # Show all cards (possible since the player is playing automatically) show_all_cards(bj_computer, bj_player) while True: # Auto-play logic here # The computer will hit on 4-15 and stand on 16-21 if bj_computer.hand.value > 15 and bj_computer.hand.value <= 21: print('+---------------+') print(f'| {bj_computer.name} Stands |') print('+---------------+') break else: hit(deck, bj_computer.hand) show_all_cards(bj_computer, bj_player) # If player's hand exceeds 21, run player_busts() and break out of loop if bj_computer.hand.value > 21: player_busts(bj_computer, bj_player) break # If computer hasn't busted, the Dealer's hand will be played if bj_computer.hand.value <= 21: while playing: # Dealer auto stands at 17, 18, 19, 20, 21 if bj_player.hand.value >= 17 and bj_player.hand.value <= 21: playing = False break hit_or_stand(deck, bj_player) show_all_cards(bj_computer, bj_player) if bj_player.hand.value > 21: playing = False check_winner(bj_computer, bj_player) # Inform Player of their chips total print(f'\nYour total chips are at: {bj_player.chips.total}') new_game = input('\nWould you like to play again? y/n: ') if new_game.lower() == 'y': playing = True print('\n' * 100) continue else: print('\n' * 100) ascii_msg.goodbye() break
def test_deal_initial_cards(self): deck = deck_of_cards.Deck() player1 = player.Player('Lasse', player.Hand(), player.Chips()) computer1 = player.Player('Lasse', player.Hand(), player.Chips()) blackjack.deal_initial_cards(deck, player1, computer1) self.assertEqual(len(player1.hand.cards), len(computer1.hand.cards))
def test_hit_or_stand_case_hit(self, input): deck = deck_of_cards.Deck() player1 = player.Player('Lasse', player.Hand(), player.Chips()) blackjack.hit_or_stand(deck, player1) self.assertEqual(len(player1.hand.cards), 1)
def test_chips_lose_bet(self): chips = player.Chips(total=100) chips.bet = 50 chips.lose_bet() result = chips.total self.assertEqual(result, 50)