Looking for feed back on my logic and though processes.

Posted by Wheniseeipee@reddit | learnprogramming | View on Reddit | 6 comments

Hello! I am curious about something that I started thinking about during my first capstone that I have posted below. I have finished the actual project but I realized I am having a hard time gauging how readable the code is. I know that I can understand it but I'm also the person that wrote it. I looked at my instructors solution and it's a bit different than mine. I understand the solution but I am wondering why I approached it differently. I compared mine and my instructors flow charts and they were pretty similar. The execution of the actual logic though was different. Can you understand my thought process? I fee like it's important to be able to get into the head of the person who's code you're reading in order to understand it, at least to a degree. I am just worried that mine is closed off and would be hard to read for someone else. I am happy I got it to work but Idk I feel like that's only half the battle. Idk maybe I'm just anxiety yapping. Sorry if this doesn't make sense as a concern lol oh this is python btw.

import random

import art

#Function that checks who wins the game at the end if the player selects "n"

def check_who_wins(computer_score_to_check, player_score_to_check):

'''Function that compares the final score of the computer and player as well as checks for computer bust'''

player_final_result = sum(player_score_to_check)
    computer_final_result = sum(computer_score_to_check)


    print(f" Your final hand: {player_score_to_check}, final score: {player_final_result}")
    print(f" Computer's final hand: {computer_score_to_check}, final score: {computer_final_result}")

    if computer_final_result > 21:
        print("The computer went over! You win! 🤭")

    elif computer_final_result > player_final_result:
        print("You lose! 😭")
    elif computer_final_result == player_final_result:
        print("You Draw! 🫢")
    else:
        print("You Win! 🤭")


#Logic that launches after the player is finished getting their cards. Once the #player stays this logic launches.

def computer_logic(c_card, card_list):

'''Computer will draw until above 17 and also logic is written that
    tells the program to change Ace from 11 to 1 if it saves computer from busting'''



computer_draw = True

    while computer_draw:

        if sum(c_card) < 17:
            new_computer_card = (random.choice(card_list))
            c_card.append(new_computer_card)
            if sum(c_card) > 21 and new_computer_card == 11:
                c_card.remove(11)
                c_card.append(1)
                print("Ace card changed to 1")

        elif sum(c_card) >= 17:
            computer_draw = False

#Checks to see if the player busts when called

def check_for_player_bust(p_card, c_card):


'''Checks to see if the player went over 21 after each new card
    is dealt until the player stays. Returns the value true if the player busts'''

p_score = sum(p_card)

    if p_score > 21:

        print(f"   Your final hand: {p_card}, final score: {p_score}")
        print(f"   Computer's final hand: {c_card}, final score: {sum(c_card)}")
        print("You went over! You lose :(")
        return True


#Launches the game when called

def black_jack():


'''Core game logic will repeat until the user inputs "n" contains logic that check for blackjack and checks to see if player busted at the start
    also contains all the functions for checking for player bust and who wins. '''


deck_of_cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

    while input("Do you want to play a game of Blackjack? type 'y' or 'n': ").lower() == "y":

        print("\n" * 50)
        print(art.logo)
#Sets up the starting deck for the player and the computer
        player_cards = [random.choice(deck_of_cards), random.choice(deck_of_cards)]

        computers_cards = [random.choice(deck_of_cards), random.choice(deck_of_cards)]

        player_score = sum(player_cards)

#prints both the players cards
        print(f"  Your cards: {player_cards}, Current score: {player_score}")
        computers_first_card = computers_cards[0]

        if player_score > 21:
            continue
        elif player_score == 21 and len(player_cards) == 2:
            print("You got blackjack! You win!")
            continue

#prints computers card so you can see the first card the computer was dealt
        print(f"  Computer's first card: {computers_first_card}")

        hit = True

#Loop that runs until the player is done hitting then the computer starts it's #turn if another_card == 'n'

        while hit:

            another_card = input("Type 'y' to get another card, type 'n' to pass: ")

            if another_card == "y":

                new_card = random.choice(deck_of_cards)
                player_cards.append(new_card)
                if sum(player_cards) > 21 and 11 == new_card:
                    player_cards.remove(11)
                    player_cards.append(1)
                    print("Ace card changed to 1")



                print(f"   Your cards: {player_cards}, Current score: {sum(player_cards)}")
                print(f"   Computer's first card: {computers_first_card}")

                if check_for_player_bust(p_card = player_cards, c_card = computers_cards):
                    hit = False

            else:
                hit = False
                computer_logic(c_card= computers_cards, card_list= deck_of_cards)
                check_who_wins(computer_score_to_check = computers_cards, player_score_to_check = player_cards)

    print("You selected n, goodbye")

#Calls the function so the game can begin.
black_jack()