From 22967327e1057e31c8edfab255bf595bcf15ae18 Mon Sep 17 00:00:00 2001 From: Andrzej Preibisz Date: Wed, 18 Jan 2023 14:13:01 +0100 Subject: [PATCH 1/3] Added cards value and game results logging --- cards.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/cards.py b/cards.py index 2645f7b..0fafa07 100644 --- a/cards.py +++ b/cards.py @@ -23,7 +23,7 @@ def shoe(n_of_decks: int = 1) -> list: random.shuffle(deck) return deck -def cards_eval(hand: list) -> list: +def cards_eval(hand: list, agent: str) -> list: """Evaluate hand value. Will return two values if there is an ace in the hand and both values are below 21. @@ -35,6 +35,7 @@ def cards_eval(hand: list) -> list: """ evaluation = [0, 0] for value in hand: + print(f"{agent} : {value}") if value in ['jack', 'queen', 'king']: evaluation[0] += 10 evaluation[1] += 10 @@ -61,10 +62,10 @@ def dealer(hand: list, shoe: iter) -> int: Returns: int: dealer hand value """ - evaluation = cards_eval(hand) + evaluation = cards_eval(hand, "dealer") while max(evaluation) <= 17: #solve soft 17 hand.append(next(shoe)) - evaluation = cards_eval(hand) + evaluation = cards_eval(hand, "dealer") return max(evaluation) def AI(hand: list, face_up: str) -> str: @@ -83,7 +84,7 @@ def AI(hand: list, face_up: str) -> str: # if face_up == 'ace': # return 'surrender' # else: - evaluation = cards_eval(hand) + evaluation = cards_eval(hand, "player") if max(evaluation) <= 17: return 'hit' else: @@ -109,6 +110,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: decision = '' while decision != 'stand' or decision != 'surrender': decision = AI(player_hand, face_up) + print(f"Decision: {decision}") if decision == 'hit': player_hand.append(next(shoe)) elif decision == 'double down': @@ -130,10 +132,12 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: #dealer turn dealer_value = dealer(dealer_hand, shoe) - player_value = max(cards_eval(player_hand)) + player_value = max(cards_eval(player_hand, "player")) # print(dealer_value, player_value) #debug #round end + print(f"Dealer's deck value: {dealer_value}") + print(f"Player's deck value: {player_value}") if player_value > 21: return 'dealer win' elif dealer_value > 21: @@ -162,6 +166,9 @@ def game_loop() -> None: # elif result == 'push': # stats[0] += 0 # stats[1] += 1 + if result in ['player win', 'dealer win']: + print(result) + print("===="*10) return stats # if __name__ == '__main__': @@ -172,7 +179,7 @@ if __name__ == '__main__': import time #don't use time.time() for counting code execution time! start = time.perf_counter() - for i in range(1000): + for i in range(100): wins, loses = game_loop() statistics[0] += wins statistics[1] += loses @@ -181,4 +188,4 @@ if __name__ == '__main__': result = end - start print(result) print(statistics) - print(statistics[0]/sum(statistics)) \ No newline at end of file + print(statistics[0]/sum(statistics)) From 298d239b8443c4506925cdb89f8a7247f3f5f9c9 Mon Sep 17 00:00:00 2001 From: Andrzej Preibisz Date: Thu, 26 Jan 2023 01:09:02 +0100 Subject: [PATCH 2/3] Bets WIP --- cards.py | 65 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 17 deletions(-) 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}") From d7bae269a469dca7dd2a94bb6f9835c1e9f6a945 Mon Sep 17 00:00:00 2001 From: Andrzej Preibisz Date: Tue, 31 Jan 2023 00:42:14 +0100 Subject: [PATCH 3/3] Betting system fixes --- cards.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/cards.py b/cards.py index 1166696..4cc9bbf 100644 --- a/cards.py +++ b/cards.py @@ -85,6 +85,8 @@ def AI(hand: list, face_up: str) -> str: # return 'surrender' # else: evaluation = cards_eval(hand, "player") + if max(evaluation) == 11: + return 'double down' if max(evaluation) <= 17: return 'hit' else: @@ -100,7 +102,7 @@ def has_blackjack_occured(hand: list): return False -def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: +def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[], bet: int =10) -> str: """Single blackjack round Args: @@ -123,6 +125,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: if decision == 'hit': player_hand.append(next(shoe)) elif decision == 'double down': + bet *= 2 player_hand.append(next(shoe)) elif decision == 'split': #this wont work! @@ -132,7 +135,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: #so both wager have the same dealer_value! ##this will work reccursevly player_hand = player_hand[0] - blackjack(shoe, dealer_hand, player_hand)#new wager start + blackjack(shoe, dealer_hand, player_hand, bet)#new wager start #old wager continue elif decision == 'surrender': break @@ -148,17 +151,17 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: print(f"Dealer's deck value: {dealer_value}") print(f"Player's deck value: {player_value}") if player_value == 21 and has_blackjack_occured(player_hand): - return 'player blackjack' + return 'player blackjack', bet elif player_value > 21: - return 'dealer win' + return 'dealer win', bet elif dealer_value > 21: - return 'player win' + return 'player win', bet elif player_value > dealer_value: - return 'player win' + return 'player win', bet elif player_value == dealer_value: - return 'push' #keep money, no win no lose 0$ + return 'push', bet #keep money, no win no lose 0$ else: - return 'dealer win' + return 'dealer win', bet #TODO: add adidtional return with wager value like +10$ or -20$ def game_loop(balance, bet) -> None: @@ -171,20 +174,20 @@ def game_loop(balance, bet) -> None: #round start try: balance -= bet - result = blackjack(shoe_iter) + result, game_bet = blackjack(shoe_iter, bet=bet) except StopIteration: break if result == 'player win': wins += 1 - balance += (2*bet) + balance += (2*game_bet) elif result == 'player blackjack': wins += 1 player_blackjack += 1 - balance += (2*bet) + (bet/2) + balance += (2*game_bet) + (game_bet/2) elif result == 'dealer win': losses += 1 elif result == 'push': - balance += bet + balance += game_bet draws += 1 if result in notable_results: print(result) @@ -206,7 +209,7 @@ if __name__ == '__main__': import time #don't use time.time() for counting code execution time! start = time.perf_counter() - for i in range(1000): + for i in range(100): wins, loses, draws, balance, p_blackjack = game_loop(balance, bet) total_wins += wins total_losses += loses