diff --git a/cards.py b/cards.py index 0fafa07..1166696 100644 --- a/cards.py +++ b/cards.py @@ -91,6 +91,15 @@ def AI(hand: list, face_up: str) -> str: return 'stand' ### temp +def has_blackjack_occured(hand: list): + """Method assumes that hand value == 21 """ + if len(hand) != 2: + return False + if "ace" in hand and any([value in hand for value in ['jack', 'queen', 'king']]): + return True + return False + + def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: """Single blackjack round @@ -138,7 +147,9 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: #round end print(f"Dealer's deck value: {dealer_value}") print(f"Player's deck value: {player_value}") - if player_value > 21: + if player_value == 21 and has_blackjack_occured(player_hand): + return 'player blackjack' + elif player_value > 21: return 'dealer win' elif dealer_value > 21: return 'player win' @@ -150,42 +161,62 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: return 'dealer win' #TODO: add adidtional return with wager value like +10$ or -20$ -def game_loop() -> None: - stats = [0, 0] #wins, loses +def game_loop(balance, bet) -> None: + wins, losses, draws = 0, 0, 0 + player_blackjack = 0 + shoe_iter = iter(shoe(10)) + notable_results = ['player win', 'dealer win', 'player blackjack'] while True: #round start try: + balance -= bet result = blackjack(shoe_iter) except StopIteration: break if result == 'player win': - stats[0] += 1 + wins += 1 + balance += (2*bet) + elif result == 'player blackjack': + wins += 1 + player_blackjack += 1 + balance += (2*bet) + (bet/2) elif result == 'dealer win': - stats[1] += 1 - # elif result == 'push': - # stats[0] += 0 - # stats[1] += 1 - if result in ['player win', 'dealer win']: + losses += 1 + elif result == 'push': + balance += bet + draws += 1 + if result in notable_results: print(result) print("===="*10) - return stats + return wins, losses, draws, balance, player_blackjack + +def calculate_bet(wins, losses, hand): + pass # if __name__ == '__main__': # print(game_loop()) if __name__ == '__main__': - statistics = [0, 0] + total_wins, total_losses, total_draws = 0, 0, 0 + total_p_blackjack = 0 + balance = 0 + bet = 10 + import time #don't use time.time() for counting code execution time! start = time.perf_counter() - for i in range(100): - wins, loses = game_loop() - statistics[0] += wins - statistics[1] += loses + for i in range(1000): + wins, loses, draws, balance, p_blackjack = game_loop(balance, bet) + total_wins += wins + total_losses += loses + total_draws += draws + total_p_blackjack += p_blackjack end = time.perf_counter() result = end - start print(result) - print(statistics) - print(statistics[0]/sum(statistics)) + print(f"Wins: {total_wins}, Losses: {total_losses}, Draws: {total_draws}") + print(f"Wins/Losses ratio: {total_wins/sum([total_wins, total_losses])}") + print(f"Balance: {balance}") + print(f"Times player hit blackjack: {total_p_blackjack}")