def test_hand_add_card(self): hand1 = player.Hand() hand2 = player.Hand() card = deck_of_cards.Card('Hearts', 'Ace') hand2.add_card(card) result = hand1.cards == hand2.cards self.assertFalse(result)
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 reset_hands(player, computer): """Resets the player's and computer's hands to prepair for a new game Parameters ---------- player : Player The player in the game computer : Player The computer in the game """ player.hand = player_module.Hand() computer.hand = player_module.Hand()
def test_manual_open_a_shape(self): h = player.Hand() h.open_strings = (0, 1, 5) h.fingers[0].move((2, 2)) h.fingers[1].move((3, 2)) h.fingers[2].move((4, 2)) self.assertEqual(h.shape, open_a)
def teat_manual_open_g_shape(self): h = player.Hand() h.open_strings = [2, 3, 4] h.fingers[0].move((1, 2)) h.fingers[1].move((0, 3)) h.fingers[2].move((5, 3)) self.assertEqual(h.shape, open_g)
def test_manual_open_e_shape(self): h = player.Hand() h.open_strings = [0, 4, 5] h.fingers[0].move((3, 1)) h.fingers[1].move((1, 2)) h.fingers[2].move((2, 2)) self.assertEqual(h.shape, open_e)
def test_manual_open_d_shape(self): h = player.Hand() h.open_strings = [1, 2] h.fingers[0].move((3, 2)) h.fingers[1].move((4, 3)) h.fingers[2].move((5, 2)) self.assertEqual(h.shape, open_d)
def test_hand_adjust_for_ace(self): hand1 = player.Hand() hand1.add_card(deck_of_cards.Card('Diamonds', 'Ace')) hand1.add_card(deck_of_cards.Card('Spades', 'Ace')) hand1.adjust_for_ace() result = hand1.value self.assertEqual(result, 12)
def test_hand_add_card_ace_counter(self): hand1 = player.Hand() hand1.add_card(deck_of_cards.Card('Hearts', 'Ace')) hand1.add_card(deck_of_cards.Card('Spades', 'Ace')) hand1.add_card(deck_of_cards.Card('Clubs', 'Ace')) result = hand1.aces self.assertEqual(result, 3)
def test_manual_barre_b_shape(self): h = player.Hand() h.barre = True h.fingers[0].move((0, 2)) h.fingers[1].move((2, 4)) h.fingers[2].move((3, 4)) h.fingers[3].move((4, 4)) self.assertEqual(h.shape, barre_b)
def test_hand_adjust_for_ace_with_value_above_21_and_aces(self): hand1 = player.Hand() hand1.add_card(deck_of_cards.Card('Diamonds', 'Ace')) hand1.add_card(deck_of_cards.Card('Spades', 'Ace')) aces_pre_adjustment = hand1.aces hand1.adjust_for_ace() result = (hand1.value == 12 and aces_pre_adjustment != hand1.aces) self.assertTrue(result)
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 test_manual_barre_a_shape(self): h = player.Hand() h.barre = True h.fingers[0].move((0, 5)) h.fingers[1].move((1, 7)) h.fingers[2].move((2, 7)) h.fingers[3].move((3, 6)) self.assertEqual(h.shape, barre_a)
def test_hand_adjust_for_ace_with_value_under_21(self): hand1 = player.Hand() hand1.add_card(deck_of_cards.Card('Diamonds', 'Ace')) hand1.add_card(deck_of_cards.Card('Spades', 'Four')) value_pre_adjustment = hand1.value aces_pre_adjustment = hand1.aces hand1.adjust_for_ace() result = (value_pre_adjustment == hand1.value and aces_pre_adjustment == hand1.aces) self.assertTrue(result)
def test_hand_adjust_for_ace_with_value_above_21_no_ace(self): hand1 = player.Hand() hand1.add_card(deck_of_cards.Card('Diamonds', 'King')) hand1.add_card(deck_of_cards.Card('Spades', 'Queen')) hand1.add_card(deck_of_cards.Card('Spades', 'Seven')) value_pre_adjustment = hand1.value aces_pre_adjustment = hand1.aces hand1.adjust_for_ace() result = (value_pre_adjustment == hand1.value and aces_pre_adjustment == hand1.aces) self.assertTrue(result)
def test_barred_slide_up_f_to_a(self): h = player.Hand(barre_f) self.assertEqual(h.move(barre_a), 4)
def test_init_open_d_shape(self): h = player.Hand(open_d) self.assertEqual(h.shape, open_d)
def test_all_open_shape(self): h = player.Hand(all_open) self.assertEqual(h.shape, all_open)
def test_null_move(self): h = player.Hand(all_open) self.assertEqual(h.move(all_open), 0)
def test_init_barre_b_shape(self): h = player.Hand(barre_b) self.assertEqual(h.shape, barre_b)
def test_barre_a_strain(self): h = player.Hand(barre_a) self.assertEqual(h.strain, 6)
def test_open_d_strain(self): ''' Should be 2, but barring it does hit 1. I'll allow it. ''' h = player.Hand(open_d) self.assertEqual(h.strain, 1)
def test_open_e_strain(self): h = player.Hand(open_e) self.assertEqual(h.strain, 2)
def test_null_strain(self): h = player.Hand(all_open) self.assertEqual(h.strain, 0)
def test_chord_change_e_to_a(self): h = player.Hand(open_e) self.assertEqual(h.move(open_a), 8)
def test_single_notes(self): for string, fret in it.product(range(5), range(19)): with self.subTest(i=(string, fret)): h = player.Hand([(string, fret)]) self.assertEqual(h.shape, [(string, fret)])
def test_barred_slide_down_a_to_f(self): h = player.Hand(barre_a) self.assertEqual(h.move(barre_f), 4)
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_chord_change_c_to_g(self): h = player.Hand(open_c) self.assertEqual(h.move(open_g), 11)