Blackjack Game Rules

Overview

  • Players: 2 (Computer vs Human)
  • Objective: Reach a card sum closest to 21 without exceeding it.

Card Values

  • Number Cards: Face value (2-10)
  • Face Cards (Jack, Queen, King): 10
  • Ace: 1 or 11 (player’s choice)

Gameplay

  1. Initial Deal: Each player receives two random cards. Card values range from 1 to 11.
  2. Player’s Turn:
    • Draw Option: Player can draw additional cards to get closer to 21.
    • Stand Option: Player can choose to stop drawing cards.
    • Bust: If total sum exceeds 21, the player automatically loses.
  3. Dealer’s (Computer’s) Turn:
    • Mandatory Draw: Dealer must draw cards until the total is 17 or more.
    • Stand: Dealer stands if the sum is 17 or more.
    • Bust: If dealer’s total exceeds 21, the dealer automatically loses.
  4. Conclusion:
    • Player Wins: If player’s total is greater than the dealer’s and ≤ 21.
    • Dealer Wins: If dealer’s total is greater than the player’s and ≤ 21.
    • Bust Rule: Player or dealer automatically loses if their total exceeds 21.

Additional Notes:

  • Ensure a fair random function for card drawing.
  • Implement UI with two buttons: “Draw” for drawing cards, “Stand” to end turn.
  • The game continues until either player stands or busts.
  • The winner is determined by comparing the totals of the player and the dealer.

Gameplay

1. Define Card Deck and Values

# Import the random module for shuffling the deck
import random

# Define the suits and ranks of a standard deck of cards
suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']

# Create a function to generate and shuffle the deck
def create_deck():
    """Create and shuffle a deck of cards."""
    deck = [{'rank': rank, 'suit': suit} for rank in ranks for suit in suits]
    random.shuffle(deck)
    return deck

# Create a function to determine the numerical value of a card
def card_value(card):
    """Return the numerical value of a card."""
    if card['rank'] in ['King', 'Queen', 'Jack']:
        return 10
    elif card['rank'] == 'Ace':
        return 11  # Ace can be 1 or 11; initially set to 11
    else:
        return int(card['rank'])

Code Snippet

The provided Python code initializes a deck of standard playing cards, assigning suits and ranks to each card. The create_deck function generates and shuffles a deck using list comprehensions and the random.shuffle function from the random module. Additionally, the card_value function determines the numerical value of a card based on its rank, with special consideration for face cards and the flexible value of an Ace. These functions lay the foundation for a card game, offering a shuffled deck and a mechanism to assign values to cards for gameplay purposes.

2. Initial Deal

# Create a function to deal two cards to each player
def initial_deal(deck):
    """Deal two cards to each player."""
    player_hand = [deck.pop(), deck.pop()]  # Player's initial hand
    dealer_hand = [deck.pop(), deck.pop()]  # Dealer's initial hand
    return player_hand, dealer_hand, deck

Code Snippet

The provided Python code defines a function named initial_deal responsible for dealing two cards to each player in a card game. The function takes a deck of cards as an input parameter. It uses the pop method to draw and remove two cards from the deck for both the player and the dealer. The player’s and dealer’s initial hands are then returned as tuples along with the updated deck. This function encapsulates the initial dealing phase of the card game, setting up the players with their starting hands for further gameplay.

Player's Turn

# Create a function to handle the player's turn
def player_turn(player_hand, deck):
    """Handle the player's turn."""
    while sum(card_value(card) for card in player_hand) < 21:
        print(f"Player's hand: {', '.join(card['rank'] for card in player_hand)}")
        choice = input("Do you want to draw another card? (y/n): ").lower()
        if choice == 'y':
            player_hand.append(deck.pop())  # Draw a card
        elif choice == 'n':
            break
    return player_hand

Code Snippet

The player_turn function in the provided Python code manages the player’s turn in a card game. During the player’s turn, the function iteratively displays the current hand and prompts the player to decide whether to draw another card. The loop continues as long as the total numerical value of the player’s hand is below 21. If the player opts to draw another card, it is added to their hand, and the loop continues. If the player chooses not to draw (‘n’), the loop breaks. The function returns the updated player’s hand after the turn, capturing the player’s decisions and card draws during gameplay.

Blackjack Binary Figma Planning

Binary Use in Code

We plan on converting card and sum values into binary at the very end, after doing the algorithms and functions, to allow the user/player to be able to convert from binary to numbers themselves to figure out if they want to hit or stand.

var numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
    var suits = ["♠︎", "♥︎", "♦︎", "♣︎"];

// to binary pre

var numbers = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];

// Function to convert a decimal number to 4-bit binary representation
function decimalToBinary(decimal) {
    // Use toString(2) to convert the decimal to binary
    var binary = parseInt(decimal, 10).toString(2);

    // Add leading zeros to make it 4 bits
    while (binary.length < 4) {
        binary = '0' + binary;
    }

    return binary;
}

// Convert each number in the array to binary
var binaryNumbers = numbers.map(decimalToBinary);

console.log(binaryNumbers);