Results 1 to 7 of 7
  1. #1
    rarman555 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Reverse Number guessing game

    I call this a reverse number guessing game because usually the computer would generate a number for you to guess, but I want the computer to guess my number by guessing randomly in decreasing ranges depending on if I say too high or too low.

    The biggest problem I'm having is making the range of the random number be correct. It likes to guess numbers out of the range.

    The computer starts off by giving me a number from 1 - 100. Then i type in "th", "tl", or "co" (too high, too low, correct).

    If I say too low the lowest number in the range should change to the previous guess and same for if i say too high (but it gets set to the previous highest number instead of previous lowest).

    This should decrease the range of the random generator until it gets to the point where the range is so small that the computer can only guess my number or it might just get lucky before that (just like a human).

    Here's the code:
    Java Code:
    import java.util.*;	
    
    public class computer_guess 
    {
    	public static void main(String[] args) 
    	{
    		Scanner console;
    		console = new Scanner(System.in);
    		Random aRandom = new Random(); 
    		
    		int  
    			prev_low = 1, 
    			prev_high = 100, 
    			guess = aRandom.nextInt(prev_high) + prev_low,
    			guesses = 0;
    		
    		
    	//	long time = 0; 
    		
    		boolean win = false;
    		
    		String feedback;
    		
    		
    	
    		while(!win)
    		{
    			System.out.println("My guess is: " + guess);
    			feedback = console.nextLine();
    			//
    			
    			if(feedback.equals("tl") || feedback.equals("th") || feedback.equals("co"))
    			{
    				if(feedback.equals("tl"))//too low
    				{
    					prev_low = guess;
    					System.out.println(feedback);
    				}
    				else if(feedback.equals("th"))//too high
    				{
    
    					prev_high = guess;
    					System.out.println(feedback);
    				}
    				else if(feedback.equals("co"))//correct
    				{
    					System.out.print("Yay, I WIN!");
    					win = true;
    					System.out.println(feedback);
    				}
    				
    				guess = aRandom.nextInt(prev_high) + prev_low;
    				//++guesses;
    				//feedback = " ";
    				//System.out.print(feedback);
    			}
    		}
    	}
    }
    Last edited by rarman555; 04-23-2011 at 05:15 AM.

  2. #2
    nikkka is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    Now, look. I edited your code, so that it works. But you have to find out what was the matter yourself. And, use debugger.
    Java Code:
    import java.util.*;	
    
    public class Bounce 
    {
    	public static void main(String[] args) 
    	{
    		Scanner console;
    		console = new Scanner(System.in);
    		Random aRandom = new Random(); 
    		
    		int  
    			prev_low = 1, 
    			prev_high = 100, 
    			guess = aRandom.nextInt(prev_high) + prev_low,
    			guesses = 0;
    		
    		
    	//	long time = 0; 
    		
    		boolean win = false;
    		
    		String feedback;
    		
    		while(!win)
    		{
    			guess = aRandom.nextInt(prev_high-prev_low) + prev_low;
    			System.out.println("My guess is: " + guess);
    			feedback = console.nextLine();
    			if(feedback.equals("tl") || feedback.equals("th") || feedback.equals("co"))
    			{
    				if(feedback.equals("tl"))
    					prev_low = guess;
    				if(feedback.equals("th"))
    					prev_high = guess;
    				if(feedback.equals("co")) {
    					System.out.print("Yay, I WIN!");
    					win = true;
    				}
    				//++guesses;
    				//feedback = " ";
    				//System.out.print(feedback);
    			}
    		}
    	}
    }

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

    Default

    Quote Originally Posted by rarman555 View Post
    Java Code:
    			
    guess = aRandom.nextInt(prev_high) + prev_low;
    This line most certainly isn't correct; make it:

    Java Code:
    guess = aRandom.nextInt(prev_high-prev_low) + prev_low;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    rarman555 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    thanks for the help guys but i actually figured it out not too long after i posted this :p

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    thanks for the help guys but i actually figured it out not too long after i posted this
    indeed. Cross posted at Reverse Number guessing game

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

    Default

    Quote Originally Posted by doWhile View Post
    indeed. Cross posted at Reverse Number guessing game
    It's the lying part "I actually figured it out", I dislike most.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    rarman555 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    Sorry for the trouble, didnt know these forums were connected (are they?). And the lying part I wasnt meaning to lie i just wanted to give a quick answer.

Similar Threads

  1. Help with Reverse Guessing Game Code
    By koryvandell in forum New To Java
    Replies: 2
    Last Post: 04-04-2011, 01:53 AM
  2. guessing game
    By MrM in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 07:51 PM
  3. Need help in Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 10:43 PM
  4. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 08:00 PM
  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
  •