mercredi 29 septembre 2021

C# logic problem get to the wrong loop with the same input

Is a simple c# guessing game in which class encapsulates a random number and driver random a number to guess it is too high or too low, the problem is for the driver when it comes to Higher( this one I used to check the guess number is higher than target number or not) it gives me the wrong message if you saw I hardcore the target is 10, and guess number be 11 which guess > target then should go to (db[i].higher) this loop all the time, but it went to else loop after it hits the right path. I have no idea what's wrong with that. ``` status = true; reseted = false; higher = false; //lower = false;

    }
    public bool Query(int guess)
    {

        if (!status|| guess < 0) return false;
        total_round++;

        if (guess > num )
        {
            higher_count++;
            higher = !higher;
            return false;
        }
        if (guess < num)
        {
            lower_count++;
            //!Compare;
            //higher = false;
            Console.WriteLine("get here!\n");
            return false;
        }
        else
        {
            right_count++;
            status = !status;
            return true;
        }
        //return false;
    }

    public bool Higher
    {
        get { return higher;}
    }
``` int input= rnd.Next(5, 10);
        //int round = rnd.Next(5,10);
        int round = 2;
        targetInt[] db = new targetInt[2];
        for (int i = 0; i < db.Length; i++)
        {
            Console.WriteLine("==================New Round Of 
          Game=================");
            db[i] = new targetInt(i);
            int counter = 1;
            while (counter <= round)
            {
                int bound = 0, upperbound = 100;
                //int guess = rnd.Next(bound, upperbound);
                int guess = 11;//guess < target
                //int guess = i;
                Console.WriteLine("Guess number is: " + guess);
                //list[i] = guess;

                if (db[i].Query(guess))
                {
                    Console.WriteLine("Round : " + db[i].TotalRound + " Guess number 
           is: " + guess + "\n");
                    Console.WriteLine("You are right!\n");
                    break;
                }
                else
                {
                    if (!db[i].Status)
                    {
                        Console.WriteLine("You dont have any chance left!");
                        break;
                    }

                    
                    if (db[i].Higher)//guess > num                       
                    {
                        Console.WriteLine(db[i].Higher.ToString());
                        Console.WriteLine("Guess number is too big, guess again");
                        upperbound = guess - 2;
                        Console.WriteLine("Round : " + db[i].TotalRound + " Guess 
          number is: " + guess + "\n");
                        

                    }
                   // if(!db[i].Higher && db[i].Status)//guess < num
                    else
                    {
                        Console.WriteLine(db[i].Higher.ToString());
                        Console.WriteLine("Guess number is too small, guess again");
                        bound = guess + 2;
                        Console.WriteLine("Round : " + db[i].TotalRound + " Guess 
         number is: " + guess + "\n");   
                    }   
                }
                counter++;
            }

            //Console.WriteLine("You dont have any chance left!");
            Console.WriteLine("====================Game 
             Statics====================");
            Console.WriteLine("Total round: " + db[i].TotalRound);
            Console.WriteLine("Right guess count: " + db[i].RightCount);
            Console.WriteLine("Higher guess count: " + db[i].HigherCount);
            Console.WriteLine("lower guess count: " + db[i].LowerCount);
            Console.WriteLine("GAME OVER!!!!\n");
        }
```
>this is output message, the round two should be the same as round 1 since my guess 
```  number is hardcoded 11;
Target: 10
Guess number is: 11
True
Guess number is too big, guess again
 Round : 1 Guess number is: 11

Guess number is: 11
False
Guess number is too small, guess again
Round : 2 Guess number is: 11

Aucun commentaire:

Enregistrer un commentaire