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, or less than zero 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:score and both start at 501 (for testing start at 101).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 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: 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):
---
valid = False
---
# Single or low double/treble thrown
---
if dart > -1 and dart < 21:
---
valid = True
---
# Bullseye or 25 thrown
---
elif dart == 25 or dart == 50:
---
valid = True
print("Shot!")
---
# Double thrown
---
elif dart % 2 == 0 and dart <= 40:
---
valid = True
---
print("Double", int(dart / 2))
---
# Treble thrown
---
elif dart % 3 == 0 and dart <= 60:
---
valid = True
---
print("Treble", int(dart / 3))
---
return valid
play_game, main program
# Play the game
def play_game():
---
player = 0
winner = -1
---
# Play until one player reaches zero with a double
while winner == -1:
---
print("Player", player + 1, "it's your turn. Your score is:", score[player])
darts_thrown = 0
total = 0
valid_dart = False
---
# Throw up to 3 darts unless the leg is won sooner
while valid_dart == False or darts_thrown < 3 and winner == -1:
---
print("Dart", darts_thrown + 1)
dart = int(input("Enter score: "))
---
# Keep a running total of the darts for the turn
valid_dart = is_dart_valid(dart)
---
# If the dart is valid, process the score
if valid_dart:
---
darts_thrown = darts_thrown + 1
total = total + dart
---
# Player wins if they reach exactly zero and threw a double with the dart
if score[player] - total == 0 and dart % 2 == 0:
---
winner = player + 1
---
# Bust score
elif score[player] - total < 0 or score[player] - total == 1:
---
print("Bust!")
darts_thrown = 3
total = 0
---
else:
---
print("Invalid dart.")
---
print(total, "scored.")
score[player] = score[player] - total
---
# Next player
print()
---
player = (player + 1) % 2
---
print("Player", winner, "wins the leg.")
---
# -------------------------
# Main program
# -------------------------
score = [101, 101]
play_game()