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()
GlassCommission4916@reddit
How was your instructor's logic different?
The flow of your code is fine, but I see many bugs and the formatting leaves a lot to be desired (that might be from you having trouble copy pasting it into reddit though, since what you actually posted isn't valid python afaik).
Wheniseeipee@reddit (OP)
Yeah I posted this from my computer and it looked identical to what I have in python but looking at it on my phone now it doesn’t. An another commenter mentioned Input -> process -> output as a sort of guide and I think my instructors logic follows that more than mine. I have decided to code another version using my instructors hints that I didn’t use the first time and compare mine to the second one so I can see the differences. Also I’m gonna edit mine to try and fit that flow better
Dangerous-Rooster162@reddit
Your code is actually pretty readable! The logic flow makes sense and I can follow your thought process through the functions. You broke things down in logical chunks which is good practice
One thing that jumps out is the ace handling - you're doing it in two different places (player and computer logic) which could maybe be extracted into separate function to avoid repetition. Also some of the variable names could be shorter, like `computer_score_to_check` is bit verbose when just `computer_cards` would work fine
The structure is solid though, you separated concerns well with different functions for checking bust, computer logic, and final scoring. Your instructor probably just has different style or approach but that doesn't mean yours is wrong. As long as it works and someone else can understand what each part does, you're on right track
Don't stress too much about comparing to instructor's solution - there's usually multiple ways to solve same problem in programming. Your code tells story of how you thought through the problem which is what matters
Wheniseeipee@reddit (OP)
Thank you! I appreciate your feedback, I am glad it seems more readable than I thought. The formatting came out weirder on Reddit. Also yeah I think I was running out of ways to say the same things with all the functions. I am gonna re-examine that to make it clearer.
aqua_regis@reddit
In programming, there usually are several ways to achieve the same goal and depending on how much or little detail the original flow chart included, the actual implementation can be quite different.
Also, experience plays a huge role in implementation. A fairly inexperienced programmer will definitely come up with different solutions than an experienced one.
Try to figure out the differences between your and your instructor's implementations and work out their advantages and disadvantages.
It would be interesting to see the solution of your instructor as well.
The code in general is readable (albeit some indentation got lost when posting here).
Yet, there are a few positions where your code comes quite unexpected, e.g.
in the main loop - this should be in its own winner/loser detection function.
Another thing that somewhat puts me off is that you have output (
printstatements) scattered all over your program and with that no clear and easy to follow path through.In general, it is preferable to have a clear input section, followed by processing, followed by output. Functions should rarely directly produce output and generally better return values that then can be handled by the main routine. This has the advantage that if you later change from console to GUI you can reuse your functions - the actual business logic without having to rewrite everything.
What is positive is that you tried to break functionality down into individual functions, but it seems that you didn't really plan the structure out well. This is not a problem as such since the program works; it rather attributes to inexperience.
Don't know whether the assignment called for it, or not, but your
deck_of_cardsand your selections from it are quite strange since you do not remove any card from the deck. In a traditional Black Jack game, you have a sled of several (typically 3 to 5) Poker Decks, each deck consisting of the classic 52 (4 suits x 13 ranks) cards. You only handle a single suit (which is generally okay for Black Jack as the suit is irrelevant), but do not multiply for a full deck, nor are you removing already drawn cards.In a realistic Black Jack game, you'd create the full deck or even a full sled, shuffle it, and then only pull the top card, removing it from the deck/sled.
In a realistic version a "card" (as an object) travels from the deck/sled to the player's (or computer's) hand. It only exists once and changes "ownership" in the process of a game round.
Wheniseeipee@reddit (OP)
Yeah after reading your comment I can see why I should have set up a return inside a function for that if and elif I can put that in the check who wins function I’m pretty sure.
I think you’re right I made a flow chart but maybe I didn’t internalize the steps as well as I thought I did. Also none of my comments are formatted like that idk why Reddit did that.
Also no the assignment didn’t call for removing the cards from the deck as they were dealt. The instructor specifically left that out. I’m gonna try reformatting with the input -> process -> output concept in mind but right now I’m gonna crash lol thank you for your advice!