From d7fb5e161d84c12b99227e16ad150af92ae90c96 Mon Sep 17 00:00:00 2001 From: Karol Idaszak Date: Tue, 31 Jan 2023 20:51:34 +0100 Subject: [PATCH] Replace prints with show_game_state function Co-authored-by: karoel2 Reviewed-on: https://git.wmi.amu.edu.pl/s444383/blackjack-fuzzy/pulls/2 --- cards.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/cards.py b/cards.py index 14260b5..184decc 100644 --- a/cards.py +++ b/cards.py @@ -1,4 +1,3 @@ -# import itertools import random @@ -18,7 +17,6 @@ def shoe(n_of_decks: int = 1) -> list: for _ in range(n_of_decks): for _ in suits: deck.extend(vals) - # deck.extend(itertools.product(vals, suits)) random.shuffle(deck) return deck @@ -101,8 +99,22 @@ def has_blackjack_occured(hand: list): return True return False +def show_game_state(player_hand: list, dealer_hand: list, decision: str='') -> None: + """Print dealer and player card values and player decision -def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[], bet: int =10) -> str: + Args: + player_hand (list): List of cards in player hand + dealer_hand (list): List of cards in dealer hand + decision (str, optional): Player decision. Will not be printed if defaults to ''. + """ + if decision and cards_eval(player_hand)[0] > 21: + print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} fail") + elif decision: + print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} decision: {decision}") + elif not cards_eval(player_hand)[0] > 21: + 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: """Single blackjack round Args: @@ -121,11 +133,7 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[], bet: int decision = '' while decision != 'stand' or decision != 'surrender': decision = AI(player_hand, face_up) - if cards_eval(player_hand)[0] > 21: - print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} fail") - else: - print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)} decision: {decision}") - + show_game_state(player_hand, dealer_hand, decision) if decision == 'hit': player_hand.append(next(shoe)) elif decision == 'double down': @@ -149,12 +157,9 @@ def blackjack(shoe: iter, dealer_hand: list=[], player_hand: list =[], bet: int #dealer turn dealer_value = dealer(dealer_hand, shoe) player_value = max(cards_eval(player_hand)) - # print(dealer_value, player_value) #debug - - if not cards_eval(player_hand)[0] > 21: - print(f"dealer: {cards_eval(dealer_hand)} player: {cards_eval(player_hand)}") - + show_game_state(player_hand, dealer_hand) + if player_value == 21 and has_blackjack_occured(player_hand): return 'player blackjack', bet elif player_value > 21: