Let’s break down the solution for the simple Python program that simulates a game of 21 (blackjack capstone project):
- Initialization:
- The program begins by printing a welcome message and explaining the rules of the game.
- It initializes two variables:
player_score
andcomputer_score
to keep track of the scores of the player and the computer.
- Game Loop:
- The program enters a
while
loop, which will continue until the game ends. - Within the loop, it displays the player’s current score and asks whether the player wants to draw a card. The player’s response is stored in the
draw_card
variable after converting it to lowercase.
- The program enters a
- Player’s Turn:
- If the player chooses to draw a card (“yes”), the program generates a random card value between 1 and 10 using
random.randint(1, 10)
. This card value is added to the player’s score. - The program checks if the player’s score exceeds 21. If it does, the player has busted, and the game ends. A message is displayed, and the loop is terminated using
break
.
- If the player chooses to draw a card (“yes”), the program generates a random card value between 1 and 10 using
- Player’s Decision:
- If the player chooses not to draw a card (“no”), the program displays the player’s final score and exits the loop using
break
.
- If the player chooses not to draw a card (“no”), the program displays the player’s final score and exits the loop using
- Computer’s Turn:
- After the player’s turn, it’s the computer’s turn to play.
- The computer will continue drawing cards as long as its score is less than 17. This simulates a basic strategy where the computer is more aggressive if its score is low.
- Computer’s Final Score:
- Once the computer’s turn is over, the program displays the computer’s final score.
- Determining the Winner:
- The program determines the winner based on the following rules:
- If the player’s score exceeds 21, the computer wins.
- If the computer’s score exceeds 21, the player wins.
- If the player’s score is higher than the computer’s score, the player wins.
- If the computer’s score is higher than the player’s score, the computer wins.
- If both the player and computer have the same score, it’s a tie.
- The program determines the winner based on the following rules:
- End of the Game:
- The program prints the result of the game, whether it’s a win for the player, the computer, or a tie.
The solution provides a basic implementation of a 21 (blackjack) game, where the player and the computer take turns drawing cards and the winner is determined based on the scores. It demonstrates the use of while
loops, conditional statements (if
and elif
), user input handling, random number generation, and score tracking to create a simple game simulation.
import random
def main():
print("Welcome to the Game of 21!")
print("You and the computer will take turns drawing cards.")
print("Try to get as close to 21 as possible without going over.")
player_score = 0
computer_score = 0
while True:
print("\nYour current score:", player_score)
draw_card = input("Do you want to draw a card? (yes/no): ").lower()
if draw_card == "yes":
card = random.randint(1, 10)
print("You drew a card with a value of", card)
player_score += card
if player_score > 21:
print("Bust! Your score is over 21. You lose.")
break
elif draw_card == "no":
print("Your final score:", player_score)
break
else:
print("Invalid input. Please enter 'yes' or 'no'.")
# Computer's turn
if computer_score < 17:
computer_card = random.randint(1, 10)
print("\nComputer drew a card with a value of", computer_card)
computer_score += computer_card
print("\nComputer's final score:", computer_score)
# Determine the winner
if player_score > 21:
print("Computer wins!")
elif computer_score > 21:
print("You win!")
elif player_score > computer_score:
print("You win!")
elif computer_score > player_score:
print("Computer wins!")
else:
print("It's a tie!")
if __name__ == "__main__":
main()
Day 11 : Blackstone capstone project Day 12 : Scope of Variable
One Response