dimanche 30 juillet 2023

rock,paper,scissors infinte loop

I'm very new to c#, i was making a simple rock,paper,scissors game everytime i run the code, i get stuck in a loop.

take a look at the code :

using System.ComponentModel.Design;
using System.Runtime.CompilerServices;

Random random = new Random();

bool playagain = true;
String player = "";
int computer;
computer = random.Next(1,4);
Console.WriteLine("Enter, ROCK,PAPER,SCISSORS : ");
player = Console.ReadLine();
player.ToUpper();

while (playagain)
{
    
    
    while (player != "ROCK" && player != "PAPER" && player != "SCISSORS")
    {
        Console.WriteLine("Enter, ROCK,PAPER,SCISSORS : ");
        player = Console.ReadLine();
        player.ToUpper();


    }



    switch (computer) {
        case 1:// computer = rock
            if (player.Equals("ROCK"))
            {
                Console.WriteLine("Computer : Rock");
                Console.WriteLine("It's a tie !");
            }


            else if (player.Equals("PAPER"))
            {

                Console.WriteLine("Computer : ROCK");
                Console.WriteLine("You lose !");


            }
            else {
                Console.WriteLine("Computer : SCISSORS");
                Console.WriteLine("You win !");

            }
            break;
        case 2:// computer = paper
            if (player.Equals("ROCK"))
            {
                Console.WriteLine("Computer : PAPER");
                Console.WriteLine("You lose !");
            }


            else if (player.Equals("PAPER"))
            {

                Console.WriteLine("Computer : PAPER");
                Console.WriteLine("It's  a tie  !");


            }
            else// player = scissors 
            {
                Console.WriteLine("Computer : PAPER");
                Console.WriteLine("You win !");

            }

            break;
        case 3: // computer = scissors 
            if (player.Equals("ROCK"))
            {
                Console.WriteLine("Computer : SCISSORS");
                Console.WriteLine("You win !");
            }


            else if (player.Equals("PAPER"))
            {

                Console.WriteLine("Computer : SCISSORS");
                Console.WriteLine("You lose  !");


            }
            else// player = scissors 
            {
                Console.WriteLine("Computer : SCISSORS");
                Console.WriteLine("It's a tie  !");

            }
            break;



    }

}

evertime i run this code, i get stuck in a loop with the console displaying : Enter, ROCK,PAPER,SCISSORS :.

i tried putting the : Enter, ROCK,PAPER,SCISSORS : text outside of the main while loop, but nothing changed, it's very strange.




Aucun commentaire:

Enregistrer un commentaire