none
Mystery Number Guessing Games with Two Classes RRS feed

  • Question

  • I am trying to create a Number Guessing Game and I'm a little stuck...I have to create a Mystery Number Guessing Game that has two classes, one class being a test class. I have the following so far but its all on one Class(test). I am also having trouble with ending the application after it reaches its 5th attempt. My application needs to include:

    1. Two Classes

    2. A constructor that creates a random integer between 1-100 stored in an instance variable

    3. At least one public Method

    4. At least one public property

    This is what I have so far.

    using System;


    class MysteryNumber
    {
           
      public int RNumber { get; }// auto implemented property

        private int random;//instance variable

        public MysteryNumber(int myRand)//constructor
        {
            MyRand = myRand;
        } 



    }

                                   

    =====================SEPARATE CLASS=============================

    using System;

    class MysteryNumberTest
    {
        static void Main()
        {
            int myRand;

            Random randomNumbers = new Random();
            myRand = randomNumbers.Next(1, 100);
            int numberOfGuessesLeft = 4;
            int numberOfGuessesMade = 1;
            while (numberOfGuessesLeft <= 5)
            {
                Console.Write("Enter A Number Between 1 and 100: ");
                int guess = int.Parse(Console.ReadLine());
                guess = numberOfGuessesMade;

                if (numberOfGuessesMade == myRand)
                {
                    Console.WriteLine($"Good Job! It only took {numberOfGuessesMade}");
                }
                else if (numberOfGuessesMade != myRand)
                {
                    Console.WriteLine($"Sorry, Try Again. This is guess number {numberOfGuessesMade}, you have {numberOfGuessesLeft} left.");
                    numberOfGuessesLeft = numberOfGuessesLeft - 1;
                    numberOfGuessesMade = numberOfGuessesMade + 1;
                }


            }

             Console.ReadLine();
        }


    }

    Friday, October 18, 2019 6:30 PM

All replies

  • Obviously this is a class assignment, aka homework. So the best answer is to help you do it yourself.

    I do not see how RNumber changes.

    I do not see any declaration of MyRand (in MysteryNumber).

    You should use int.TryParse instead of int.Parse, but for a beginner you can keep it simple and use what you have. Just be aware that int.TryParse should be avoided.

    (Update: that should be int.Parse should be avoided.)

    Look again at:

    if (numberOfGuessesMade == myRand)

    And:

    if (numberOfGuessesMade != myRand)

    I really doubt that they are doing what you want to do.



    Sam Hobbs
    SimpleSamples.Info


    Friday, October 18, 2019 7:05 PM

  • Just be aware that int.TryParse should be avoided.


    Sam -

    I'm not sure that statement says what you intended. Was there a typo?

    - Wayne

    Saturday, October 19, 2019 9:29 AM
  • The idea behind two classes is separation of concerns. You should separate the logic of the game from the UI. It is also called single responsibility principle, which is part of the SOLID canon.

    The commented code in your Main() method belongs to your MysteryNumber class, because it's logic. The rest is UI in should stay in the main method:

    using System;
    
    class MysteryNumberTest
    {
        static void Main()
        {
    //        int myRand;
    //        Random randomNumbers = new Random();
    //        myRand = randomNumbers.Next(1, 100);
    //        int numberOfGuessesLeft = 4;
    //        int numberOfGuessesMade = 1;
    //        while (numberOfGuessesLeft <= 5)
    //        {
                Console.Write("Enter A Number Between 1 and 100: ");
                int guess = int.Parse(Console.ReadLine());
    //            guess = numberOfGuessesMade;
    //            if (numberOfGuessesMade == myRand)
    //            {
                    Console.WriteLine($"Good Job! It only took {numberOfGuessesMade}");
    //            }
    //            else if (numberOfGuessesMade != myRand)
    //            {
                    Console.WriteLine($"Sorry, Try Again. This is guess number {numberOfGuessesMade}, you have {numberOfGuessesLeft} left.");
    //                numberOfGuessesLeft = numberOfGuessesLeft - 1;
    //                numberOfGuessesMade = numberOfGuessesMade + 1;
    //            }
    //      }
    
             Console.ReadLine();
        }
    }

    Saturday, October 19, 2019 9:54 AM
  • Stefan's explanation of separation of concerns is spot-on. I'm guessing that's why your instructor wants you to do this using two classes.
     
    Here are a few pointers:
     
    1) The MysteryClass should have all the functionality for creating the random number and a Method for determining if the user guesses the correct number.
     
    2) The Main() method should do the UI (the Console Reads and Writes) and also the loop for number of tries (calling the Method in the MysteryClass to see if the guess was correct or not). Don't forget to also create an instance of the MysteryClass before the loop!
     
    Let us know how you're doing!   =0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, October 19, 2019 6:00 PM
    Moderator
  • According to point 2:

    The input loop belongs the UI. Counting and signaling an abort like too much tries belongs to the logic class.

    The outline can look like:

    Main {
      initializeMysteryClass
      loop {
        getImput
        convertToNumber
        if mysteryClass.testGuess {
          outputWin
          break
        }
    
        if mysteryClass.tooMuchTries {
          outputTooMuchTries
          break
        } else {
          outputTriesLeft( mysteryClass.triesLeft )
        }
      }
    }
    

    Saturday, October 19, 2019 7:37 PM

  • Just be aware that int.TryParse should be avoided.


    Sam -

    I'm not sure that statement says what you intended. Was there a typo?

    - Wayne

    Obviously I did not double-check what I wrote; I usually do. I will add an update to my previous reply.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, October 19, 2019 7:54 PM
  • Hope my solution could be helpful.

    It does not help GrianB if they use the code and get in trouble for plagiarizing. Are you familiar with that?

    Quite often it is easier to just write code than to try to help the student understand how to do it themselves. For class assignments the usual response is to not do the entire class assignment.



    Sam Hobbs
    SimpleSamples.Info

    Monday, October 21, 2019 3:33 AM
  • I agree with Sam!

    Timon, you should *not* provide a full working example to a student who is learning to code.

    GrianB obviously was asking questions about a homework assignment. Besides, your example, although it works, doesn't really have the "separation of concerns" paradigm that the teacher/professor was obviously looking for.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, October 21, 2019 5:02 AM
    Moderator