In: Computer Science
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.
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 ###############