Home > Software engineering >  my guessing game is not working and i don´t know why
my guessing game is not working and i don´t know why

Time:07-19

I'm working on a guessing game that I need to improve for school, and I need to make it so that my game can make more rounds. But I hav a problem, the program doesn´t want to quit, it only starts a new round even if I "make the quit command". The problem now is that my game doesn´t even want to start and I have no idea why. I have tried so many things to try to fix it and nothing works and I need to make it to a do while loop instead. And I think I know how to do that without big problems. Here is my code

using System;

 namespace MyFirstProgram
 {
     class Program
     {
         static void Main(string[] args)
         {
             Random random = new Random();
             bool playAgain = true;
             int min = 1;
             int max = 100;
             int guess;
             int number;
             int guesses;
             String response;

             while (playAgain)
             {
                 guess = 0;
                 guesses = 0;
                 response = "";
                 number = random.Next(min, max   1);

                 while (guess != number)
                 {
                     //opening quote
                     Console.ForegroundColor = ConsoleColor.Green;
                     Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 
 ocn 100, så varsågod..\nskriv in ett tal");
                     guess = Convert.ToInt32(Console.ReadLine());
                     Console.WriteLine("Guess: "   guess);

                     //when guess is over 100
                     if (guess > 100)
                     {
                          Console.ForegroundColor = ConsoleColor.DarkRed;
                          Console.WriteLine("Du måste skriva in ett tal mellan 1 och 
 100!");
                     }
                     else
                     {

                         //if guess is to small
                         if (guess > number)
                         {
                             Console.ForegroundColor = ConsoleColor.DarkRed;
                             Console.WriteLine("Ditt tal är för litet. gissa på ett 
 större tal");
                         }

                         //if guess is to large
                         else if (guess < number)
                         {
                             Console.ForegroundColor = ConsoleColor.DarkRed;
                             Console.WriteLine("Ditt tal är för stort. gissa på ett 
 mindre tal.");
                         }

                         //when your guess is close but not right
                         if (Math.Abs(guess - number) <= 3)
                         {
                             Console.ForegroundColor = ConsoleColor.Red;
                             Console.WriteLine("Du är dock nära och det bränns");
                         }
                     }
                         guesses  ;
                 }
                 //when you won
                 Console.ForegroundColor = ConsoleColor.Yellow;
                 Console.WriteLine("Grattis du gissa rätt,\n talet är"   " "   number);
                 Console.WriteLine("gissning"   " "   guesses);

                 //playagain command
                 Console.ForegroundColor = ConsoleColor.Cyan;
                 Console.WriteLine("vill du spela igen? (Ja/Nej): ");
                 response = Console.ReadLine();
                 response = response.ToUpper();

                 if (response == "Ja")
                 {
                     playAgain = true;
                 }
                 else
                 {
                     playAgain = false;
                 }
             }
             //end quote
             Console.WriteLine("tack för att du spela");

             Console.ReadKey();
         }
     }
 }    

CodePudding user response:

The reason that it doesn't work is that you convert the response to upper case. However you don't check for the upper case version of the word.

So if (response == "Ja") should be if (response == "JA")

CodePudding user response:

Just for the record, usually when you compare string in C#, seek for ways that let you also provide StringComparison.

For example, if I want to check if a string is Ja, but without considering character case, I can write something like this:

    public static void Main()
    {
        string input = "JA";

        if (input.Equals("Ja", StringComparison.InvariantCultureIgnoreCase))
        {
            Console.WriteLine("They're the same!");
        }
    }

// result: They're the same!

By the way, C# Dictionary also supports this.

    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
        
        dict.Add("Ja", "Peko");
        
        Console.WriteLine(dict["JA"]);
    }

// result: Peko

CodePudding user response:

You have line breaks in your strings, which make your code unable to compile:

using System;

namespace MyFirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            bool playAgain = true;
            int min = 1;
            int max = 100;
            int guess;
            int number;
            int guesses;
            String response;

            while (playAgain)
            {
                guess = 0;
                guesses = 0;
                response = "";
                number = random.Next(min, max   1);

                while (guess != number)
                {
                    //opening quote
                    Console.ForegroundColor = ConsoleColor.Green;
/* here ---> */     Console.WriteLine("gissa talet\nDu ska nu gissa ett tal mellan 1 ocn 100, så varsågod..\nskriv in ett tal");
                    guess = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Guess: "   guess);

                    //when guess is over 100
                    if (guess > 100)
                    {
                        Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */         Console.WriteLine("Du måste skriva in ett tal mellan 1 och 100!");
                    }
                    else
                    {
                        //if guess is to small
                        if (guess > number)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */             Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
                        }
                        //if guess is to large
                        else if (guess < number)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkRed;
/* here ---> */             Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal.");
                        }
                        //when your guess is close but not right
                        if (Math.Abs(guess - number) <= 3)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("Du är dock nära och det bränns");
                        }
                    }
                    guesses  ;
                }
                //when you won
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Grattis du gissa rätt,\n talet är"   " "   number);
                Console.WriteLine("gissning"   " "   guesses);

                //playagain command
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("vill du spela igen? (Ja/Nej): ");
                response = Console.ReadLine();
                response = response.ToUpper();

                if (response == "JA")
                {
                    playAgain = true;
                }
                else
                {
                    playAgain = false;
                }
            }
            //end quote
            Console.WriteLine("tack för att du spela");

            Console.ReadKey();
        }
    }
}

Side note: if you guess the right number, you still enter the "guess is close" part (as guess-number == 0 which is <= 3).

See red line in below screenshot:

good guess

CodePudding user response:

Besides your other problems you have a infinite loop hidden somewhere here...

    //if guess is to small
if (guess > number)
{
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("Ditt tal är för litet. gissa på ett större tal");
}

//if guess is to large
else if (guess < number)
{
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("Ditt tal är för stort. gissa på ett mindre tal.");
}

//when your guess is close but not right
if (Math.Abs(guess - number) <= 3)
{
    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Du är dock nära och det bränns");
}

I am sure you can figure it out by yourself from here on!

  •  Tags:  
  • c#
  • Related