-1

I'm trying to work on an app/small game that is about trying to guess the same number between 1-10 same as the program and win, but after that round you are supposed to be able to play again! My problem is that the program generates the same number everytime. I have looked through other threads on here and none of them seem to help or at least I cannot understand them. How can I fix this?

static void Main(string[] args)
{
   Random random = new Random();
   bool play = true;

   int playnum = random.Next(1, 11);

   while (play)
   {
      Console.Write("\n\tGuess number between 1-10 ");

      int numb;
      bool success = Int32.TryParse(Console.ReadLine(), out numb);

      if (success)
      {
         if (numb < playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tinput number " + numb + " too small, try again.");
         }

         if (numb > playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tinput number " + numb + " too big, try again.");
         }

         if (numb == playnum)
         {
            Console.WriteLine(playnum);
            Console.WriteLine("\tcongrats!");

            bool LoopBreaker = true;
            do
            {
               Console.WriteLine("continue or finish?");
               string input = Console.ReadLine();
               char CharFromTryPass = ' ';
               bool TryparsResult = char.TryParse(input, out CharFromTryPass);

               bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
               if (IsValidChar)
               {
                  Console.WriteLine(" please write A or F!");
               }

               else if (TryparsResult)
               {

                  if (CharFromTryPass == 'A')
                  {
                     Console.WriteLine("see you!");
                     play = false;
                     LoopBreaker = false;
                  }

                  if (CharFromTryPass == 'F')
                  {
                     play = true;
                     LoopBreaker = false;
                  }
               }

            } while (LoopBreaker);

thatguy
  • 21,059
  • 6
  • 30
  • 40
jujuju199
  • 11
  • 2
  • 1
    Where in your code do you generate playNum? Where do you start a new game? You only seem to have one loop, which is play current game, so you probably need to make a new playNum at the point you start a new game. – Rup Jul 17 '20 at 12:33
  • 1
    You only ever generate a single random number. Fire up your debugger and see what that code actually does. Also `random.Next(1, 11);` will never ever generate a number greater than 10 - title says 100 – Ňɏssa Pøngjǣrdenlarp Jul 17 '20 at 12:34
  • Put `int playnum = random.Next(1, 11);` inside of while loop – Max Jul 17 '20 at 12:37
  • Inside the game if you 'win' aka guess the same number as the program you get two choices, either to keep going or quit... (also sorry yes its supposed to be 10 not 100) – jujuju199 Jul 17 '20 at 12:50
  • sorry guys im very much a newbie at coding but appreciate each answer very much! i mightve found the asnwer by daniel!! seems to work THANK YOU – jujuju199 Jul 17 '20 at 12:55

2 Answers2

1

Your code is a bit messy, you should brake it down to smaller methods. But basically you just need to generate a new number after the person guesses the correct one. So add this after the "congrats!" line.

playnum = random.Next(1, 11);

.

static void Main(string[] args)
        {
            Random random = new Random();
            bool play = true;

            int playnum = random.Next(1, 11);

            while (play)
            {
                Console.Write("\n\tGuess number between 1-10 ");

                int numb;
                bool success = Int32.TryParse(Console.ReadLine(), out numb);

                if (success)
                {
                    if (numb < playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tinput number " + numb + " too small, try again.");
                    }

                    if (numb > playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tinput number " + numb + " too big, try again.");
                    }

                    if (numb == playnum)
                    {
                        Console.WriteLine(playnum);
                        Console.WriteLine("\tcongrats!");
                        playnum = random.Next(1, 11);

                        bool LoopBreaker = true;
                        do
                        {
                            Console.WriteLine("continue or finish?");
                            string input = Console.ReadLine();
                            char CharFromTryPass = ' ';
                            bool TryparsResult = char.TryParse(input, out CharFromTryPass);

                            bool IsValidChar = (CharFromTryPass != 'A' && CharFromTryPass != 'F');
                            if (IsValidChar)
                            {
                                Console.WriteLine(" please write A or F!");
                            }

                            else if (TryparsResult)
                            {

                                if (CharFromTryPass == 'A')
                                {
                                    Console.WriteLine("see you!");
                                    play = false;
                                    LoopBreaker = false;
                                }

                                if (CharFromTryPass == 'F')
                                {
                                    play = true;
                                    LoopBreaker = false;
                                }
                            }

                        } while (LoopBreaker);
                    }
                }
            }
        }
Daniel Björk
  • 2,475
  • 1
  • 19
  • 26
0

You cannot generate a new number because you call the random number only once in your code. Put your int playnum = random.Next(1, 11); inside of your loop and it should be fine. As Nyssa already said, you may should use your debugger to understand what your code is doing wrong.

Davis Jahn
  • 441
  • 5
  • 17
  • i tried debugging but nothing comes up :/ also i should mention that when i put that line of code inside the loop it generates different numbers every time i put a guess inside that round, instead a new number every round/ match. – jujuju199 Jul 17 '20 at 12:48
  • I think you should consider to clearify what is actually a round in that case. I had the opinion that a round is over after every guess. – Davis Jahn Jul 17 '20 at 12:51
  • yes! by round i meant every guess till you get it right, and after that you either start a new one or close! sorry for the confusion – jujuju199 Jul 17 '20 at 13:04
  • You really need to specify where in the loop it should go because they should not be generating a new number between each guess. – juharr Jul 17 '20 at 14:04