From 22967327e1057e31c8edfab255bf595bcf15ae18 Mon Sep 17 00:00:00 2001 From: Andrzej Preibisz Date: Wed, 18 Jan 2023 14:13:01 +0100 Subject: [PATCH] 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))