Game logic fix

This commit is contained in:
karoel2 2023-01-10 12:15:53 +01:00
parent 086b2affcf
commit 5b44348bfa

View File

@ -62,7 +62,7 @@ def dealer(hand: list, shoe: iter) -> int:
int: dealer hand value int: dealer hand value
""" """
evaluation = cards_eval(hand) evaluation = cards_eval(hand)
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)
return max(evaluation) return max(evaluation)
@ -84,7 +84,7 @@ 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) < 18: if max(evaluation) <= 17:
return 'hit' return 'hit'
else: else:
return 'stand' return 'stand'
@ -114,8 +114,12 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
elif decision == 'double down': elif decision == 'double down':
player_hand.append(next(shoe)) player_hand.append(next(shoe))
elif decision == 'split': elif decision == 'split':
#test if works #this wont work!
#this will work reccursevly #it will need to
# dealer_value = dealer(dealer_hand, shoe)
#be calculated before and passed to blackjack()
#so both wager have the same dealer_value!
##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)#new wager start
#old wager continue #old wager continue
@ -130,7 +134,9 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
# print(dealer_value, player_value) #debug # print(dealer_value, player_value) #debug
#round end #round end
if dealer_value > 21: if player_value > 21:
return 'dealer win'
elif dealer_value > 21:
return 'player win' return 'player win'
elif player_value > dealer_value: elif player_value > dealer_value:
return 'player win' return 'player win'
@ -141,7 +147,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[]) -> str:
#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 stats = [0, 0] #wins, loses
shoe_iter = iter(shoe(10)) shoe_iter = iter(shoe(10))
while True: while True:
#round start #round start
@ -150,23 +156,29 @@ def game_loop() -> None:
except StopIteration: except StopIteration:
break break
if result == 'player win': if result == 'player win':
stats += 1 stats[0] += 1
elif result == 'dealer win': elif result == 'dealer win':
stats -= 1 stats[1] += 1
elif result == 'push': # elif result == 'push':
stats += 0 # stats[0] += 0
# stats[1] += 1
return stats return stats
if __name__ == '__main__':
print(game_loop())
# if __name__ == '__main__': # if __name__ == '__main__':
# import time # print(game_loop())
# #don't use time.time() for counting code execution time!
# start = time.perf_counter()
# for i in range(1000):
# game_loop()
# end = time.perf_counter() if __name__ == '__main__':
# result = end - start statistics = [0, 0]
# print(result) import time
#don't use time.time() for counting code execution time!
start = time.perf_counter()
for i in range(1000):
wins, loses = game_loop()
statistics[0] += wins
statistics[1] += loses
end = time.perf_counter()
result = end - start
print(result)
print(statistics)
print(statistics[0]/sum(statistics))