add card value prints

This commit is contained in:
karoel2 2023-01-31 19:54:38 +01:00
parent 8a4c81f988
commit dad56d9e7a

View File

@ -90,7 +90,7 @@ def AI(hand: list, face_up: str) -> str:
return 'stand' return 'stand'
### temp ### temp
def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str: def blackjack(shoe: iter, money:int, dealer_hand: list=[], player_hand: list =[]) -> str:
"""Single blackjack round """Single blackjack round
Args: Args:
@ -109,6 +109,10 @@ 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)
if cards_eval(player_hand)[0] > 21:\
print(f"dealer: {cards_eval(dealer_hand)}, player: {cards_eval(player_hand)}, fail")
else:
print(f"dealer: {cards_eval(dealer_hand)}, player: {cards_eval(player_hand)}, 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':
@ -135,24 +139,25 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
#round end #round end
if player_value > 21: if player_value > 21:
return 'dealer win' return 'dealer win', money
elif dealer_value > 21: elif dealer_value > 21:
return 'player win' return 'player win', money
elif player_value > dealer_value: elif player_value > dealer_value:
return 'player win' return 'player win', money
elif player_value == dealer_value: elif player_value == dealer_value:
return 'push' #keep money, no win no lose 0$ return 'push', money #keep money, no win no lose 0$
else: else:
return 'dealer win' return 'dealer win', money
#TODO: add adidtional return with wager value like +10$ or -20$ #TODO: add adidtional return with wager value like +10$ or -20$
def game_loop() -> None: def game_loop() -> None:
stats = [0, 0] #wins, loses stats = [0, 0] #wins, loses
money = 500 #100$ start money
shoe_iter = iter(shoe(10)) shoe_iter = iter(shoe(10))
while True: while True:
#round start #round start
try: try:
result = blackjack(shoe_iter) result, money = blackjack(shoe_iter, money)
except StopIteration: except StopIteration:
break break
if result == 'player win': if result == 'player win':
@ -162,6 +167,8 @@ def game_loop() -> None:
# elif result == 'push': # elif result == 'push':
# stats[0] += 0 # stats[0] += 0
# stats[1] += 1 # stats[1] += 1
print("---------------------")
stats.append(money)
return stats return stats
# if __name__ == '__main__': # if __name__ == '__main__':
@ -169,16 +176,19 @@ def game_loop() -> None:
if __name__ == '__main__': if __name__ == '__main__':
statistics = [0, 0] statistics = [0, 0]
money_sum = 0
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(1000):
wins, loses = game_loop() wins, loses, money = game_loop()
statistics[0] += wins statistics[0] += wins
statistics[1] += loses statistics[1] += loses
money_sum += money
end = time.perf_counter() end = time.perf_counter()
result = end - start result = end - start
print(result) print(f'time: {round(result, 3)} seconds')
print(statistics) print(f'wins: {statistics[0]} | losses: {statistics[1]}')
print(statistics[0]/sum(statistics)) print(f'win {round((statistics[0]/sum(statistics)*100), 2)}%')
print(f'moeny return {round((money_sum/500)*100, 2)}%')