I created a Rock/Paper/Scissors game in C# that asks the player the number of rounds he wants to play, and then calculates the points and determines the winner. Is there a way I can fix my code? Is it appropriate to use switch and if/else together?
Console.Write("How many rounds do you want to play ? : ");
int numberOfRounds = Convert.ToInt32(Console.ReadLine());
Random rnd = new Random();
string[] hands = { "Rock", "Paper", "Sci" };
// 0 1 2
int playerPoints = 0;
int computerPoints = 0;
for (int counter = 1; counter <= numberOfRounds; counter++)
{
Console.Write("Player Hand : ");
string playerHand = Console.ReadLine();
int randomNumber = rnd.Next(0, 3);
string computerHand = hands[randomNumber];
Console.WriteLine("Computer Hand : " + computerHand);
switch (playerHand)
{
case "Rock":
if (computerHand == "Rock")
{
playerPoints++;
computerPoints++;
Console.WriteLine("Player And Computer Win a Point");
Console.WriteLine();
}
if (computerHand == "Paper")
{
computerPoints++;
Console.WriteLine("Computer wins a point");
Console.WriteLine();
}
if (computerHand == "Sci")
{
playerPoints++;
Console.WriteLine("Player wins a point");
Console.WriteLine();
}
break;
case "Paper":
if (computerHand == "Paper")
{
playerPoints++;
computerPoints++;
Console.WriteLine("Player and Computer Win a Point");
Console.WriteLine();
}
if (computerHand == "Sci")
{
computerPoints++;
Console.WriteLine("Computer wins a point");
Console.WriteLine();
}
if (computerHand == "Rock")
{
playerPoints++;
Console.WriteLine("Player wins a point");
Console.WriteLine();
}
break;
case "Sci":
if (computerHand == "Sci")
{
playerPoints++;
computerPoints++;
Console.WriteLine("Player and Computer Win a Point");
Console.WriteLine();
}
if (computerHand == "Rock")
{
computerPoints++;
Console.WriteLine("Computer wins a point");
Console.WriteLine();
}
if (computerHand == "Paper")
{
playerPoints++;
Console.WriteLine("Player wins a point");
Console.WriteLine();
}
break;
}
}
Console.WriteLine("*** RESULTS ***");
Console.WriteLine("Player Points : " + playerPoints);
Console.WriteLine("Computer Points : " + computerPoints);
if (playerPoints > computerPoints)
{
Console.WriteLine("Player Wins");
}
else
{
if (computerPoints > playerPoints)
{
Console.WriteLine("Computer Wins");
}
else
{
Console.WriteLine("Equal None Win");
}
}
Console.ReadLine();