def play_blackjack_game(): # initialize a game with shuffled deck and hands dealt to dealer and player: game = blackjack_engine.BlackjackGame() print('initial player hand: ', game.player_hands[0]) print('initial dealer hand: ', game.dealer_hand, '\n') # check if game ended with blackjacks if game.is_finished == True: if game.result == 2: print('Player wins with blackjack') elif game.result == -1: print('Player loses, dealer blackjack') # otherwise, loop through player decisions until game is over else: while True: hit_or_stay = player_bot(game.player_hands) print('player will ', hit_or_stay) game.player_move(hit_or_stay) if hit_or_stay == 'hit': print('new player hand:', game.player_hands[0], '\n') if game.player_hands[0].is_busted: print('Player busted') # print('dealer hand: ', game.dealer_hand.cards, '\n') if game.is_finished == True: print('Final dealer hand:', game.dealer_hand) if game.result == -1: print('Player loses') if game.result == 0: print('Push') if game.result == 1: print('Player wins') break # game is finished so exit loop return game
def test_Game_full_loss_2(): # player... # initial player hand [J 4], total: 14 # initial dealer hand [A K], total: 21 seeded_deck = blackjack_engine.Deck(seed=55832) game = blackjack_engine.BlackjackGame(deck=seeded_deck) print('initial player hand', game.player_hands[0]) print('initial dealer hand', game.dealer_hand) assert game.is_finished == True # assert game.player_hands[0].cards == [] # behavior undefined ? assert game.dealer_hand.cards == ['A', 'K'] assert game.result == -1
def test_Game_full_push_1(): # both dealt 20s, both stay, push # player_hands: [[J 10], total: 20] # dealer_hand: [Q 10], total: 20 seeded_deck = blackjack_engine.Deck(seed=1) game = blackjack_engine.BlackjackGame(deck=seeded_deck) assert game.is_finished == False game.player_move('stay') assert game.is_finished == True assert game.player_hands[0].cards == ['J', '10'] assert game.dealer_hand.cards == ['Q', '10'] assert game.result == 0
def test_Game_full_win_3(): # player dealt blackjack, player wins # initial player hand [10 A], total: 21 # initial dealer hand [J 6], total: 16 seeded_deck = blackjack_engine.Deck(seed=63) game = blackjack_engine.BlackjackGame(deck=seeded_deck) print('initial player hand', game.player_hands[0]) print('initial dealer hand', game.dealer_hand) assert game.is_finished == True print('final dealer hand: ', game.dealer_hand) assert game.player_hands[0].cards == ['10', 'A'] # assert game.dealer_hand.cards == ['J', '6'] # behavior undefined? assert game.result == 2
def test_Game_full_push_2(): # player hits to 19, dealer stays, push # initial player hand: [3 4], total: 7 # initial dealer hand: [10 9], total: 19 seeded_deck = blackjack_engine.Deck(seed=2) game = blackjack_engine.BlackjackGame(deck=seeded_deck) game.player_move('hit') # [[3 4 5], total: 12] game.player_move('hit') # [[3 4 5 7], total: 19] assert game.is_finished == False game.player_move('stay') assert game.is_finished == True assert game.player_hands[0].cards == ['3', '4', '5', '7'] assert game.dealer_hand.cards == ['10', '9'] assert game.result == 0
def test_Game_full_loss_1(): # player hits to hard 19, dealer hits to 21, player loses # initial player hand: [A 3], total: 14 # initial dealer hand: [4 7], total: 11 seeded_deck = blackjack_engine.Deck(seed=3) game = blackjack_engine.BlackjackGame(deck=seeded_deck) game.player_move('hit') # [[A 3 4], total: 18] game.player_move('hit') # [[A 3 4 A], total: 19] game.player_move('hit') # [[A 3 4 A K], total: 19] assert game.is_finished == False game.player_move('stay') assert game.is_finished == True assert game.player_hands[0].cards == ['A', '3', '4', 'A', 'K'] assert game.dealer_hand.cards == ['4', '7', '10'] assert game.result == -1
def test_Game_full_win_1(): # player hits on 16, wins with 21 # initial player hand [Q 6], total: 16 # initial dealer hand [10 K], total: 20 seeded_deck = blackjack_engine.Deck(seed=5) game = blackjack_engine.BlackjackGame(deck=seeded_deck) print('initial player hand', game.player_hands[0]) print('initial dealer hand', game.dealer_hand) assert game.is_finished == False game.player_move('hit') print('new player hand: ', game.player_hands) assert game.is_finished == True print('final dealer hand: ', game.dealer_hand) assert game.player_hands[0].cards == ['Q', '6', '5'] assert game.dealer_hand.cards == ['10', 'K'] assert game.result == 1
def test_Game_full_win_2(): # player hits to hard 18, dealer hits to hard 17, player wins # initial player hand [Q 10], total: 20 # initial dealer hand [A 9], total: 20 seeded_deck = blackjack_engine.Deck(seed=7) game = blackjack_engine.BlackjackGame(deck=seeded_deck) print('initial player hand', game.player_hands[0]) print('initial dealer hand', game.dealer_hand) assert game.is_finished == False game.player_move('hit') print('player hand 2: ', game.player_hands) game.player_move('hit') print('player hand 3: ', game.player_hands) game.player_move('stay') assert game.is_finished == True print('final dealer hand: ', game.dealer_hand) assert game.player_hands[0].cards == ['5', 'A', '2', 'Q'] assert game.dealer_hand.cards == ['6', 'J', 'A'] assert game.result == 1