Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By kneitzel
  • 1 Post By kneitzel

Thread: Updating limits for random int values?

  1. #1
    Cellie is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Updating limits for random int values?

    Hello! I have to complete the following assignment:

    Write a subclass of BasicGame in which the computer asks the user to choose a whole number from 1 to 100, inclusive. The program then makes a number of guesses. After each guess, the user tells whether the guess is too high, too low, or exactly right. Once the computer "knows" what the correct number is, the computer tells the user the correct number and the number of guesses tried. Have the program ask the user after each game whether she wants to play again. Design the program so that it always guesses the correct answer by the seventh try at latest. Hint: Have two instance variables that keep track of the smallest and largest values that the guess could be (initially 1 and 100). After each wrong guess, update these smallest and largest values appropriately.


    This is what I have written for the assignment:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class GuessThatNumber extends BasicGame
    {
        private java.util.Random randy;
        private int itsCompNumber;
        private int itsHigh = 99;
        private int itsLow = 1;
        
        public GuessThatNumber()
        {   super();
            randy = new java.util.Random();
        }   //===================
        
        public void playManyGames()  
        {    do
              {    playOneGame();
              }while (JOptionPane.showConfirmDialog (null, "Play again?")
                             == JOptionPane.YES_OPTION);
        }  //======================
        
        public void playOneGame()
        {   JOptionPane.showMessageDialog (null, "Choose a number from 1 to 100, inclusive.");
            guessUsersNumber();
        }   //==================
        
        public void guessUsersNumber()
        {   itsCompNumber = randy.nextInt((itsHigh + itsLow + 1) / 2);
            continueGame();
        }
        
        public void continueGame()
        {   if (JOptionPane.showConfirmDialog (null, "Is " + itsCompNumber + " correct?")
                                != JOptionPane.YES_OPTION)
                {   String a = JOptionPane.showInputDialog ("Was it too high or too low?");
                    if (a.equals ("too high"))
                    {   itsHigh = itsCompNumber;
                    }
                    if (a.equals ("too low"))
                    {   itsLow = itsCompNumber;
                    }
                    guessUsersNumber();
                }
                else
                    showFinalStatus();
        }   //================
    
        public void showFinalStatus()
        {   JOptionPane.showMessageDialog (null, "The computer has guessed correctly!");
        }
    }

    The part I'm having trouble with is changing the high and low values based on the input of "too high" or "too low," which I tried in the continueGame method... When I test the game, the values for itsCompNumber aren't restricted by the user's input at all, and I'm not sure how to fix it.
    Last edited by Cellie; 05-04-2014 at 05:15 PM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: Updating limits for random int values?

    Hi,

    I think your calculation oft the random number is wrong. You want to geht a random number which is between your low and high mark but not the marks itself.

    So if the low mark is five and the high one is 10, you want a guess of the range 6 ... 9.

    NextInt gives a value of 0..n so you have to translate the range.
    (Low+1 ... High-1) is the same as
    (0 ... High-Low) + low+ 1
    Cellie likes this.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    7

    Default Re: Updating limits for random int values?

    And two more points which are not directly your question but I want to mention them:
    a) in your variables for the limits, you store the value that was to high or to low. So you should initialize them with values that are to low or to high.
    1 and 99 seems both wrong in my eyes.
    b) you build your solution through a recursion. That is valid and works, but I would have used a loop construct. A recursion over 2 methods is hard to read ...

    Konrad
    Cellie likes this.

  4. #4
    Cellie is offline Member
    Join Date
    May 2014
    Posts
    5
    Rep Power
    0

    Default Re: Updating limits for random int values?

    Ah, I see. I changed it & it seems to be picking up the changes in low and high values now. :)

    I noticed the 99 and 1 issue and changed them to 101 and 0, but I forgot to update here - sorry about that. I'll keep your advice in mind. Thank you!

Similar Threads

  1. Filling array with 10 random values
    By quinnvanorder in forum New To Java
    Replies: 6
    Last Post: 05-26-2012, 10:03 PM
  2. Issue with filling array full of random values
    By quinnvanorder in forum New To Java
    Replies: 5
    Last Post: 05-20-2012, 02:04 AM
  3. Replies: 9
    Last Post: 03-03-2011, 12:52 AM
  4. Replies: 6
    Last Post: 08-26-2010, 03:26 PM
  5. Replies: 17
    Last Post: 07-25-2010, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •