Merge branch 'ints'

This commit is contained in:
karoel2 2023-01-31 22:06:50 +01:00
commit 9b63de3d22

View File

@ -85,19 +85,24 @@ def AI(hand: list, face_up: str, losses_in_row: int) -> str:
return 'stand' return 'stand'
### temp ### temp
def show_game_state(player_hand: list, dealer_hand: list, decision: str='') -> None: def show_game_state(player_hand: list, dealer_hand: list, splited: bool, decision: str='') -> None:
"""Print dealer and player card values and player decision """Print dealer and player card values and player decision
Args: Args:
player_hand (list): List of cards in player hand player_hand (list): List of cards in player hand
dealer_hand (list): List of cards in dealer hand dealer_hand (list): List of cards in dealer hand
splited (bool): True if it is subprocess by spliting
decision (str, optional): Player decision. Will not be printed if defaults to ''. decision (str, optional): Player decision. Will not be printed if defaults to ''.
""" """
if decision and cards_eval(player_hand)[0] > 21: if decision and cards_eval(player_hand)[0] > 21:
print("split| " if splited else '', end='')
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")
elif decision: elif decision:
print("split| " if splited else '', end='')
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}")
elif not cards_eval(player_hand)[0] > 21: elif not cards_eval(player_hand)[0] > 21:
print("split| " if splited else '', end='')
print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)}") print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)}")
def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list=[], bet: int=10, losses_in_row: int=0) -> str: def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list=[], bet: int=10, losses_in_row: int=0) -> str:
@ -112,52 +117,58 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list=[], bet: int=1
Returns: Returns:
str: game result str: game result
""" """
splited = False
if dealer_hand == [] and player_hand == []: if dealer_hand == [] and player_hand == []:
dealer_hand = [next(shoe), next(shoe)] dealer_hand = [next(shoe), next(shoe)]
player_hand = [next(shoe), next(shoe)] player_hand = [next(shoe), next(shoe)]
else:
splited = True
#dealer turn
face_up = dealer_hand[0] face_up = dealer_hand[0]
dealer_value = dealer(dealer_hand, shoe)
decision = '' decision = ''
while decision != 'stand' or decision != 'surrender': while decision != 'stand' or decision != 'surrender':
decision = AI(player_hand, face_up, losses_in_row) decision = AI(player_hand, face_up, losses_in_row)
show_game_state(player_hand, dealer_hand, decision) show_game_state(player_hand, [face_up], splited, 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 bet *= 2
player_hand.append(next(shoe)) player_hand.append(next(shoe))
elif decision == 'split': elif decision == 'split':
#this wont work! #this work reccursevly
#it will need to player_hand = [player_hand[0]]
# dealer_value = dealer(dealer_hand, shoe) splited_ai = True #temp
#be calculated before and passed to blackjack() blackjack(shoe, dealer_hand, player_hand.copy(), bet)#new wager start
#so both wager have the same dealer_value!
##this will work reccursevly
player_hand = player_hand[0]
blackjack(shoe, dealer_hand, player_hand, bet)#new wager start
#old wager continue #old wager continue
elif decision == 'surrender': elif decision == 'surrender':
break break
elif decision == 'stand': elif decision == 'stand':
break break
#dealer turn
dealer_value = dealer(dealer_hand, shoe)
player_value = max(cards_eval(player_hand)) player_value = max(cards_eval(player_hand))
show_game_state(player_hand, dealer_hand) show_game_state(player_hand, dealer_hand, splited)
print("split| " if splited else '', end='')
if player_value == 21 and 1 in player_hand: if player_value == 21 and 1 in player_hand:
return 'player blackjack', bet print('player blackjack', "="*50, sep='\n')
return 'player blackjack', bet
elif player_value > 21: elif player_value > 21:
print('dealer win', "="*50, sep='\n')
return 'dealer win', bet return 'dealer win', bet
elif dealer_value > 21: elif dealer_value > 21:
print('player win', "="*50, sep='\n')
return 'player win', bet return 'player win', bet
elif player_value > dealer_value: elif player_value > dealer_value:
print('player win', "="*50, sep='\n')
return 'player win', bet return 'player win', bet
elif player_value == dealer_value: elif player_value == dealer_value:
print('push', "="*50, sep='\n')
return 'push', bet #keep money, no win no lose 0$ return 'push', bet #keep money, no win no lose 0$
else: else:
print('dealer win', "="*50, sep='\n')
return 'dealer win', bet return 'dealer win', bet
def game_loop(balance, bet) -> None: def game_loop(balance, bet) -> None:
@ -191,11 +202,6 @@ def game_loop(balance, bet) -> None:
elif result == 'push': elif result == 'push':
balance += game_bet balance += game_bet
draws += 1 draws += 1
losses_in_row = 0
if result in notable_results:
print(result)
print("="*50)
return wins, losses, draws, balance #player_blackjack return wins, losses, draws, balance #player_blackjack
def calculate_bet(wins, losses, hand): def calculate_bet(wins, losses, hand):