From 4a5f35dc64dab36e55f9c4d5e8daa4c34da8979e Mon Sep 17 00:00:00 2001 From: karoel2 Date: Tue, 31 Jan 2023 21:58:39 +0100 Subject: [PATCH] Add splits --- cards.py | 54 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/cards.py b/cards.py index 48ad438..c870233 100644 --- a/cards.py +++ b/cards.py @@ -58,7 +58,7 @@ def dealer(hand: list, shoe: iter) -> int: evaluation = cards_eval(hand) return max(evaluation) -def AI(hand: list, face_up: str) -> str: +def AI(hand: list, face_up: str, splited) -> str: #TODO: add fuzzy logic """Fuzzy AI possible player decision: @@ -75,6 +75,8 @@ def AI(hand: list, face_up: str) -> str: # return 'surrender' # else: evaluation = cards_eval(hand) + if len(hand) == 2 and hand[0] == hand[1] and not splited: + return 'split' if max(evaluation) == 11: return 'double down' if max(evaluation) <= 17: @@ -83,19 +85,24 @@ def AI(hand: list, face_up: str) -> str: return 'stand' ### 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 Args: player_hand (list): List of cards in player 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 ''. + """ 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") elif decision: + print("split| " if splited else '', end='') print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} decision: {decision}") 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)}") def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list=[], bet: int=10) -> str: @@ -109,52 +116,60 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list=[], bet: int=1 Returns: str: game result """ + splited = False + splited_ai = False #temp if dealer_hand == [] and player_hand == []: dealer_hand = [next(shoe), next(shoe)] player_hand = [next(shoe), next(shoe)] + else: + splited = True + splited_ai = True #temp + + #dealer turn face_up = dealer_hand[0] - + dealer_value = dealer(dealer_hand, shoe) + decision = '' while decision != 'stand' or decision != 'surrender': - decision = AI(player_hand, face_up) - show_game_state(player_hand, dealer_hand, decision) + decision = AI(player_hand, face_up, splited_ai) + show_game_state(player_hand, [face_up], splited, decision) if decision == 'hit': player_hand.append(next(shoe)) elif decision == 'double down': bet *= 2 player_hand.append(next(shoe)) elif decision == 'split': - #this wont work! - #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] - blackjack(shoe, dealer_hand, player_hand, bet)#new wager start + #this work reccursevly + player_hand = [player_hand[0]] + splited_ai = True #temp + blackjack(shoe, dealer_hand, player_hand.copy(), bet)#new wager start #old wager continue elif decision == 'surrender': break elif decision == 'stand': break - #dealer turn - dealer_value = dealer(dealer_hand, shoe) 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: - return 'player blackjack', bet + print('player blackjack', "="*50, sep='\n') + return 'player blackjack', bet elif player_value > 21: + print('dealer win', "="*50, sep='\n') return 'dealer win', bet elif dealer_value > 21: + print('player win', "="*50, sep='\n') return 'player win', bet elif player_value > dealer_value: + print('player win', "="*50, sep='\n') return 'player win', bet elif player_value == dealer_value: + print('push', "="*50, sep='\n') return 'push', bet #keep money, no win no lose 0$ else: + print('dealer win', "="*50, sep='\n') return 'dealer win', bet def game_loop(balance, bet) -> None: @@ -182,9 +197,6 @@ def game_loop(balance, bet) -> None: elif result == 'push': balance += game_bet draws += 1 - if result in notable_results: - print(result) - print("="*50) return wins, losses, draws, balance #player_blackjack def calculate_bet(wins, losses, hand):