I have been studying C#, and I am trying to model the "Rock / Paper / Scissors" game.
Is my program reasonable or is there anything I need to improve?
- user input a round.
- user input RockPaper or Scissor.
- After the game is finished , user can choose to play again or quit.
namespace Project_RockPaperSci_Revised
{
public class RockPaperScissor
{
public Random rnd = new Random();
public string userInput;
public int userScore = 0;
public int comScore = 0;
public bool isPlaying = true;
public int around;
public void PlayGame()
{
Console.WriteLine("Welcome Let's play Rock Paper Scissors");
SetRound(ref around);
while (isPlaying) {
userScore = 0;
comScore = 0;
while (userScore < around && comScore < around) {
PlayerInput(ref userInput);
Console.WriteLine("\nPlayer chose {0}", userInput);
Match(userInput, ref userScore, ref comScore);
Console.WriteLine($"User's Score<{userScore}>\tCom's Score<{comScore}>\n");
}
string again = string.Empty;
Console.WriteLine("Do you want to play again? y/n ");
again = Console.ReadLine();
again = again.ToUpper();
if (again == "Y")
{
Console.Clear();
} else if (again == "N")
{
Console.WriteLine("Good Bye");
isPlaying = false;
}
}
}
public string PlayerInput(ref string userInput)
{
Console.Write("Choose btw RockPaperScissor \n");
userInput = Console.ReadLine();
userInput = userInput.ToUpper();
return userInput;
}
public int ShowResult(int flagNum)
{
int flagScore =0 ;
switch (flagNum)
{
case 1:
Console.WriteLine("User Win !!");
break;
case 2:
Console.WriteLine("Computer Win !!");
break;
case 3:
Console.WriteLine("Draw !!");
break;
default:
break;
}
return flagScore;
}
public void Match(string userInput,ref int userScore, ref int comScore)
{
int comChoice;
comChoice = rnd.Next(1, 4);
// flagNum 1 = userWin 2 = ComWin 3 = Draw
int flagNum = 0;
switch (comChoice)
{
case 1:
Console.WriteLine("Computer chose ROCK");
if (userInput == "ROCK")
{
flagNum = 3;
}else if (userInput == "PAPER")
{
flagNum = 1;
comScore++;
} else if (userInput == "SCISSOR")
{
flagNum = 2;
userScore++;
}
break;
case 2:
Console.WriteLine("Computer chose PAPER");
if (userInput == "ROCK")
{
flagNum = 2;
comScore++;
}
else if (userInput == "PAPER")
{
flagNum = 3;
}
else if (userInput == "SCISSOR")
{
flagNum = 1;
userScore++;
}
break;
case 3:
Console.WriteLine("Com Chose SCISSOR");
if (userInput == "ROCK")
{
flagNum = 1;
userScore++;
}
else if (userInput == "PAPER")
{
flagNum = 2;
comScore++;
}
else if (userInput == "SCISSOR")
{
flagNum = 3;
}
break;
default:
Console.WriteLine("Error");
break;
}
ShowResult(flagNum);
}
public void SetRound(ref int around)
{
Console.Write("How many round would you like to play?\t");
around = Convert.ToInt32(Console.ReadLine());
}
}
class Program
{
static void Main(string[] args)
{
RockPaperScissor rps1 = new RockPaperScissor();
rps1.PlayGame();
}
}
}