Fusion Coding Assignment

This is the first assignment of the fusion course. We as students are assigned to work with AI to create a full stack block of code in python with AI without the AI directly giving you the code. A flowchart was also requested to show my thinking process through creating the code.

Definition of some words

Python:
Python is a basic coding language that is able to create and proccess functions to run what you tell a computer to do, for example if you want to let it return “Hello World”. You can simply type this into your coding application(pycharm or other):

print(“Hello World”)

Flowcharts:
Flowcharts are basically a mind map that shows your thought process before you create your code. In my case it also constructs my questions I ask to Google gemini in order for it to help me.

My Flowchart

I started with a flowchart to plan out how I am going to ask Gemini the questions and also how my code is going to be constructed.

In my flowchart there are 3 big parts, the first part is defining the attributes a card, my plan was to use suits and ranks for cards and later on define the value of each rank. The second part is when I created the Deck with suits and ranks. in my flowchart I connected suits to term since there are only 4 different suits and they do not interfere with the outcome, but ranks on the other hand do, so I wanted to give them values too. The third part was how the game would function including shuffling, dealing, the ace count since it is a changing variable.

My code

My decision was to make a game of Black Jack using python

Under here is my complete block of code:

import random
class Card:
    def __init__(self, suit, rank, value):
        self.suit = suit
        self.rank = rank
        self.value = value

    def __str__(self):
        return f"{self.rank} of {self.suit}"
class Deck:
    def __init__(self):
        suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
        ranks = {
            "2": 2, "3": 3, "4": 4, "5": 5, "6": 6,
            "7": 7, "8": 8, "9": 9, "10": 10,
            "Jack": 10, "Queen": 10, "King": 10, "Ace": 11
        }
        self.cards = [Card(suit, rank, value) for suit in suits for rank, value in ranks.items()]
        random.shuffle(self.cards)
    def deal_card(self):
        return self.cards.pop()
class Hand:
    def __init__(self):
        self.cards = []
    def add_card(self, card):
        self.cards.append(card)
    def get_value(self):
        total = 0
        aces = 0
        for card in self.cards:
            total += card.value
            if card.rank == "Ace":
                aces += 1
        while total > 21 and aces:
            total -= 10
            aces -= 1
        return total
    def __str__(self):
        return ", ".join(str(card) for card in self.cards)
def play_blackjack():
    deck = Deck()
    player_hand = Hand()
    dealer_hand = Hand()
    player_hand.add_card(deck.deal_card())
    player_hand.add_card(deck.deal_card())
    dealer_hand.add_card(deck.deal_card())
    dealer_hand.add_card(deck.deal_card())
    while True:
        print(f"\nYour hand: {player_hand} (value: {player_hand.get_value()})")
        print(f"Dealer shows: {dealer_hand.cards[0]}")
        if player_hand.get_value() > 21:
            print("You bust! Dealer wins.")
            return
        choice = input("Do you want to (h)it or (s)tand? ").lower()
        if choice == "h":
            player_hand.add_card(deck.deal_card())
        elif choice == "s":
            break
    print(f"\nDealer's hand: {dealer_hand} (value: {dealer_hand.get_value()})")
    while dealer_hand.get_value() < 17:
        dealer_hand.add_card(deck.deal_card())
        print(f"Dealer hits: {dealer_hand} (value: {dealer_hand.get_value()})")
    player_total = player_hand.get_value()
    dealer_total = dealer_hand.get_value()
    if dealer_total > 21 or player_total > dealer_total:
        print("You win!")
    elif player_total < dealer_total:
        print("Dealer wins!")
    else:
        print("It's a tie!")
if __name__ == "__main__":
    play_blackjack()

Description – (words in bold are parts of the code)

Defining a Card

The Card class represents a single playing card. Each card has a suit (like Hearts), a rank (like Ace or 7), and a value (the number it’s worth in Blackjack). The __str__ method is what makes the card print nicely, such as "Ace of Hearts".

import random
class Card:
    def __init__(self, suit, rank, value):
        self.suit = suit
        self.rank = rank
        self.value = value

    def __str__(self):
        return f"{self.rank} of {self.suit}"

Creating a Deck

The Deck class represents the whole 52-card deck. Inside the constructor, we define all four suits and the possible ranks with their values. Then we build every combination (suit + rank) into a list of Card objects. After that, we shuffle the deck using random.shuffle(). The deal_card method removes the top card from the deck and gives it to a player.

class Deck:
    def __init__(self):
        suits = ["Hearts", "Diamonds", "Clubs", "Spades"]
        ranks = {
            "2": 2, "3": 3, "4": 4, "5": 5, "6": 6,
            "7": 7, "8": 8, "9": 9, "10": 10,
            "Jack": 10, "Queen": 10, "King": 10, "Ace": 11
        }
        self.cards = [Card(suit, rank, value) for suit in suits for rank, value in ranks.items()]
        random.shuffle(self.cards)
    def deal_card(self):
        return self.cards.pop()

Creating hands for player and dealer

The Hand class represents the cards that a player or the dealer holds. It starts empty. The add_card method lets us add a card to the hand. The get_value method calculates the total Blackjack score of the hand, treating Aces as 11 but changing them to 1 if the total goes over 21. The __str__ method makes the hand print as a list of cards separated by commas.

class Hand:
    def __init__(self):
        self.cards = []
    def add_card(self, card):
        self.cards.append(card)
    def get_value(self):
        total = 0
        aces = 0
        for card in self.cards:
            total += card.value
            if card.rank == "Ace":
                aces += 1
        while total > 21 and aces:
            total -= 10
            aces -= 1
        return total
    def __str__(self):
        return ", ".join(str(card) for card in self.cards)

Game Setup

The play_blackjack function is the main game. It first makes a shuffled Deck, then creates empty Hand objects for the player and the dealer. Each of them is then dealt two cards to begin the game.

def play_blackjack():
    deck = Deck()
    player_hand = Hand()
    dealer_hand = Hand()
    player_hand.add_card(deck.deal_card())
    player_hand.add_card(deck.deal_card())
    dealer_hand.add_card(deck.deal_card())
    dealer_hand.add_card(deck.deal_card())

Player’s Turn

During the player’s turn, the program shows the player’s full hand and one of the dealer’s cards. If the player’s hand is already over 21, they “bust” and lose immediately. Otherwise, the player can choose to hit (take another card) or stand (keep their current hand). Hitting adds a card, and standing moves to the dealer’s turn.

while True:
        print(f"\nYour hand: {player_hand} (value: {player_hand.get_value()})")
        print(f"Dealer shows: {dealer_hand.cards[0]}")
        if player_hand.get_value() > 21:
            print("You bust! Dealer wins.")
            return
        choice = input("Do you want to (h)it or (s)tand? ").lower()
        if choice == "h":
            player_hand.add_card(deck.deal_card())
        elif choice == "s":
            break

Dealer’s Turn

Once the player is done, the dealer’s full hand is revealed. The dealer must follow Blackjack rules: keep hitting until the hand is worth at least 17. Each time the dealer takes a card, the program shows the new hand and its total.

    print(f"\nDealer's hand: {dealer_hand} (value: {dealer_hand.get_value()})")
    while dealer_hand.get_value() < 17:
        dealer_hand.add_card(deck.deal_card())
        print(f"Dealer hits: {dealer_hand} (value: {dealer_hand.get_value()})")

Deciding the Winner

After both turns are finished, the program compares the totals. If the dealer goes over 21, the player automatically wins. Otherwise, whoever has the higher total without going over 21 wins. If both totals are equal, the game ends in a tie.

player_total = player_hand.get_value()
    dealer_total = dealer_hand.get_value()
    if dealer_total > 21 or player_total > dealer_total:
        print("You win!")
    elif player_total < dealer_total:
        print("Dealer wins!")
    else:
        print("It's a tie!")

How to play

When you run the game with the code, your console would pop up and it will show 3 lines. the first line shows your cards and the total count of your cards, the second line shows 1 of the dealer’s cards, and the third line would ask if you would like to hit or stand. To hit simply enter “h” and then click enter on your keyboard. To stand enter “s” and then click enter on your keyboard. The game will keep going if you keep hitting until you bust, and when you stand the dealer will process through hitting until his cards are either larger than yours in count, or until he busts.

AI Transcript – (https://docs.google.com/document/d/1ycuqsLcK_CNEMFykyVAm08KaD53f2Z8QLlQ30SFKsBY/edit?usp=sharing)

Comments

One response to “Fusion Coding Assignment”

  1. mcrompton Avatar
    mcrompton

    Excellent work, Jacob! You clearly have a strong background in programming and I like that you leaned on AI to help make the transition to Python and to help explain kety difference in how you might use the languages. From a UX perspective, it would be nice if you had created a link to runnable code. It was easy to copy and paste, but that is one extra step that your reader could have avoided. Not a requirement, but something to keep in mind for the future.

Leave a Reply

Your email address will not be published. Required fields are marked *