Gucci ϟ
Gucci ϟ

Reputation: 1

issue with While Loops C#

I have a rock paper scissors program that asks for your input, makes a random cpu input, compares them and then declares the winner of that round. I tried to make a while loop that when a round is over, it plays again until someone is at 3 points. After one round is over the program seems to break. I have quoted the while loop. Thank you!

string playerInput;
string cpuInput;
int randomInt;
bool playAgain = true;
int scorePlayer = 0;
int scoreCPU = 0;


while (scorePlayer > 3 && scoreCPU > 3);
{
    Console.WriteLine("Rock Paper or Scissors?");
    playerInput = Console.ReadLine();
    playerInput.ToLower();
    Random rnd = new Random();
    randomInt = rnd.Next(1, 4);
    switch (randomInt)
    {
        case 1:
            cpuInput = "rock";
            Console.WriteLine("CPU chose rock");
            if (playerInput == "rock")
            {
                Console.WriteLine("DRAW!");
            }
            else if (playerInput == "paper")
            {
                Console.WriteLine("PLAYER WINS!");
                    scorePlayer++;
            }
            else if (playerInput == "scissors")
            {
                Console.WriteLine("CPU WINS!");
                scoreCPU++;
            }
            break;
        case 2:
            cpuInput = "paper";
            Console.WriteLine("CPU chose paper");
            if (playerInput == "rock")
            {
                Console.WriteLine("PLAYER WINS");
                scorePlayer++;
            }
            else if (playerInput == "paper")
            {
                Console.WriteLine("DRAW!");

            }
            else if (playerInput == "scissors")
            {
                Console.WriteLine("CPU WINS!");
                scoreCPU++;
            }
            break;
        case 3:
            cpuInput = "scissors";
            Console.WriteLine("CPU chose scissors");
                if (playerInput == "rock")
            {
                Console.WriteLine("CPU WINS!");
                scoreCPU++;
            }
            else if (playerInput == "paper")
            {
                Console.WriteLine("PLAYER WINS!");
                    scorePlayer++;
            }
            else if (playerInput == "scissors")
            {
                Console.WriteLine("DRAW!");
            }
            break;
        default:
            Console.WriteLine("INVALID INPUT!");
            break;
                if (scorePlayer == 3)
                    {
                    Console.WriteLine("PLAYER WON!");
                    }
                else if (scoreCPU ==3);
                {
                    Console.WriteLine("CPU WON!");
                }      
        }
}

Console.ReadLine();

Upvotes: 0

Views: 193

Answers (2)

Rashid Ali
Rashid Ali

Reputation: 617

As per program description you mentioned, it should run until one of the player or CPU have 3 points, allow the While loop to run until it meets the condition to break.

Change your While loop condition to:

while(scorePlayer < 3 && scoreCPU < 3)

Upvotes: 0

madreflection
madreflection

Reputation: 4957

The problem lies here:

int scorePlayer = 0;
int scoreCPU = 0;


while (scorePlayer > 3 && scoreCPU > 3);

Neither scorePlayer nor scoreCPU is greater than 3, so the loop condition fails. The semicolon on the end is an empty statement. Everything that follows is in a code block, but it's not the contents of the while loop.

Remove the semicolon and change your condition to allow the loop condition to succeed initially.
For example:

while (scorePlayer < 3 && scoreCPU < 3)

Using a debugger to examine the contents of your variables will help you understand why your loop is not working, and you'll see it step on the semicolon once the condition is right.

Upvotes: 2

Related Questions