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:true if the score of a single dart is valid, or false if it is not. A valid dart is…play_game that:score and both start at 501 (for testing, start at 101).play_game procedure.is_dart_valid
// Check if a score for one dart is valid
static bool is_dart_valid(int dart)
{
---
if (0 <= dart && dart <= 20)
{
---
return true;
---
}
---
if (dart == 25 || dart == 50)
{
---
Console.WriteLine("Shot!");
return true;
---
}
---
if (dart % 2 == 0 && dart >= 2 && dart <= 40)
{
---
Console.WriteLine($"Double {dart / 2}");
return true;
---
}
---
if (dart % 3 == 0 && dart >= 3 && dart <= 60)
{
---
Console.WriteLine($"Treble {dart / 3}");
return true;
---
}
---
return false;
}
play_game, main program
// Play the game
static void play_game()
{
---
int player = 0;
bool first_turn = true;
int last_winner_player = -1;
while (score[0] > 0 && score[1] > 0)
{
---
if (!first_turn)
{
---
Console.WriteLine();
---
}
---
first_turn = false;
Console.WriteLine($"Player {player + 1} it's your turn. Your score is: {score[player]}");
int attempt = 1;
int score_of_turn = 0;
int dart = 0;
while (attempt <= 3 && score[player] - score_of_turn > 0)
{
---
Console.WriteLine($"Dart {attempt}");
Console.WriteLine("Enter score:");
dart = Convert.ToInt32(Console.ReadLine());
while (!is_dart_valid(dart))
{
---
Console.WriteLine("Enter score:");
dart = Convert.ToInt32(Console.ReadLine());
---
}
---
attempt++;
score_of_turn += dart;
---
}
---
// Update the player score
if (score[player] - score_of_turn > 1)
{
---
score[player] -= score_of_turn;
---
}
---
else if (score[player] - score_of_turn == 0 && dart % 2 == 0)
{
---
score[player] = 0;
last_winner_player = player;
---
}
---
else
{
---
Console.WriteLine("Bust!");
score_of_turn = 0;
---
}
---
Console.WriteLine($"{score_of_turn} scored.");
player = 1 - player;
---
}
---
Console.WriteLine();
Console.WriteLine($"Player {last_winner_player + 1} wins the leg.");
---
}
---
public static void Main(string[] args)
{
---
play_game();
}