Added cards value and game results logging
This commit is contained in:
parent
8a4c81f988
commit
22967327e1
21
cards.py
21
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))
|
||||
print(statistics[0]/sum(statistics))
|
||||
|
Loading…
Reference in New Issue
Block a user