1. Homepage
  2. Programming
  3. Card Game: 21 Blackjack

Card Game: 21 Blackjack

Engage in a Conversation
Card Game21 BlackjackPython ProgrammingPython

This is a simplified version of a common card game, "21". Blackjack is a better-known variant. CourseNana.COM

In this game, the dealer deals two "cards" to each player in the very first round, one hidden, so that only CourseNana.COM

the player who gets it knows what it is, and one face up, so that everyone can see it. In the following CourseNana.COM

rounds, subsequent cards are added to the visible cards in hand for both players, one card per round, CourseNana.COM

until the user passes. That is to say, each user will get only one hidden hard, which is assigned to them in CourseNana.COM

the first round. (Actually, what the other players see is the total of each other player's visible cards, not CourseNana.COM

the individual cards. Users can know all their own cards and the sum of them.) CourseNana.COM

There are two players: one human player (the person playing the game) and one computer player. To CourseNana.COM

play, the players take turns requesting cards, trying to get as close to 21 as possible, but not going over CourseNana.COM

21. A player may pass (ask for no more cards). Once a player has passed, he or she cannot later ask for CourseNana.COM

another card. When all players have passed, the game ends. CourseNana.COM

The winner is the player who has come closest to 21 without exceeding it. In the case of a tie, or if CourseNana.COM

everyone goes over 21, no one wins. CourseNana.COM

The game is only played once (so it's actually just one "hand"). CourseNana.COM

The "cards" are the numbers 1 through 10 and they are randomly generated, not drawn from a deck of CourseNana.COM

limited size. The odds of returning a 10 are four times as likely as any other value (because in an actual CourseNana.COM

deck of cards, 10, Jack, Queen, and King all count as 10). CourseNana.COM

Provided program implementation: CourseNana.COM

We have provided a simple21.py skeleton file which includes: CourseNana.COM

def main(): CourseNana.COM

Prints the game instructions by calling print_instructions() CourseNana.COM

Gets and sets the user's name dynamically based on user input CourseNana.COM

Sets the computer's name to Computer CourseNana.COM

While the game is running CourseNana.COM

Runs the game by calling run() CourseNana.COM

Asks the user if he/she wants to play the game again by calling ask_yes_or_no("Play CourseNana.COM

again? (y/n) ") CourseNana.COM

If yes: CourseNana.COM

Asks the user if they want to modify their username by calling CourseNana.COM

ask_yes_or_no("Modify username? (y/n) ") CourseNana.COM

Runs the game again by calling run() CourseNana.COM

If no: CourseNana.COM

Exits the program CourseNana.COM

def print_instructions(): CourseNana.COM

Prints out instructions for the game. CourseNana.COM

  CourseNana.COM

def ask_yes_or_no(prompt): CourseNana.COM

Displays the given prompt and asks the user for input. If the user's input starts with 'y', returns CourseNana.COM

True. If the user's input starts with 'n', returns False. CourseNana.COM

For example, calling ask_yes_or_no("Do you want to play again? (y/n)") would display "Do you CourseNana.COM

want to play again? (y/n)", wait for user input that starts with 'y' or 'n', and return True or False CourseNana.COM

accordingly. CourseNana.COM

Other requirements: CourseNana.COM

If the user types in y or yyyy, the function returns True anyway. CourseNana.COM

"Yes" or "No" are case-insensitive. Thats to say, YES and Yes are equal. Theyll both CourseNana.COM

return True. CourseNana.COM

The function needs to trim leading and trailing whitespace (including \t, \n) of the CourseNana.COM

user input. The word “ Yes ” with whitespace will be considered the same as “Yes”. CourseNana.COM

If the users input is invalid, prompt the question again until a valid input. CourseNana.COM

def next_card(): CourseNana.COM

Returns a random "card", represented by an int between 1 and 10, inclusive. CourseNana.COM

The "cards" are the numbers 1 through 10 and they are randomly generated, not drawn from a CourseNana.COM

deck of limited size. The odds of returning a 10 are four times as likely as any other value CourseNana.COM

(because in an actual deck of cards, 10, Jack, Queen, and King all count as 10). CourseNana.COM

Hint: Think about how to write code that will generate a random number from 1 - 13, but will CourseNana.COM

only return a number from 1 - 10 from this function. CourseNana.COM

def take_another_card(computer_total_points, user_visible_card): CourseNana.COM

Strategy for computer to take another card or not. CourseNana.COM

  CourseNana.COM

According to the computers own given total points (sum of visible cards + hidden card) and the CourseNana.COM

user's sum of visible cards, you need to design a game strategy for the computer to win the CourseNana.COM

game. CourseNana.COM

Returns True if the strategy decides to take another card, False if the computer decides not to CourseNana.COM

take another card. CourseNana.COM

Notice that, if the computer decides not to take a card, he will not be able to take the card in the CourseNana.COM

following rounds until the game is restarted. (Same as the user, if the user decides not to take CourseNana.COM

cards, he will be marked as passed and will not take cards until the game is restarted.) CourseNana.COM

def is_game_over(is_user_passed, is_computer_passed): CourseNana.COM

Determines if the game is over or not. If the given is_user_passed is set to True, the user has CourseNana.COM

passed. If the given is_computer_passed is set to True, the computer has passed. CourseNana.COM

This function returns True if both the user and the computer have passed, and False if either of CourseNana.COM

them has not yet passed. CourseNana.COM

def print_status(is_user, name, hidden_card, visible_card, total_points): CourseNana.COM

In each turn, prints out the current status of the game. CourseNana.COM

If is_user is set to True, the given player is the user. In this case, print out the user's given name, CourseNana.COM

his/her hidden card points, visible card points, and total points. CourseNana.COM

If is_user is set to False, the given player is the computer. In this case, print out the computer's CourseNana.COM

given name, and his/her visible card points. CourseNana.COM

For example, calling print_status(True, "Brandon", 4, 15, 19) would print: CourseNana.COM

 Brandon has 4 hidden point(s). CourseNana.COM

 Brandon has 15 visible point(s). CourseNana.COM

 Brandon has 19 total point(s). CourseNana.COM

As another example, calling print_status(False, "Computer", 1, 19, 20) would print: CourseNana.COM

 Computer has 19 visible point(s). CourseNana.COM

def print_winner(username, user_total_points, computer_name, computer_total_points): CourseNana.COM

Determines who won the game and prints the game results in the following format: CourseNana.COM

User's given name and the given user's total points CourseNana.COM

Computer's given name and the given computer's total points CourseNana.COM

The player who won the game and the total number of points he/she won by, or if it's a CourseNana.COM

tie, nobody won. CourseNana.COM

def run(username, computer_name): CourseNana.COM

This function controls the overall game and logic for the given user and computer. CourseNana.COM

Gives each player, in turn, a chance to take a card, until both the user and computer have CourseNana.COM

passed. Prints a message when a player passes. Once a player has passed, that player is not CourseNana.COM

given another chance to take a card CourseNana.COM

  CourseNana.COM

First, assign the user two random cards, one hidden, and one visible. Print the users status. CourseNana.COM

Then, assign the computer two random cards, one hidden, and one visible. Print its status. CourseNana.COM

While the game is not over, ask the user/computer if they want to take random cards until both CourseNana.COM

of them pass. CourseNana.COM

If both of them passed, print the winner, and the run() function will return. CourseNana.COM

Notice that, once a player has passed, that player is not given another chance to take a card. CourseNana.COM

And the user can choose to ask for more cards even though they have more than 21 points. CourseNana.COM

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
Card Game代写,21 Blackjack代写,Python Programming代写,Python代写,Card Game代编,21 Blackjack代编,Python Programming代编,Python代编,Card Game代考,21 Blackjack代考,Python Programming代考,Python代考,Card Gamehelp,21 Blackjackhelp,Python Programminghelp,Pythonhelp,Card Game作业代写,21 Blackjack作业代写,Python Programming作业代写,Python作业代写,Card Game编程代写,21 Blackjack编程代写,Python Programming编程代写,Python编程代写,Card Gameprogramming help,21 Blackjackprogramming help,Python Programmingprogramming help,Pythonprogramming help,Card Gameassignment help,21 Blackjackassignment help,Python Programmingassignment help,Pythonassignment help,Card Gamesolution,21 Blackjacksolution,Python Programmingsolution,Pythonsolution,