This commit is contained in:
karoel2 2023-01-31 20:24:47 +01:00
commit 2498837c55

123
cards.py
View File

@ -84,13 +84,25 @@ def AI(hand: list, face_up: str) -> str:
# return 'surrender' # return 'surrender'
# else: # else:
evaluation = cards_eval(hand) evaluation = cards_eval(hand)
if max(evaluation) == 11:
return 'double down'
if max(evaluation) <= 17: if max(evaluation) <= 17:
return 'hit' return 'hit'
else: else:
return 'stand' return 'stand'
### temp ### temp
def blackjack(shoe: iter, money:int, dealer_hand: list=[], player_hand: list =[]) -> str:
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 =[], bet: int =10) -> str:
"""Single blackjack round """Single blackjack round
Args: Args:
@ -109,13 +121,15 @@ def blackjack(shoe: iter, money:int, dealer_hand: list=[], player_hand: list =[]
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:\ if cards_eval(player_hand)[0] > 21:
print(f"dealer: {cards_eval(dealer_hand)}, player: {cards_eval(player_hand)}, fail") print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} fail")
else: else:
print(f"dealer: {cards_eval(dealer_hand)}, player: {cards_eval(player_hand)}, decision: {decision}") 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':
bet *= 2
player_hand.append(next(shoe)) player_hand.append(next(shoe))
elif decision == 'split': elif decision == 'split':
#this wont work! #this wont work!
@ -125,7 +139,7 @@ def blackjack(shoe: iter, money:int, dealer_hand: list=[], player_hand: list =[]
#so both wager have the same dealer_value! #so both wager have the same dealer_value!
##this will work reccursevly ##this will work reccursevly
player_hand = player_hand[0] player_hand = player_hand[0]
blackjack(shoe, dealer_hand, player_hand)#new wager start blackjack(shoe, dealer_hand, player_hand, bet)#new wager start
#old wager continue #old wager continue
elif decision == 'surrender': elif decision == 'surrender':
break break
@ -137,58 +151,99 @@ def blackjack(shoe: iter, money:int, dealer_hand: list=[], player_hand: list =[]
player_value = max(cards_eval(player_hand)) player_value = max(cards_eval(player_hand))
# print(dealer_value, player_value) #debug # print(dealer_value, player_value) #debug
#round end
if player_value > 21:
return 'dealer win', money
elif dealer_value > 21:
return 'player win', money
elif player_value > dealer_value:
return 'player win', money
elif player_value == dealer_value:
return 'push', money #keep money, no win no lose 0$
else:
return 'dealer win', money
#TODO: add adidtional return with wager value like +10$ or -20$
def game_loop() -> None: if not cards_eval(player_hand)[0] > 21:
stats = [0, 0] #wins, loses print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)}")
money = 500 #100$ start money
if player_value == 21 and has_blackjack_occured(player_hand):
return 'player blackjack', bet
elif player_value > 21:
return 'dealer win', bet
elif dealer_value > 21:
return 'player win', bet
elif player_value > dealer_value:
return 'player win', bet
elif player_value == dealer_value:
return 'push', bet #keep money, no win no lose 0$
else:
return 'dealer win', bet
def game_loop(balance, bet) -> None:
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:
result, money = blackjack(shoe_iter, money) balance -= bet
result, game_bet = blackjack(shoe_iter, bet=bet)
except StopIteration: except StopIteration:
break break
if result == 'player win': if result == 'player win':
stats[0] += 1 wins += 1
balance += (2*game_bet)
elif result == 'player blackjack':
wins += 1
# player_blackjack += 1
balance += (2*game_bet) + (game_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 += game_bet
# stats[1] += 1 draws += 1
print("---------------------") if result in notable_results:
stats.append(money) print(result)
return stats print("="*50)
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] statistics = [0, 0, 0]
money_sum = 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(10):
wins, loses, money = game_loop() wins, loses, draws, money = game_loop(0, 10)
statistics[0] += wins statistics[0] += wins
statistics[1] += loses statistics[1] += loses
statistics[2] += draws
money_sum += money money_sum += money
end = time.perf_counter() end = time.perf_counter()
result = end - start result = end - start
print(f'time: {round(result, 3)} seconds') print(f'time: {round(result, 3)} seconds')
print(f'wins: {statistics[0]} | losses: {statistics[1]}') print(f'wins: {statistics[0]} | losses: {statistics[1]} | draws: {statistics[2]}')
print(f'win {round((statistics[0]/sum(statistics)*100), 2)}%') print(f'win {round((statistics[0]/sum(statistics)*100), 2)}%')
print(f'moeny return {round((money_sum/500)*100, 2)}%') print(f'balance: {money_sum}')
# print(f'moeny return {round((money_sum)*100, 2)}%')
# total_wins, total_losses, total_draws = 0, 0, 0
# total_p_blackjack = 0
# balance = 0
# bet = 10
# import time
# #don't use time.time() for counting code execution time!
# start = time.perf_counter()
# for i in range(100):
# wins, loses, draws, balance, p_blackjack = game_loop(balance, bet)
# total_wins += wins
# total_losses += loses
# total_draws += draws
# total_p_blackjack += p_blackjack
# end = time.perf_counter()
# result = end - start
# print(result)
# print(f"Wins: {total_wins}, Losses: {total_losses}, Draws: {total_draws}")
# 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}")