Added cards value and game results logging

This commit is contained in:
Andrzej Preibisz 2023-01-18 14:13:01 +01:00
parent 8a4c81f988
commit 22967327e1

View File

@ -23,7 +23,7 @@ def shoe(n_of_decks: int = 1) -> list:
random.shuffle(deck) random.shuffle(deck)
return 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 """Evaluate hand value. Will return two values if there is an ace
in the hand and both values are below 21. in the hand and both values are below 21.
@ -35,6 +35,7 @@ def cards_eval(hand: list) -> list:
""" """
evaluation = [0, 0] evaluation = [0, 0]
for value in hand: for value in hand:
print(f"{agent} : {value}")
if value in ['jack', 'queen', 'king']: if value in ['jack', 'queen', 'king']:
evaluation[0] += 10 evaluation[0] += 10
evaluation[1] += 10 evaluation[1] += 10
@ -61,10 +62,10 @@ def dealer(hand: list, shoe: iter) -> int:
Returns: Returns:
int: dealer hand value int: dealer hand value
""" """
evaluation = cards_eval(hand) evaluation = cards_eval(hand, "dealer")
while max(evaluation) <= 17: #solve soft 17 while max(evaluation) <= 17: #solve soft 17
hand.append(next(shoe)) hand.append(next(shoe))
evaluation = cards_eval(hand) evaluation = cards_eval(hand, "dealer")
return max(evaluation) return max(evaluation)
def AI(hand: list, face_up: str) -> str: def AI(hand: list, face_up: str) -> str:
@ -83,7 +84,7 @@ def AI(hand: list, face_up: str) -> str:
# if face_up == 'ace': # if face_up == 'ace':
# return 'surrender' # return 'surrender'
# else: # else:
evaluation = cards_eval(hand) evaluation = cards_eval(hand, "player")
if max(evaluation) <= 17: if max(evaluation) <= 17:
return 'hit' return 'hit'
else: else:
@ -109,6 +110,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
decision = '' decision = ''
while decision != 'stand' or decision != 'surrender': while decision != 'stand' or decision != 'surrender':
decision = AI(player_hand, face_up) decision = AI(player_hand, face_up)
print(f"Decision: {decision}")
if decision == 'hit': if decision == 'hit':
player_hand.append(next(shoe)) player_hand.append(next(shoe))
elif decision == 'double down': elif decision == 'double down':
@ -130,10 +132,12 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
#dealer turn #dealer turn
dealer_value = dealer(dealer_hand, shoe) 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 # print(dealer_value, player_value) #debug
#round end #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:
return 'dealer win' return 'dealer win'
elif dealer_value > 21: elif dealer_value > 21:
@ -162,6 +166,9 @@ def game_loop() -> None:
# elif result == 'push': # elif result == 'push':
# stats[0] += 0 # stats[0] += 0
# stats[1] += 1 # stats[1] += 1
if result in ['player win', 'dealer win']:
print(result)
print("===="*10)
return stats return stats
# if __name__ == '__main__': # if __name__ == '__main__':
@ -172,7 +179,7 @@ if __name__ == '__main__':
import time import time
#don't use time.time() for counting code execution time! #don't use time.time() for counting code execution time!
start = time.perf_counter() start = time.perf_counter()
for i in range(1000): for i in range(100):
wins, loses = game_loop() wins, loses = game_loop()
statistics[0] += wins statistics[0] += wins
statistics[1] += loses statistics[1] += loses