A game of 501.
★★☆Darts is a game for two players. The objective of the game is to reach a score of exactly zero, starting from 501. In each turn a player throws up to three darts. Each dart can score 0-20, a double 1-20, a triple 1-20, a 25 or 50.
Write a program to simulate the scoring for the game of darts.
If the total of the darts thrown in a turn when subtracted from the player’s remaining score equals one, is less than zero, or equals zero without the last dart being a double, then the player scores zero and their turn is over.
If the total of one or more darts thrown in a turn would result in a score of exactly zero when subtracted from the remaining score, and the last dart thrown is a double, the player wins. 50 counts as double 25.
In all other cases the total of the three darts thrown is subtracted from the score remaining and the other player then takes their turn.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
is_dart_valid that:play_game that:Invalid dart. and ask them to re-enter the same dart (the Dart <n> prompt is reprinted).Bust! when the player busts (the running total would bring their score below zero, or to exactly one). The turn ends immediately, the player’s score is left unchanged, and 0 scored. is then printed in place of the turn total.<total> scored. where <total> is the score the player just earned this turn (0 on a bust).score and both start at 501.Player 1 it's your turn. Your score is: 501
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 60
Treble 20
Dart 3
Enter score: 20
140 scored.
Player 1 it's your turn. Your score is: 501
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 23
Invalid dart.
Dart 2
Enter score: 60
Treble 20
Dart 3
Enter score: 60
Treble 20
180 scored.
Player 2 it's your turn. Your score is: 501
Dart 1
Enter score: 20
Dart 2
Enter score: 57
Treble 19
Dart 3
Enter score: 19
96 scored.
Player 1 it's your turn. Your score is: 361
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 15
Dart 3
Enter score: 20
95 scored.
Player 2 it's your turn. Your score is: 405
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 20
Dart 3
Enter score: 1
81 scored.
Player 1 it's your turn. Your score is: 266
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 57
Treble 19
Dart 3
Enter score: 57
Treble 19
174 scored.
Player 2 it's your turn. Your score is: 324
Dart 1
Enter score: 20
Dart 2
Enter score: 20
Dart 3
Enter score: 20
60 scored.
Player 1 it's your turn. Your score is: 43
Dart 1
Enter score: 40
Double 20
Dart 2
Enter score: 5
Bust!
0 scored.
Player 1 it's your turn. Your score is: 92
Dart 1
Enter score: 60
Treble 20
Dart 2
Enter score: 32
Double 16
92 scored.
Player 1 wins the leg.
is_dart_valid
# Check if a score for one dart is valid
def is_dart_valid(dart):
---
if 0 <= dart <= 20:
return True
---
if dart in (25, 50):
print("Shot!")
return True
---
if dart % 2 == 0 and 2 <= dart <= 40:
print("Double", dart // 2)
return True
---
if dart % 3 == 0 and 3 <= dart <= 60:
print("Treble", dart // 3)
return True
---
return False
play_game, main program
# Play the game
def play_game():
---
# game starts with player 1
player = 0
---
# players take turn until one player ends with score 0
first_turn = True
while min(score) > 0:
---
if not first_turn:
print()
first_turn = False
---
# outputs whose turn it is and their current score
print("Player", player + 1, "it's your turn. Your score is:", score[player])
---
# allow user to input up to three darts in a turn, validating each dart
attempt = 1
score_of_turn = 0
while attempt <= 3 and score[player] - score_of_turn > 0:
---
print("Dart", attempt)
dart = int(input("Enter score: "))
---
while not is_dart_valid(dart):
print("Invalid dart.")
print("Dart", attempt)
dart = int(input("Enter score: "))
---
attempt += 1
score_of_turn += dart
---
# updates the player score
if score[player] - score_of_turn > 1:
score[player] -= score_of_turn
---
elif score[player] - score_of_turn == 0 and dart % 2 == 0:
score[player] = 0
---
else:
# show that player got busted
print("Bust!")
score_of_turn = 0
---
# show score of turn
print(score_of_turn, "scored.")
---
# give turn to other player
player = 1 - player
---
print()
print("Player", 2 - player, "wins the leg.")
---
# -------------------------
# Main program
# -------------------------
score = [501, 501]
play_game()