MUST BE DONE IN CSHARP Create a RockPaperScissors Applicatio
MUST BE DONE IN CSHARP Create a Rock-Paper-Scissors Application Create a Class to contain the game data and any input or output methods. The main method should contain no Console methods, all functionality should be contained within the class. The program will ask the user’s name, and the number of matches to play. This input needs to be validated as an odd number (in order to avoid ties), with a maximum value of 9. The individual match results should be stored in an array. The program will then prompt the user to choose rock, paper or scissors. It will generate a random value for the computer and declare a winner. In the case of a tie, that match will repeat until a winner is found. After the number of matches specified above the program will provide a game summary for each match and the overall winner. For instance: Match 1 (Player won) Rocks vs Scissors [after 3 ties] Match 2 (Computer won)…. Match 3 (Player won)… Overall winner: Player! Congratulations!
Solution
using System;
using System.Collections.Generic;
namespace ConsoleApplication19
{
public class Game
{
enum RPS : int
{
Rock = 0,
Paper = 1,
Scissors = 2,
Invalid = 100
}
Dictionary<RPS, RPS> winners = new Dictionary<RPS, RPS>
{
{ RPS.Rock, RPS.Scissors },
{ RPS.Scissors, RPS.Paper },
{ RPS.Paper, RPS.Rock }
};
int computerWins = 0;
int playerWins = 0;
string playerName = null;
int scoreX = 0,
scoreY = 0;
static Random r = new Random();
public void Main(String[] args)
{
playerName = null;
while (string.IsNullOrEmpty(playerName))
{
Console.Clear();
Console.Write(\"Welcome to Rock, Paper, Scissors.\ \ \ \ Please enter your name... \");
playerName = Console.ReadLine();
}
Console.Write($\"Welcome {playerName}...\ \ \ \ How many games do you wish to play? Please enter an odd number... \");
string gameStr = Console.ReadLine();
int numOfGames = 0;
int.TryParse(gameStr, out numOfGames);
while (numOfGames <= 0 || numOfGames % 2 == 0)
{
Console.Write($\"{gameStr} is not a valid input.\ \ Please enter the amount of games you wish to play. The number you enter must be an odd number... \");
gameStr = Console.ReadLine();
int.TryParse(gameStr, out numOfGames);
}
SetupGame();
DrawGameBoard();
Console.WriteLine(\"The rules are very simple, you will be asked to make your selection. The computer will choose first, this is so it cant cheat. You can then enter Rock, Paper or Scissors. The winner will be determined against the standard Rock, Paper, Scissors rule book.\ \ \ \ When you are ready press [Enter] to start the game.\");
Console.ReadLine();
for (int i = 0; i < numOfGames; i++)
if (!PlayRound(i))
break;
DrawEnd();
}
bool PlayRound(int roundNumber)
{
DrawGameBoard();
RPS computerChoice = GetComputerRps();
RPS playerChoice = RPS.Invalid;
Console.WriteLine($\"Round {roundNumber + 1}:\");
bool quit = false;
while (playerChoice == RPS.Invalid)
{
Console.Write(\"Please make your selection. Rock, Paper, Scissors... \");
string choice = Console.ReadLine();
switch (choice.ToLowerInvariant().Trim())
{
case \"rock\":
case \"r\":
playerChoice = RPS.Rock;
break;
case \"paper\":
case \"p\":
playerChoice = RPS.Paper;
break;
case \"scissors\":
case \"s\":
playerChoice = RPS.Scissors;
break;
case \"quit\":
case \"q\":
quit = true;
break;
default:
Console.WriteLine($\"{choice} is not a valid selection. Please try again.\ \ \");
break;
}
if (quit)
break;
}
if (quit)
return false;
Console.WriteLine($\"The computer chose: {computerChoice}\");
Console.WriteLine($\"You chose: {playerChoice}\");
CalculateWinner(playerChoice, computerChoice);
Console.Write(\"Press [Enter] to start the next game.\");
Console.ReadLine();
return true;
}
void CalculateWinner(RPS player, RPS computer)
{
if (player == computer)
{
Console.WriteLine($\"The game is a tie.\");
playerWins++;
computerWins++;
}
else
{
var p = winners[player];
if (p == computer)
{
Console.WriteLine($\"Congratulations you won. {player} beats {computer}.\");
playerWins++;
}
else
{
Console.WriteLine($\"Computer Wins. {computer} beats {player}.\");
computerWins++;
}
}
}
RPS GetComputerRps()
{
int rng = r.Next(0, 3);
return (RPS)rng;
}
void SetupGame()
{
int requiredWidth = \"Score: \".Length +playerName.Length +1 +1 +4 +4 +\"Computer\".Length +1 +1 +4 +1;
int right = Console.WindowWidth - requiredWidth;
int top = 1;
if (right < \"ROCK, PAPER, SCISSORS...\".Length)
top++;
scoreX = right;
scoreY = top;
}
void DrawGameBoard()
{
Console.Clear();
Console.SetCursorPosition(0, 0);
for (int i = 0; i < Console.WindowWidth; i++)
Console.Write(\"-\");
Console.Write(\"ROCK, PAPER, SCISSORS...\");
Console.SetCursorPosition(scoreX, scoreY);
Console.Write($\"Score: {playerName}: {FormatScore(playerWins)} Computer: {FormatScore(computerWins)}\");
Console.SetCursorPosition(0, scoreY + 1);
for (int i = 0; i < Console.WindowWidth; i++)
Console.Write(\"-\");
Console.SetCursorPosition(0, scoreY + 4);
}
void DrawEnd()
{
DrawGameBoard();
Console.WriteLine(\"Thank you for playing Rock, Paper, Scissors.\");
Console.WriteLine(\"\ \ \ \ The final score was:\");
Console.WriteLine($\"\ \ {playerName}: {playerWins}\");
Console.WriteLine($\"\ \ Computer: {computerWins}\");
if (playerWins > computerWins)
Console.WriteLine(\"\ \ Congratulations, you have won this round.\");
else
Console.WriteLine(\"\ \ Maybe you will have better luck next time.\");
Console.Write(\"\ \ Press [Enter] to finish the game.\");
Console.ReadLine();
}
string FormatScore(int score)
{
string format = \" \" + score.ToString();
return format.Substring(format.Length - 4);
}
}
}



