Results 1 to 16 of 16
  1. #1
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Help With Random Number Guessing Game

    Ooookay so I've been editing this thing like crazy and the errors keep changing and I am just getting very confused so if anyone could offer some advice as to how I go about fixing this would be greatly appreciated.

    a. Seven instance variables.
    i. answer - an integer representing the randomly generated number.
    ii. generator a random Generator object (create from the Java API Random class) (pg 250 in text)
    iii. gameOver a Boolean, false if game still in progress, true if the game is over.
    iv. differential an integer representing the difference between a guess and the answer.
    v. max maximum value of the number to guess. For example, if the maximum number is 100 then the number to guess would be between 0 and 100.(inclusive)
    vi. maxGuessesAllowed the maximum number of guesses the user gets, once this value is passed the game is over.
    vii. numGuessesTaken an integer that stores the number of guessed taken so far in any game.


    b. Constructor and Methods
    i. Default Constructor
    1. Sets max to zero
    2. Creates the random number generator object.
    ii. Parameterized Constructor
    1. Takes an integer parameter representing the maximum value of the number to guess.
    2. Creates the random number generator object.
    iii. newGame method
    1. Takes in an integer as a parameter representing the maximum number of guesses and sets maxGuessesAllowed . In other words the parameter represents how many guesses the user gets before the game is over.
    2. Generates the answer using the random number generator.(0 - max).
    3. Sets gameOver to false.
    4. Sets differential to the max value.
    5. Sets numGuessTaken to zero.
    iv. guess method
    1. Takes in an integer as a parameter representing a new guess.
    2. Compares the new guess with the answer and generates and returns a String representing an appropriate response.
    3. The response is based on:
    a. The relation of the guess and answer (too high, too low or correct).
    b. The comparison of difference between the current guess and the answer and the previous guess and the answer. (warmer, colder)
    c. Guess out of range error, if the guess is not between 0 and the max number (inclusive) (see sample run below)
    d. User has taken too many guess because numGuessesTaken is greater than maxGuessesAllowed. If this is the case set isGameOver to true.
    v. isGameOver method - returns the state of game.
    1. true if game is over
    2. false if still in progress.
    vi. Accessor and mutator methods for all instance fields except the Random number generator. Use the Accessor or mutator methods within the other method of the class rather than directly accessing the instance fields.


    4) Design and build GuessingGameTester class

    a. This program will create GuessingGame objects and completely test the GuessingGame Class.
    b. The tester will also provide two loops.
    c. The first loop will allow the user to play a new game after the previous game is completed.
    d. The second or nested loop will prompt the user for a new guess and provide a response based on the guess.




    Java Code:
     import java.util.Random;
    
       public class GuessingGame
       {
          private int answer;
          private Random generator;
          private boolean gameOver;
          private int differential;
          private int max;
          private int maxGuessesAllowed;
          private int numGuessesTaken;
       
          public GuessingGame ()
          {
             max = 0;
             Random generator = new Random();
             int number = generator.nextInt(101);
          }
       	
          public GuessingGame(int newMax)
          {
             max = newMax;
          }
       	
          public void newGame (int numGuesses)
          {
             maxGuessesAllowed = numGuesses;
             Random generator = new Random();
             gameOver = false;
             differential = 100;
             numGuessesTaken = 0;
          }
    		
    		
       	
          public String guess (int newGuess)
          {
             differential = answer - newGuess;
             String guess = "";
             String closer = "";
             int currentDiff = answer - differential;
             for (numGuessesTaken = 0; 
             numGuessesTaken <= 6;
             numGuessesTaken++)
             {
                if (newGuess==answer)
                {
                   guess = "You won!";
                }
                else if (newGuess < answer && currentDiff > differential)
                {
                   guess = "Too low";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess < answer && currentDiff < differential)
                {
                   guess = "Too low";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff > differential)
                {
                   guess = "Too high";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff < differential)
                {
                   guess = "Too high";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if(newGuess < 0 || newGuess > 100)
                {
                   guess = "Guess out of range.";
                   newGuess++;
                }
                else if (newGuess==answer || numGuessesTaken > max)
                {
                   guess = "Game Over.";
                }
             	
             	
             }
          }
             
          public boolean isGameOver(int newGuess)
          {
             if (newGuess !=answer || numGuessesTaken > max)
             {
                gameOver = false;
             
             }
             else
             {
                gameOver = true;
             }
             return gameOver;
          }
       		
          public int getAnswer()
          {
             return answer;
          }
             
             
             
             
                
          
       
       
       }
    tester:
    Java Code:
     import java.util.Scanner;
    
    public class GuessingGameTester
    {
    public static void main(String[] args)
    {
    GuessingGame gameOne = new GuessingGame(5);
    gameOne.answer = (int) (generator.random()*100);
    System.out.println(gameOne.answer);
    
    System.out.println("Guess a number between 0 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    Scanner input = new Scanner(System.in);
    gameOne.newGuess = input.nextInt();
    String playerGame1 = gameOne.guess(gameOne.newGuess);
    System.out.println(playerGame1);
    
    boolean gameOver = false;
    while (!gameOver)
    {
    System.out.println("Would you like to play again? Enter Y for yes or N for no.");
    String playerResponse = input.next()
    
    if(playerResponse.equalsIgnoreCase("Y"))
    {
    GuessingGame gameTwo = new GuessingGame(5);
    gameTwo.answer = (int)(generator.random()*100)+1;
    System.out.println(gameTwo.answer);
    System.out.println("Guess a number between 1 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    gameTwo.newGame(gameTwo.newGuess);
    gameTwo.newGuess = input.nextInt();
    String playerGame2 = gameTwo.guess(gameTwo.newGame(gameTwo.newGuess));
    System.out.println(playerGame2);
    }
    else
    {
    System.out.println("Goodbye");
    }
    }
    }
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Help With Random Number Guessing Game

    What are the errors? Post the whole stack trace.
    Get in the habit of using standard Java naming conventions!

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    public String guess(int newGuess) doesn't return a type String, you need to fix that. If you want to return a string do it, if it won't return anything then void.
    In your tester you are calling the generator which is in your GuessingGame class but it can't be resolved like that.
    you should make a method getGenerator() that returns your generator and say gameOne.getGenerator().random() ... in the way
    you are doing it, it cannot be resolved.

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    Just spotted more errors. Random generator doesn't have a random method. You can't call generator.random(). Maybe you want Math.random() ?
    Also, you create an instance of GuessingGame and then you do something weird; gameOne.newGuess = ... newGuess is not a field. I don't know what you are trying to do with that.

  5. #5
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    So I fixed the String to have a return statement but now my class is getting the follow errors:
    Java Code:
     ----jGRASP exec: javac -g GuessingGame.java
    
    GuessingGame.java:88: error: illegal start of expression
          public boolean isGameOver(int newGuess)
          ^
    GuessingGame.java:88: error: ';' expected
          public boolean isGameOver(int newGuess)
                                   ^
    GuessingGame.java:88: error: ';' expected
          public boolean isGameOver(int newGuess)
                                                ^
    GuessingGame.java:102: error: illegal start of expression
          public int getAnswer()
          ^
    GuessingGame.java:102: error: ';' expected
          public int getAnswer()
                              ^
    GuessingGame.java:107: error: reached end of file while parsing
         }    
          ^
    6 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation com

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    it is obvious you are missing some semicolons and maybe some curly brackets.

  7. #7
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    I understand that's what it is saying but I've looked through this thing several times and I am not understanding where I am missing the semi-colons.

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    and I am supposed to guess where the syntax errors are? Post your updated code
    But use eclipse or netbeans to spot mistakes like these easily

  9. #9
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    Ok so here are my updated codes, my class method is still getting a slew of "error: class, interface or enum expected" errors and I have gone through and counted the brackets and added a couple that I was missing and now I have more errors then before I did that I'm becoming completely at a loss as to what its asking me to fix. I cannot use any other program aside from jGrasp to do this because that is what our professor wants us to use.
    Java Code:
    import java.util.Random;
    
       public class GuessingGame
       {
          private int answer;
          private Random generator;
          private boolean gameOver;
          private int differential;
          private int max;
          private int maxGuessesAllowed;
          private int numGuessesTaken;
       
          public GuessingGame ()
          {
             max = 0;
             Random generator = new Random();
             int number = generator.nextInt(101);
          }
       	
          public GuessingGame(int newMax)
          {
             max = newMax;
          }
       	
          public void newGame (int numGuesses)
          {
             maxGuessesAllowed = numGuesses;
             Random generator = new Random();
             gameOver = false;
             differential = 100;
             numGuessesTaken = 0;
          }
       	
       	}
       	
          public String guess (int newGuess)
          {
             differential = answer - newGuess;
             String guess = "";
             String closer = "";
             int currentDiff = answer - differential;
             for (numGuessesTaken = 0; 
             numGuessesTaken <= maxGuessesAllowed;
             numGuessesTaken++)
             {
                if (newGuess==answer)
                {
                   guess = "You won!";
                }
                else if (newGuess < answer && currentDiff > differential)
                {
                   guess = "Too low";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess < answer && currentDiff < differential)
                {
                   guess = "Too low";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff > differential)
                {
                   guess = "Too high";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff < differential)
                {
                   guess = "Too high";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if(newGuess < 0 || newGuess > 100)
                {
                   guess = "Guess out of range.";
                   newGuess++;
                }
                else if (newGuess==answer || numGuessesTaken > max)
                {
                   guess = "Game Over.";
                }
                return guess;
             	{
             }
          
             }
          public boolean isGameOver(int newGuess)
          {
             if (newGuess !=answer || numGuessesTaken > max)
             {
                gameOver = false;
             
             }
             else
             {
                gameOver = true;
             }
             return gameOver;
          }
    		}
       		
          public int getAnswer()
          {
             return answer;
          }
    		
    	
    		public void setanswer(int newAnswer)
    		{
    		answer = newAnswer;
    		}
    		
    		public void setgameOver(boolean newgameOver)
    		{
    		gameOver = newgameOver;
    		}
    	
    		
    	public getdifferential()
    	{
    	return differential;
    	}
    	
    	public void setdifferential (int newDifferential)
    	{
    	differential = newDifferential;
    	}
    	
    	public getmax()
    	{
    	return max;
    	}
    	
    	public void setmax (int newMax)
    	{
    	max = newMax;
    	}
    	
    	public getmaxGuessesAllowed()
    	{
    	return maxGuessesAllowed;
    	}
    	
    	public void setmaxGuessesAllowed(int newmaxGuessesAllowed)
    	{
    	maxGuessesAllowed = newmaxGuessesAllowed;
    	}
    	
    	public getnumGuessesTaken()
    	{
    	return numGuessesTaken;
    	}
    	
    	public void setnumGuessesTaken(int newnumGuessesTaken)
    	{
    	numGuessesTaken = newnumGuessesTaken;
    	}
             
         }

    Here is what I have for the tester, I am not sure if I am using the Random generator correctly but I changed it from what I had before because I was trying to use it as Math.random and that is not what we are supposed to be doing we are to only use the Random Generator method. I am getting two errors with this tester,
    Java Code:
    GuessingGameTester.java:9: error: ';' expected
    gameOne.getanswer = Random generator = new Random(101);
                              ^
    GuessingGameTester.java:29: error: ';' expected
    gameTwo.getanswer = Random generator = new Random(101);
    I am certain the semi-colons here are in the correct spots so yet again I don't understand what its asking me to fix.

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class GuessingGameTester
    {
    public static void main(String[] args)
    {
    GuessingGame gameOne = new GuessingGame(5);
    gameOne.getanswer = Random generator = new Random(101);
    System.out.println(gameOne.getanswer);
    
    System.out.println("Guess a number between 0 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    Scanner input = new Scanner(System.in);
    gameOne.newGuess = input.nextInt();
    String playerGame1 = gameOne.guess(gameOne.newGuess);
    System.out.println(playerGame1);
    
    boolean gameOver = false;
    while (!gameOver)
    {
    System.out.println("Would you like to play again? Enter Y for yes or N for no.");
    String playerResponse = input.next();
    
    if(playerResponse.equalsIgnoreCase("Y"))
    {
    GuessingGame gameTwo = new GuessingGame(5);
    gameTwo.getanswer = Random generator = new Random(101);
    System.out.println(gameTwo.getanswer);
    System.out.println("Guess a number between 1 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    gameTwo.newGame(gameTwo.newGuess);
    gameTwo.newGuess = input.nextInt();
    String playerGame2 = gameTwo.guess(gameTwo.newGame(gameTwo.newGuess));
    System.out.println(playerGame2);
    }
    else
    {
    System.out.println("Goodbye");
    }
    }
    }
    }
    Any help would be greatly greatly appreciated. The teacher's assistant said that I only had one error in the tester and a couple in the class keeping me from finishing this so I would definitely love help realizing what I am doing wrong so I can see it completed.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help With Random Number Guessing Game

    The right curly bracket at line #34 doesn' belong there.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    Ok so I updated my code and now I am getting these three errors for my class:
    Java Code:
    GuessingGame.java:90: error: illegal start of expression
          public boolean isGameOver (int newGuess)
          ^
    GuessingGame.java:90: error: ';' expected
          public boolean isGameOver (int newGuess)
                                   ^
    GuessingGame.java:90: error: ';' expected
          public boolean isGameOver (int newGuess)
    Java Code:
    import java.util.Random;
    
       public class GuessingGame
       {
          private int answer;
          private Random generator;
          private boolean gameOver;
          private int differential;
          private int max;
          private int maxGuessesAllowed;
          private int numGuessesTaken;
       
          public GuessingGame ()
          {
             max = 0;
             Random generator = new Random();
             int number = generator.nextInt(101);
          }
       	
          public GuessingGame(int newMax)
          {
             max = newMax;
          }
       	
          public void newGame (int numGuesses)
          {
             maxGuessesAllowed = numGuesses;
             Random generator = new Random();
             gameOver = false;
             differential = 100;
             numGuessesTaken = 0;
          }
       	
       	
       	
          public String guess (int newGuess)
          {
             differential = answer - newGuess;
             String guess = "";
             String closer = "";
             int currentDiff = answer - differential;
             for (numGuessesTaken = 0; 
             numGuessesTaken <= maxGuessesAllowed;
             numGuessesTaken++)
             {
                if (newGuess==answer)
                {
                   guess = "You won!";
                }
                else if (newGuess < answer && currentDiff > differential)
                {
                   guess = "Too low";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess < answer && currentDiff < differential)
                {
                   guess = "Too low";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff > differential)
                {
                   guess = "Too high";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff < differential)
                {
                   guess = "Too high";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if(newGuess < 0 || newGuess > 100)
                {
                   guess = "Guess out of range.";
                   newGuess++;
                }
                else if (newGuess==answer || numGuessesTaken > max)
                {
                   guess = "Game Over.";
                }
                return guess;
             	{
             }
    			}
    			
          
             
     
          {
             if (newGuess !=answer || numGuessesTaken > max)
             {
                gameOver = false;
             
             }
             else
             {
                gameOver = true;
             }
             return gameOver;
          }
    		}
       		
          public int getAnswer()
          {
             return answer;
          }
    		
    	
    		public void setanswer(int newAnswer)
    		{
    		answer = newAnswer;
    		}
    		
    		public void setgameOver(boolean newgameOver)
    		{
    		gameOver = newgameOver;
    		}
    	
    		
    	public int getdifferential()
    	{
    	return differential;
    	}
    	
    	public void setdifferential (int newDifferential)
    	{
    	differential = newDifferential;
    	}
    	
    	public int getmax()
    	{
    	return max;
    	}
    	
    	public void setmax (int newMax)
    	{
    	max = newMax;
    	}
    	
    	public int getmaxGuessesAllowed()
    	{
    	return maxGuessesAllowed;
    	}
    	
    	public void setmaxGuessesAllowed(int newmaxGuessesAllowed)
    	{
    	maxGuessesAllowed = newmaxGuessesAllowed;
    	}
    	
    	public int getnumGuessesTaken()
    	{
    	return numGuessesTaken;
    	}
    	
    	public void setnumGuessesTaken(int newnumGuessesTaken)
    	{
    	numGuessesTaken = newnumGuessesTaken;
    	}
             
         }

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    Line 101: you return a boolean instead of a string as you have declared in the method's signature.

  13. #13
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    Ok so update...my class officially compiles fine however my tester now gives me a SLEW of errors :(
    Java Code:
    GuessingGameTester.java:9: error: cannot find symbol
    gameOne.getanswer = generator = new Random(101);
           ^
      symbol:   variable getanswer
      location: variable gameOne of type GuessingGame
    GuessingGameTester.java:9: error: cannot find symbol
    gameOne.getanswer = generator = new Random(101);
                        ^
      symbol:   variable generator
      location: class GuessingGameTester
    GuessingGameTester.java:10: error: cannot find symbol
    System.out.println(gameOne.getanswer);
                              ^
      symbol:   variable getanswer
      location: variable gameOne of type GuessingGame
    GuessingGameTester.java:16: error: cannot find symbol
    gameOne.newGuess = input.nextInt();
           ^
      symbol:   variable newGuess
      location: variable gameOne of type GuessingGame
    GuessingGameTester.java:17: error: cannot find symbol
    String playerGame1 = gameOne.guess(gameOne.newGuess);
                                              ^
      symbol:   variable newGuess
      location: variable gameOne of type GuessingGame
    GuessingGameTester.java:29: error: cannot find symbol
    gameTwo.getanswer = generator = new Random(101);
           ^
      symbol:   variable getanswer
      location: variable gameTwo of type GuessingGame
    GuessingGameTester.java:29: error: cannot find symbol
    gameTwo.getanswer = generator = new Random(101);
                        ^
      symbol:   variable generator
      location: class GuessingGameTester
    GuessingGameTester.java:30: error: cannot find symbol
    System.out.println(gameTwo.getanswer);
                              ^
      symbol:   variable getanswer
      location: variable gameTwo of type GuessingGame
    GuessingGameTester.java:34: error: cannot find symbol
    gameTwo.newGame(gameTwo.newGuess);
                           ^
      symbol:   variable newGuess
      location: variable gameTwo of type GuessingGame
    GuessingGameTester.java:35: error: cannot find symbol
    gameTwo.newGuess = input.nextInt();
           ^
      symbol:   variable newGuess
      location: variable gameTwo of type GuessingGame
    GuessingGameTester.java:36: error: cannot find symbol
    String playerGame2 = gameTwo.guess(gameTwo.newGame(gameTwo.newGuess));
                                                              ^
      symbol:   variable newGuess
      location: variable gameTwo of type GuessingGame
    11 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation
    My class:
    Java Code:
     import java.util.Random;
    
       public class GuessingGame
       {
          private int answer;
          private Random generator;
          private boolean gameOver;
          private int differential;
          private int max;
          private int maxGuessesAllowed;
          private int numGuessesTaken;
       
          public GuessingGame ()
          {
             max = 0;
             generator = new Random();
             int number = generator.nextInt(101);
          }
       	
          public GuessingGame(int newMax)
          {
             max = newMax;
          }
       	
          public void newGame (int numGuesses)
          {
             maxGuessesAllowed = numGuesses;
             generator = new Random();
             gameOver = false;
             differential = 100;
             numGuessesTaken = 0;
          }
       	
    		public String guess (int newGuess)
          {
             differential = answer - newGuess;
             String guess = "";
             String closer = "";
             int currentDiff = answer - differential;
             for (numGuessesTaken = 0; 
             numGuessesTaken <= maxGuessesAllowed;
             numGuessesTaken++)
             {
                if (newGuess==answer)
                {
                   guess = "You won!";
                }
                else if (newGuess < answer && currentDiff > differential)
                {
                   guess = "Too low";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess < answer && currentDiff < differential)
                {
                   guess = "Too low";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff > differential)
                {
                   guess = "Too high";
                   closer = "Getting colder";
                   newGuess++;
                }
                else if (newGuess > answer && currentDiff < differential)
                {
                   guess = "Too high";
                   closer = "Getting warmer";
                   newGuess++;
                }
                else if(newGuess < 0 || newGuess > 100)
                {
                   guess = "Guess out of range.";
                   newGuess++;
                }
                else if (newGuess==answer || numGuessesTaken > max)
                {
                   guess = "Game Over.";
                }
                return guess;
                
             }
          	{
                if (newGuess !=answer || numGuessesTaken > max)
                {
                   gameOver = false;
                
                }
                else
                {
                   gameOver = true;
                }
                return guess;
             }
          }
       		
          public int getAnswer()
          {
             return answer;
          }
       	
       
          public void setanswer(int newAnswer)
          {
             answer = newAnswer;
          }
       	
          public void setgameOver(boolean newgameOver)
          {
             gameOver = newgameOver;
          }
       
       	
          public int getdifferential()
          {
             return differential;
          }
       
          public void setdifferential (int newDifferential)
          {
             differential = newDifferential;
          }
       
          public int getmax()
          {
             return max;
          }
       
          public void setmax (int newMax)
          {
             max = newMax;
          }
       
          public int getmaxGuessesAllowed()
          {
             return maxGuessesAllowed;
          }
       
          public void setmaxGuessesAllowed(int newmaxGuessesAllowed)
          {
             maxGuessesAllowed = newmaxGuessesAllowed;
          }
       
          public int getnumGuessesTaken()
          {
             return numGuessesTaken;
          }
       
          public void setnumGuessesTaken(int newnumGuessesTaken)
          {
             numGuessesTaken = newnumGuessesTaken;
          }
             
       }
    My tester:

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class GuessingGameTester
    {
    public static void main(String[] args)
    {
    GuessingGame gameOne = new GuessingGame(5);
    gameOne.getanswer = generator = new Random(101);
    System.out.println(gameOne.getanswer);
    
    System.out.println("Guess a number between 0 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    Scanner input = new Scanner(System.in);
    gameOne.newGuess = input.nextInt();
    String playerGame1 = gameOne.guess(gameOne.newGuess);
    System.out.println(playerGame1);
    
    boolean gameOver = false;
    while (!gameOver)
    {
    System.out.println("Would you like to play again? Enter Y for yes or N for no.");
    String playerResponse = input.next();
    
    if(playerResponse.equalsIgnoreCase("Y"))
    {
    GuessingGame gameTwo = new GuessingGame(5);
    gameTwo.getanswer = generator = new Random(101);
    System.out.println(gameTwo.getanswer);
    System.out.println("Guess a number between 1 and 100");
    System.out.println("Number of guesses allowed: 5");
    
    gameTwo.newGame(gameTwo.newGuess);
    gameTwo.newGuess = input.nextInt();
    String playerGame2 = gameTwo.guess(gameTwo.newGame(gameTwo.newGuess));
    System.out.println(playerGame2);
    }
    else
    {
    System.out.println("Goodbye");
    }
    }
    }
    }

  14. #14
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    your second constructor is wrong; you don't create the Random object and don't get a value for number.

    It would be better to do this:

    Java Code:
     public GuessingGame()
         {
            this(0);
         }
        
         public GuessingGame(int newMax)
         {
            max = newMax;
            generator = new Random();
            int number = generator.nextInt(101); // This variable is not ever used
         }
    I have no idea what is this: gameOne.getanswer = generator = new Random(101);
    What are you trying to do?

  15. #15
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Help With Random Number Guessing Game

    I was trying to use the Random generator but I mean do you even need to declare that in the tester if it is declared in the constructors?

  16. #16
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Help With Random Number Guessing Game

    You have tons of errors in the tester. generator is only known in your class; just typing generator means nothing.
    I informed you earlier that if you want to access the generator of each GuessingGame object, the smart thing to do is
    make a method public Random getGenerator() that returns the generator. You are also trying to use some local variables in your tester, like gameTwo.newGuess(),
    there is no such method in your class. You are also forgetting many () after a method call, like gameOne.getanswer.
    This : gameOne.newGuess = input.nextInt(); is also wrong. newGuess is nothing outside it's method, it's a local variable.
    Also, the int number in your constructor is useless; there is really no use for it at the moment.
    Also, the first line (again) gameOne.getanswer = generator = new Random(101); is totally wrong. your probably want to set the answer? I really don't know.
    Assigning a value to getnaswer is a compiler error (left hand side of an assignment must be a variable). use the set method if you want the answer field to change and if you want
    to use the generator make the method I was talking about.

Similar Threads

  1. Random 1-10 guessing game with 3 tries
    By sethe23 in forum New To Java
    Replies: 6
    Last Post: 10-01-2012, 04:53 PM
  2. Number Guessing Game issue
    By gabrielpr12 in forum New To Java
    Replies: 14
    Last Post: 01-24-2012, 11:06 PM
  3. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  4. Reverse Number guessing game
    By rarman555 in forum New To Java
    Replies: 6
    Last Post: 04-24-2011, 01:46 AM
  5. Java - number guessing game
    By kev670 in forum Java Applets
    Replies: 3
    Last Post: 10-22-2010, 12:55 AM

Posting Permissions

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