Question

In: Computer Science

How do you write pseudocode for this problem? The decides are provided below. Problem Statement We...

How do you write pseudocode for this problem? The decides are provided below.

Problem Statement

We are going to create a game called TwentyOne, which is similar to the Blackjack and War card games. This game has a dealer/computer and 1-4 players, and the game will generate random numbers from 1 to 11 to simulate the values in cards. All players start with a specified amount of money, which they determine at the start of the game, and the goal is to see if you can get closer than the dealer to twenty-one points without going over.  

With each player’s turn, they first bet against the dealer, and then play against the dealer before moving on to the next player. An initial random number, 1-11, is generated, and after each random number is generated, the player decides whether they want to generate another to add to their total points trying to get as close to 21 as possible without going over. The dealer is also given random numbers, but the strategy you use for determining whether they get another random number is your choice. The rules for the game play are below.

• If the player busts, the player automatically loses their bet, and the dealer doesn’t need to play against that player.

• If the player gets exactly 21 in hand, the player automatically wins their bet, and the dealer doesn’t need to play.  

• If the dealer busts, the player automatically wins their bet.

• If the dealer ties with the player, then the player doesn’t win or lose their bet. • If the player’s bet is more than their bank, then you need to continue to re-prompt for a new bet until a good value is given.

• If a player’s bank is 0, they get skipped.

• If all players’ banks are 0, then the game is automatically over!

• After all players take a turn against the dealer, then the players can collectively cash out or play again.

Example gameplay below (User inputs are highlighted):

How many players do you have (1-4)? 3

Player 1, how much money are you starting with? 20

Player 2, how much money are you starting with? 50

Player 3, how much money are you starting with? 30

Player 1’s turn…

Player 1, how much do you bet you can beat the dealer? 10

Player 1, you got a(n) 10. Your running total is 10. Do you want another number (0-no or 1-yes)? 1

Player 1, you got a(n) 8. Your running total is 18. Do you want another number (0-no or 1-yes)? 0

Your total points are 18.

Dealer’s turn…

The dealer gets a(n) 11, running total is 11.

The dealer gets a(n) 11, running total is 22.

The dealer busts! You win!

Player 1 has 30.00.

Player 2’s turn…

Player 2, how much do you bet you can beat the dealer? 50

Player 2, you got a(n) 8. Your running total is 8. Do you want another number (0-no or 1-yes)? 1

Player 2, you got a(n) 9. Your running total is 17. Do you want another number (0-no or 1-yes)? 0

Your total points are 17.

The dealer gets a(n) 11, running total is 11.

The dealer gets a(n) 10, running total is 21.

The dealer stops…

The dealer has 21 points. You lose!

Player 2 has 0.00.

Player 3’s turn…

Player 3, how much do you bet you can beat the dealer? 50

Invalid number, you only have 30 in hand.

Player 3, how much do you bet you can beat the dealer? 25

Player 3, you got a(n) 8. Your running total is 8. Do you want another number (0-no or 1-yes)? 1

Player 3, you got a(n) 9. Your running total is 17. Do you want another number (0-no or 1-yes)? 1

Player 3, you got a(n) 3. Your running total is 20. Do you want another number (0-no or 1-yes)? 0

Your total points are 20.

The dealer gets a(n) 10, running total is 10.

The dealer gets a(n) 10, running total is 20.

The dealer stops…

The dealer has 20 points. Tie game!

Player 3 has 30.00.

Do you want to play again (0-no or 1-yes)? 1

Player 1’s turn…

Player 1, how much do you bet you can beat the dealer? 10

Player 1, you got a(n) 10. Your running total is 10. Do you want another number (0-no or 1-yes)? 1

Player 1, you got a(n) 11. Your running total is 21.  

You got 21 in hand. You win!

Player 1 has 40.00.  

Player 2 is skipped…

Player 3’s turn…

Player 3, how much do you bet you can beat the dealer? 20

Player 3, you got a(n) 10. Your running total is 10. Do you want another number (0-no or 1-yes)? 1

Player 3, you got a(n) 9. Your running total is 19. Do you want another number (0-no or 1-yes)? 1

Player 3, you got a(n) 5. Your running total is 24.  

You bust. You lose!

Player 3 has 10.00.  

Do you want to play again (0-no or 1-yes)? 0

Program Design.

• What does the overall big picture of this program look like? (flowchart or pseudocode) What data/variables do you need to create, when do you read input from the user, what is the dealer strategy going to be? Can you reuse some of the variables? If so, what are they?

o What are the decisions that need to be made in this program?

o What tasks are repeated?

• What kind of bad inputs are you going to handle?

Based on your answers above, list the specific steps or provide a flowchart of what is needed to create this program to play TwentyOne. Be very explicit!!!

(10 pts) Extra Credit 1

Handle all bad user input, even data of the wrong type. i.e. Re-prompt the user if he/she enters “asdf” instead of 1 or 0.

(10 pts) Extra Credit 2

Change the game play so that the dealer plays against all players in a turn, rather than playing only again one player at a time.

Solutions

Expert Solution

Summary :

Provides notes for implementation , code and output.

Notes : The code is implemented in python with comments. 2 functions are implemented

play21() which does the dealing of cards and deciding who won / loss/tie . It accepts player number and total amount and returns the total amout after player win/loss/ tie outcome .

The validation is shown for amount to bet input , same can be extended/ implemened to all the inputs. It utiizes try/except block to catch the valueError while converting the input string to integer .

THen main() funciton implements the driver program where in the number of players , respective bet amounts are read and then play21() is called for each player if the amount is > 0 .

########################### Code ###############

import random
# Globa variables for dealer amount and minimum percent of bet 
dealer_amt = 1000
percent_bet = 10 

# play21 is function which iterates thru player and dealer in dealing the cards 
# and decide who win and returns the total_amount of player at the end of the play
# it accept player number and total_amount started with 
def play21(playerNum, player_total_amt  ) :
    global dealer_amt
    # card total to start with is zero 
    player_card_toal = 0
    
    # Continue reading till valid values are input 
    while ( True ) :
        try :
            bet_str  = input("Player {}, how much do you bet you can beat the dealer?".format(playerNum))
            bet = int(bet_str)
        except ValueError  as e :
            print("Invalid input, try again .. ")
        else :
            if ( bet <  player_total_amt * percent_bet/100 ) :
                print(" Bet should be atleast {} of total amount ".format(percent_bet ))
            else :
                break
        
    # define play continue to 1 and player won = 0 
    play_cont = 1 
    player_won = 0 
    
    # play till the play continue is 1 
    while ( play_cont == 1 ) : 
    
        # generate random card 
        currentCard = random.randint(1,12)
        
        print(" {}, you got a(n) {} \n".format(playerNum,currentCard ))
        player_card_toal += currentCard
        
        # Decide whether player won or continue to play etc 
        if ( player_card_toal < 21 )  :
            play_cont = input(" {}, you got a(n) {}. Your running total is {}. Do you want another number (0-no or 1-yes)?".format( playerNum, currentCard, player_card_toal ))
            play_cont = int(play_cont)
            if ( play_cont == '0' ) :
                break
        elif ( player_card_toal > 21 ) :
            print("Player 3, you got a(n) 5. Your running total is 24.".format(playerNum, currentCard, player_card_toal ))
            player_total_amt -= bet
            player_won = -1 
            break
        else:
            player_total_amt += bet
            print(" You got a hand 21, you win")
            player_won = 1 
            break
    
    dealer_card_total = 0 
    
    # Play the dealer only if player didn't win or lose ( ie.e player won is  0 ) 
    if ( player_won == 0 ) :
        
        # play continously till break 
        while ( 1 > 0 ) :
            currentCard = random.randint(1,12)
            dealer_card_total += currentCard 
            print("The dealer gets a(n) {}, running total is {}.".format(currentCard, dealer_card_total))
            
            # if the card total is < 22 continue deciding else dealer lost 
            if ( dealer_card_total < 22 ) :
            
                if ( dealer_card_total < player_card_toal ) :
                    continue 
                elif ( dealer_card_total > player_card_toal ) :
                    print("The dealer stops…")
                    print("The dealer wins the game ")
                    dealer_amt += bet 
                    player_total_amt -= bet
                    break
                else :
                    print("The dealer stops…")
                    print("Its a tie ")
            else :
                print("The dealer busts! You win!")
                player_total_amt += bet
                break
        
    # return the amount of player 
    return player_total_amt
    


def main() :
    global dealer_amt
    
    # define array of player amount 
    player_Amt = [0,0,0,0]
    # read number of players 
    
    num_players = input("How many players do you have (1-4)? ")
    num_players = int( num_players )
    
    # iterate thru player number and read respective amounts 
    for i in range ( num_players ):
        inputAmt = input( "Player {}, how much money are you starting with?".format(i) )
        player_Amt[i] = int( inputAmt )
        dealer_amt += player_Amt[i]
    
    
    continue_playing  = 'y' 
    while ( True ) :
        # play with all the players  
        for i in range ( num_players) :
            
            if ( player_Amt[i] > 0 ) :
                print(" Player {}’s turn… ".format(i) )
                player_Amt[i] = play21("Player" + str(i), player_Amt[i] )
                print("Player {} has {} ".format( i, player_Amt[i]))
                
        continue_playing = input("Do you want to continue (y/n) ")
        if ( continue_playing == "n") :
            break
         
            


if __name__ == "__main__": 
    main()

################### Output ###############


Related Solutions

Write the Flowchart for the following programming problem based on the pseudocode below. Last year, a...
Write the Flowchart for the following programming problem based on the pseudocode below. Last year, a local college implemented rooftop gardens as a way to promote energy efficiency and save money. Write a program that will allow the user to enter the energy bills from January to December for the year prior to going green. Next, allow the user to enter the energy bills from January to December of the past year after going green. The program should calculate the...
Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
Does anyone know how to do pseudocode on this topic? Write a program to perform the...
Does anyone know how to do pseudocode on this topic? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop...
How do we solve the problem of the federal budget? Do you think you can do...
How do we solve the problem of the federal budget? Do you think you can do better than our lawmakers in Washington? Why is it so complicated? Here's your chance to take a crack at it. Use the budget simulator below to try to balance the federal budget, or at least bring it under control. Now, before you even get started, you must seriously think about a couple of things: you can't just cut programs you don't like, without thinking...
1. Write pseudocode for the following program. You do not have to write actual C++ code....
1. Write pseudocode for the following program. You do not have to write actual C++ code. Assume you have a text file, that has been encrypted by adding 10 to the ASCII value of each character in the message. Design a decryption program that would reverse this process, and display the original message to the console.to the new array. 2.Write the code for the specified program. Use proper style and naming. Test and upload your code as a CPP file....
How to write Problem Statement draft for a healthcare topic
How to write Problem Statement draft for a healthcare topic
Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual...
Below is the pseudocode for Quicksort and Partition that we talked about in class. As usual with recursive functions on arrays, we see the array indices s and e as arguments. Quicksort(A, s, e) sorts the part of the array between s and e inclusively. The initial call (that is, to sort the entire array) is Quicksort(A, 0, n − 1) QuickSort(A, s, e)   if s < e p = Partition (A, s, e) // Partition the array and return...
Write the following Python script: Problem Statement We want to know how many different people have...
Write the following Python script: Problem Statement We want to know how many different people have eaten at a restaurant this past week. The parameter meals has strings in the format "name:restaurant" for a period of time. Sometimes a person eats at the same restaurant often. Return the number of different people who have eaten at the eating establishment specified by parameter restaurant. For example, "John Doe:Moes" shows that John Doe ate one meal at Moes. Write function howMany that...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
We are doing this problem in R software. If you could also show how to do...
We are doing this problem in R software. If you could also show how to do it in that along with the normal work that would be much appreciated. Problem 1 Acid precipitation id linked to the disappearance of sport fish and other organisms from lakes. Sources of air pollution, including automobile emissions and burning fossil fuels, add to the natural acidity of precipitation. The Wisconsin Department of Natural Resources initiated a precipitation monitoring program with the goal of developing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT