Question

For this assignment you will create a one-player variation of the game BlackJack. The game will...

For this assignment you will create a one-player variation of the game BlackJack. The game will be divided into five rounds. The goal of each round is to accumulate a hand of cards that gets as close to 21 without going over. At the beginng of the game you will start with a score of 100 points. After each round, the difference between your hand and 21 will be subtracted from your total score. The object of the entire game is to get the highest score possible at the end of 5 rounds. The rules of each round work similarly to playing a hand of BlackJack. You will be dealt a hand of two cards, each worth a given value (see Counting a Hand below). At this point you are given the choice to either "Stand", that is keep your hand where it is, or "Hit", and be dealt another card. You may continue to hit as many times as you like, until you wish to stand or until you go over 21. If your hand goes over 21 then your hand is "Bust", and the round ends. At the beginning of the game you will start with 100 points. After each round the difference between the total value of your hand and the perfect score of 21 will be subtracted from your total. If you bust, however, a value of 21 will be subtracted from your total.

The deck of cards will be simulated with integers of values from 1 to 13, where 1 represents the Ace (A), 11 the Jack(J), 12 the Queen(Q), and 13 the King(K). Use the following rules when determining the value of a hand of cards.

  • A hand is worth the sum of the value of each of the individual cards.
  • Most cards are worth their value (ie, a 9 is worth 9).
  • "Face cards" (ie, Jack, Queen, King) are worth 10.
  • Aces are worth either 11 or 1. For your program consider Aces are worth 11 unless the total hand is over 21, then they are worth 1.

    At the start of the game your total score will begin at 100. Each round your score will be decreased by 0 or more points. At the end of the game your final score will be a number in the range [-5,100], and it will be ranked according to the following categories:

  • 95+: Ace!
  • 85 to 94: King
  • 75 to 84: Queen
  • 50 to 74: Jack
  • 25 to 49: Commoner
  • -5 to 24: Noob

    User interaction in this game will be entirely text-based. According to the following rules.

  • At the start of each round, display the round number and the player's current score.
  • Each time the player is dealt new card(s) display their current hand.
  • Display of the player's hand should include the sum of the cards in their hand.
  • Display of a card should be a value in the range 2-10, or one of the face cards: A, J, Q, K
  • After being dealt new card(s), the player should be prompted to either 'hit' or 'stand' by typing one of those two words.
  • If the player's hand ever busts, they should not be prompted to hit or stand.
  • Invalid user inputs should be met with a repeated prompt.
  • At the end of the game (after 5 rounds), the player's final score and rank should be displayed.
  • Once the game is complete your program should prompt the user if they want to play again.

    Your code must be structured in a procedural style. That is, your code should have a main() function that acts as the starting point of your program. The main function should be responsible for starting, repeating, and ending your game. In addition, your program should have (at minimum) the following other functions:

  • play() - returns nothing, controls the mechanics of one complete execution of your game (not including replays)
  • sumHand(list)→ int - sums and returns the value of the inputted hand according to the rules above
  • displayHand(list) - returns nothing, but displays the given hand as well as the given hand's sum to the console
  • dealCard() → int - returns a random card
  • getRank(int)→string - returns the rank corresponding to the inputted total points
  • Please note, your implementation should use the signatures (function names and expected parameters and return types) shown here. You are encouraged to define any additional helper functions you may need.

    You should not make use of any global variables in your solution. As a bonus challenge, design your game so that it simulates an authentic deck of 52 cards for each round. This means that at the start of each round you will build a list of 52 cards containing exactly 4 of each value (1-13). Each time the player is dealt a new card that card should be removed from the deck (so the player will never get a hand of 5 Aces, for example). Note: if you attempt the bonus you will likely need to modify the signature for the required dealCard() function mentioned above so that it accepts the deck of cards as argument. Please mention in the comments of that function that your solution attemped this bonus.

  • A sample run:

  • python twentyone.py Round 1 Score: 100 Your hand: Q K (20) Would you like to 'hit' or 'stand': stand Round 2 Score: 99 Your hand: 7 7 (14) Would you like to 'hit' or 'stand': hit Your hand: 7 7 7 (21) Would you like to 'hit' or 'stand': stand Round 3 Score: 99 Your hand: 3 A (14) Would you like to 'hit' or 'stand': hit Your hand: 3 A 3 (17) Would you like to 'hit' or 'stand': stand Round 4 Score: 95 Your hand: 2 8 (10) Would you like to 'hit' or 'stand': hit Your hand: 2 8 J (20) Would you like to 'hit' or 'stand': stand Round 5 Score: 94 Your hand: 2 K (12) Would you like to 'hit' or 'stand': hit Your hand: 2 K K (22) Bust! Final score: 73, Your rank: Jack Would you like to play again? (y/n) n Goodbye!

  • The program must be in the programming language python 3.7.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Summary :

twentyone.py is listed below and execution output is shown as part of "twentyone_output.jpg"

Note : Implemented with 52 cards , which are randomized and removed card is not available (within the round ).

       Since no global variables are allowed and can't pass any parameters to play() , used "static" variables for play() to maintain the total score and reset it every 5 rounds .

########################## twentyone.py ##################################

import random
from typing import List


def sumHand(list : List[object]) -> int:

    aces = 0
    total_hand = 0
  
    for card in (list ) :
        if card[1] == 1 :
            if ((total_hand + 11 ) > 21 ):
                total_hand = total_hand + 1
            else :
                total_hand = total_hand + 11

        elif ( card[1] > 10 ) :
            total_hand = total_hand + 10
        else :
            total_hand = total_hand + card[1]

    return total_hand


def displayHand(list : List[object] ) -> None:
    DisplayCard = { 1 : 'A' , 2 : '2' , 3 : '3' , 4 : '4' , 5 : '5' , 6 : '6' ,
                    7 : '7' , 8 : '8' , 9 : '9' , 10 : '10' , 11 : 'J' , 12 : 'K' , 13 : 'Q' }
    output_str = ""

    sum_hand = sumHand(list)
    ex_str = ""
    if ( sum_hand > 21 ) :
        ex_str = " Bust!"
    for i in (list ) :
        output_str = output_str + " " + DisplayCard[i[1]]

    print("Your Hand : " + output_str + " (" + str(sum_hand) + " )" + ex_str )


def dealCard( cards_deck : List[object] ) -> tuple :
    # From the Shuffled cards remove top card
    randomize_cards = random.sample(cards_deck,len(cards_deck))
    card_picked = cards_deck[0]
    del cards_deck[0]
  
    return card_picked


def getRank(score : int ) -> str :

    if ( score >= 95 ) :
        return "Ace!"
    if ( score >= 85 ) :
        return "King"
    if ( score >= 75 ) :
        return "Queen"
    if ( score >= 50 ) :
        return "Jack"
    if ( score >=25 ) :
        return "Commoner"
    if ( score >= -5 ) :
        return "Noob"


    return "xyz"


def play() -> None :
    cur_score = 0
    if not hasattr(play, "round"):
        play.round = 0

    play.round = play.round + 1
    if ( play.round > 5 ) :
        play.round = 1
    if not hasattr(play, "card_deck"):
           type = [ 'S' , 'C' , 'D' , 'H' ]
           play.card_deck = []

           for i in (type) :
               for j in range(1,14) :
                   play.card_deck.append([i,j])
  
    # Randomize cards
    randomized_cards = random.sample(play.card_deck,len(play.card_deck))


    if not hasattr(play, "total_score" ):
        play.total_score = 100
  
    playerHand = []
    print("#######################################################")
    #print(" Round " + str(play.round) + " , Score : " + str(cur_score ))
    card0 = dealCard(randomized_cards)

    playerHand.append(card0)
    card1 = dealCard(randomized_cards)
    playerHand.append(card1)

    print(" Round " + str(play.round) + " Score : " + str(play.total_score) )
    displayHand(playerHand)
    ip = ''
    while ( cur_score < 21 ) :

        ip = input("Would you like to 'hit' or 'stand' :" )
        if ( ip == 'hit') :
           card1 = dealCard(randomized_cards)
           playerHand.append(card1)
           cur_score = sumHand(playerHand)
           #print("Score : " , cur_score )
           displayHand(playerHand)
        if (ip == 'stand' ) :
            break

    cur_score = sumHand(playerHand)
    temp_score_minus = 21 if cur_score > 21 else ( 21 - cur_score)
    play.total_score = play.total_score - temp_score_minus
    #print(" Total Score : " + str(play.total_score) + " This Round Score : " + str(cur_score))
    #print("#######################################################")

if __name__ == "__main__":

    input_option = "Y"
    while (( input_option == "Y" ) or (input_option == "y") ) :
        for i in range(5) :
           play()
  
        print("Your rank : " + getRank(play.total_score ) )
        play.total_score = 100
        input_option = input("Would you like to play again (Yy/Nn) :")


    print("Goodbye .. ")

   

########################### End of twentyone.py ############################

###################### Execution output ###################################

Add a comment
Know the answer?
Add Answer to:
For this assignment you will create a one-player variation of the game BlackJack. The game will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a simplified Blackjack game using the Deck and Card classes (download the attached files to...

    Create a simplified Blackjack game using the Deck and Card classes (download the attached files to start).  There is also a "TestCard" class that creates a Deck and runs some simple code. You may choose to use this file to help you get started with your game program. 1) Deal 2 cards to the "player" and 2 cards to the "dealer".  Print both the player's cards, print one of the dealer's cards. Print the total value of the player's hand. 2) Ask...

  • Using C++ Create a Blackjack program with the following features: Single player game against the dealer....

    Using C++ Create a Blackjack program with the following features: Single player game against the dealer. Numbered cards count as the number they represent (example: 2 of any suit counts as 2) except the Aces which can count as either 1 or 11, at the discretion of the player holding the card. Face cards count as 10. Player starts with $500. Player places bet before being dealt a card in a new game. New game: Deal 2 cards to each...

  • Need a blackjack code for my assignment its due in 3 hours: it has to include...

    Need a blackjack code for my assignment its due in 3 hours: it has to include classes and object C++. Create a fully functioning Blackjack game in three separate phases. A text based version with no graphics, a text based object oriented version and lastly an object oriented 2D graphical version. Credits (money) is kept track of throughout the game by placing a number next to the player name. Everything shown to the user will be in plain text. No...

  • "Blackjack 40" my teacher created this assignment and I need help. He is using c++ and...

    "Blackjack 40" my teacher created this assignment and I need help. He is using c++ and has provided this template: Further instructions are below the template! #include <iostream> #include <cmath> #include <cstdlib> using namespace std; //Will return a number from 1 to 13, representing the face of a card int draw_card() { return rand() % 13 + 1; } int main() { const int BET = 10; //This will allow you to control chance, to make testing easier cout <<...

  • 2. (10 points) Suppose you are playing blackjack against a dealer. In a freshly shuffled deck, what is the probabil...

    2. (10 points) Suppose you are playing blackjack against a dealer. In a freshly shuffled deck, what is the probability that either you or the dealer is dealt a blackjack? (Recall the game of blackjack is played with each player obtaining two cards at random from a regular deck. We say these two cards form a blackjack if one card is an ace and the other card is either a 10, Jack, Queen or King) 2. (10 points) Suppose you...

  • How do I start this c++ code homework? Write a code in C++ for a BlackJack...

    How do I start this c++ code homework? Write a code in C++ for a BlackJack card game using the following simplified rules: Each card has a numerical value. Numbered cards are counted at their face value (two counts as 2 points, three, 3 points, and so on) An Ace count as either 1 point or 11 points (whichever suits the player best) Jack, queen and king count 10 points each The player will compete against the computer which represents...

  • 4. In one version of the game played by a dealer and a player, 2 cards...

    4. In one version of the game played by a dealer and a player, 2 cards of a standard 52-card bridge deck are dealt to the player and 2 cards to the dealer. For this exercise, assume that drawing an ace and a face card (Jack, Queen and King for each shape) is called blackjack. If the dealer does not draw a blackjack and the player does, the player wins. If both the dealer and player draw blackjack, a tie...

  • In the game of blackjack, the player is initially dealt two cards from a deck of...

    In the game of blackjack, the player is initially dealt two cards from a deck of ordinary playing cards. Without going into all the details of the game, it will suffice to say here that the best possible hand one could receive on the initial dear is a combination of an ace of any suit and any face card or 10. What is the probability that the player will be dealt this combination?

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • Must be in C++ Question: Blackjack (twenty-one) is a casino game played with cards. The goal...

    Must be in C++ Question: Blackjack (twenty-one) is a casino game played with cards. The goal of the game to draw cards that total as close to 21 points as possible without going over. All face cards count as 10 points, aces count as 1 or 11, and all other cards count their numeric value. The game is played against a dealer. The player tries to get closer to 21 (without going over) than the dealer. If the dealer busts...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT