The classic game for two.
★★★Rock paper scissors is a hand game originating from China, played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand. These shapes are "rock" (a closed fist), "paper" (a flat hand), and "scissors" (a fist with the index finger and middle finger extended, forming a V).
The rock beats the scissors. The paper beats the rock and the scissors beats the paper. This version of the game is played between a user and the computer over five rounds. The first player to win five rounds wins the game.
Write a program that simulates the game rock, paper, scissors to be played by the user against the computer.
Remember to add a comment before a subprogram, selection or iteration statement to explain its purpose.
get_player_choice that:convert that:rps.rps is "r" it returns "rock".rps is "p" it returns "paper".rps is "s" it returns "scissors".get_cpu_choice that:"r", "p" or "s" instead of 1, 2 or 3.who_won_round that:player and cpu which are the two choices made."player", "cpu" or "draw".play_game that:play_game subprogram is called.Use two spaces between the two scores in the score line:
"player score: {n} cpu score: {m}".
player score: 0 cpu score: 0
Enter rock, paper, scissors (r/p/s):
r
player's rock beats cpu's scissors
player score: 1 cpu score: 0
Enter rock, paper, scissors (r/p/s):
p
cpu's scissors beats player's paper
…
convert
// Convert r/p/s to rock, paper, scissors
static string convert(string rps)
{
---
string output = "";
switch (rps)
{
---
case "r":
output = "rock";
break;
---
case "p":
output = "paper";
break;
---
case "s":
output = "scissors";
break;
}
---
return output;
}
get_player_choice
// Return a valid player choice
static string get_player_choice()
{
---
bool valid = false;
string choice = "";
// Input must be r, p or s
while (!valid)
{
---
Console.WriteLine("Enter rock, paper, scissors (r/p/s):");
choice = Console.ReadLine();
---
if (choice == "r" || choice == "p" || choice == "s")
{
valid = true;
}
}
---
return choice;
}
get_cpu_choice
// Return a random cpu choice
static string get_cpu_choice()
{
---
int cpu = random_generator.Next(1, 4);
string choice = "";
// Convert the random number to a cpu choice
switch (cpu)
{
---
case 1:
choice = "r";
break;
---
case 2:
choice = "p";
break;
---
case 3:
choice = "s";
break;
}
---
return choice;
}
who_won_round
// Determine the winner of a round
static string who_won_round(string player, string cpu)
{
---
string winner = "draw";
// Check who wins the round
if (player == "r" && cpu == "s")
{
winner = "player";
}
---
if (player == "p" && cpu == "r")
{
winner = "player";
}
---
if (player == "s" && cpu == "p")
{
winner = "player";
}
---
if (cpu == "r" && player == "s")
{
winner = "cpu";
}
---
if (cpu == "p" && player == "r")
{
winner = "cpu";
}
---
if (cpu == "s" && player == "p")
{
winner = "cpu";
}
---
return winner;
}
play_game
// Procedure to play the game - first to 5 points
static void play_game()
{
---
int player_score = 0;
int cpu_score = 0;
while (player_score < 5 && cpu_score < 5)
{
---
Console.WriteLine($"player score: {player_score} cpu score: {cpu_score}");
string player_choice = get_player_choice();
string cpu_choice = get_cpu_choice();
string round_winner = who_won_round(player_choice, cpu_choice);
string player_shape = convert(player_choice);
string cpu_shape = convert(cpu_choice);
---
// Add to the score
if (round_winner == "player")
{
Console.WriteLine($"player's {player_shape} beats cpu's {cpu_shape}");
player_score = player_score + 1;
}
---
else if (round_winner == "cpu")
{
Console.WriteLine($"cpu's {cpu_shape} beats player's {player_shape}");
cpu_score = cpu_score + 1;
}
---
else
{
Console.WriteLine($"{player_shape} - {cpu_shape} - it's a draw.");
}
Console.WriteLine();
}
---
if (player_score == 5)
{
Console.WriteLine("PLAYER WINS!");
}
else
{
Console.WriteLine("CPU WINS!");
}
}