This commit is contained in:
Andrzej Preibisz 2023-01-26 01:09:02 +01:00
parent 22967327e1
commit 298d239b84

View File

@ -91,6 +91,15 @@ def AI(hand: list, face_up: str) -> str:
return 'stand' return 'stand'
### temp ### 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: def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
"""Single blackjack round """Single blackjack round
@ -138,7 +147,9 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
#round end #round end
print(f"Dealer's deck value: {dealer_value}") print(f"Dealer's deck value: {dealer_value}")
print(f"Player's deck value: {player_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' return 'dealer win'
elif dealer_value > 21: elif dealer_value > 21:
return 'player win' return 'player win'
@ -150,42 +161,62 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
return 'dealer win' return 'dealer win'
#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(balance, bet) -> None:
stats = [0, 0] #wins, loses wins, losses, draws = 0, 0, 0
player_blackjack = 0
shoe_iter = iter(shoe(10)) shoe_iter = iter(shoe(10))
notable_results = ['player win', 'dealer win', 'player blackjack']
while True: while True:
#round start #round start
try: try:
balance -= bet
result = blackjack(shoe_iter) result = blackjack(shoe_iter)
except StopIteration: except StopIteration:
break break
if result == 'player win': 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': elif result == 'dealer win':
stats[1] += 1 losses += 1
# elif result == 'push': elif result == 'push':
# stats[0] += 0 balance += bet
# stats[1] += 1 draws += 1
if result in ['player win', 'dealer win']: if result in notable_results:
print(result) print(result)
print("===="*10) print("===="*10)
return stats return wins, losses, draws, balance, player_blackjack
def calculate_bet(wins, losses, hand):
pass
# if __name__ == '__main__': # if __name__ == '__main__':
# print(game_loop()) # print(game_loop())
if __name__ == '__main__': 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 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(100): for i in range(1000):
wins, loses = game_loop() wins, loses, draws, balance, p_blackjack = game_loop(balance, bet)
statistics[0] += wins total_wins += wins
statistics[1] += loses total_losses += loses
total_draws += draws
total_p_blackjack += p_blackjack
end = time.perf_counter() end = time.perf_counter()
result = end - start result = end - start
print(result) print(result)
print(statistics) print(f"Wins: {total_wins}, Losses: {total_losses}, Draws: {total_draws}")
print(statistics[0]/sum(statistics)) 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}")