dimanche 30 juillet 2023

rock,paper,scissors gone wrong. c#

i'm a beginner in c#, i was making a simple rock,paper,scissors game, when the code was done i ran it, and the problem is that each time i pick rock,paper or scissors i get stuck in a loop where the console displays the computer pick, and if I won or lost (or if it's a tie).

here is a look at my stuckable, unsuccesful 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 = player.ToUpper();

while (playagain == true)
{
    
    
    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;

            

    }


    }
    Console.WriteLine("Do you want to play again? (yes/no)");
    String response = Console.ReadLine().ToLower();
    playagain = response == "yes";
    if (playagain.Equals("yes"))
        {
        Console.WriteLine("Enter, ROCK,PAPER,SCISSORS : ");
        player = Console.ReadLine();



        }

can anyone solve this, it's prolly an easy fix, but i'm to new to figure it out....

i tried moving the part where the player is asked if he want to continue tha game or not, specifcally this portion of the code :

    Console.WriteLine("Do you want to play again? (yes/no)");
    String response = Console.ReadLine().ToLower();
    playagain = response == "yes";
    if (playagain.Equals("yes"))
        {
        Console.WriteLine("Enter, ROCK,PAPER,SCISSORS : ");
        player = Console.ReadLine();



        }

nothing changed.




Aucun commentaire:

Enregistrer un commentaire