Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: High and Low Game using limited resource

  1. #1
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default High and Low Game using limited resource

    Hi Masters!,

    My assignment is to think of a number (in head) between 1-100 where program will eliminate options by h or l. I've checked online and most of the code is by using

    import java.math.*;
    import java.util.Random;
    import java.util.Scanner;

    I don't have the luxury but just the Scanner with IF + while loop. This is what I have so far don't know where to go now with it? Any hints?

    Java Code:
    public static void main(String [] args)
    	{	
    		Scanner keyboard = new Scanner (System.in);
    		
    		int highestValue = 100;
    		int lowestValue = 1;
    		int guesses = 0;
    		int number;
    		
    		char C,H,L;
    		
    		boolean high, low, correct;
    		
    		String pcGuess;
    		
    		high = true;
    		low = true;
    		correct = true;
    		
    		System.out.print("Is the number 50? ");
    		pcGuess = keyboard.nextLine;
    		
    		if(pcGuess == correct);
    			System.out.print("It took me 1 guess!");
    		
    	}
    }

  2. #2
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    output should be something like this:

    ----jGRASP exec: java Assignment6b
    Is the number 50? H
    Is the number 75? L
    Is the number 62? L
    Is the number 56? L
    Is the number 53? L
    Is the number 51? C
    It took me 6 guesses!
    ----jGRASP: operation complete.

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: High and Low Game using limited resource

    Just think about it. If the number is between 1 and 100, and the computer guesses 50, and you tell the computer that the number is higher, what is the number between now?

    Think about how you'd play this game with a friend. Write out the steps you'd follow. When you're done with that, you'll have an algorithm that should be pretty easy to translate into code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    Quote Originally Posted by KevinWorkman View Post
    Just think about it. If the number is between 1 and 100, and the computer guesses 50, and you tell the computer that the number is higher, what is the number between now?

    Think about how you'd play this game with a friend. Write out the steps you'd follow. When you're done with that, you'll have an algorithm that should be pretty easy to translate into code.
    I just don't know where I'm going with this =(, can't think of right algorithm....

    Java Code:
    boolean number, h, l, c;
    		
    		String pcGuess;
    		
    		h = true;
    		l = true;
    		c = true;
    		
    			System.out.print("Is the number 50? ");
    		pcGuess = keyboard.nextLine();
    		
    		if(pcGuess = h || pcGuess !=l || pcGuess !=c)

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: High and Low Game using limited resource

    Don't worry about the code or even a computer for a minute. Pretend you're playing with a friend, and you have to guess the number he's thinking. How do you do it? Write out your thought process on a piece of paper. Understanding how to solve problems, especially problems where the solution feels like "I don't know, I just do it" is a crucial skill to programming, but it's one that's almost impossible to teach. That's one of the real points of the assignment.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    Write out the pseudo-code for your program first so you can get organized and design your plans or the program.

    Your going to need a loop that continuously asks "Is the number x" , where x is the variable of one of the guesses.

    You can logically eliminate the other values and slowly refine the guesses by using the Higher/lower indication until you have the exact number.

  7. #7
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    Quote Originally Posted by KevinWorkman View Post
    Don't worry about the code or even a computer for a minute. Pretend you're playing with a friend, and you have to guess the number he's thinking. How do you do it? Write out your thought process on a piece of paper. Understanding how to solve problems, especially problems where the solution feels like "I don't know, I just do it" is a crucial skill to programming, but it's one that's almost impossible to teach. That's one of the real points of the assignment.
    Ok,

    I ask my friend to pick a number between 1-7 (in head)
    1 2 3 4 5 6 7
    I will ask him is that number 4?
    friend answers L // that tells me that all the numbers from 4-7 are not his number

    i will ask him again is that number 2?
    friend answers L// that tells me that 2-3 is not his number so I know he picked number "1"

    So I guess your number is 1 // he says yes

    I know the reasoning behind it but I cant/don't know how to put in into algorithm and loop so it works like on a piece of paper ......its so easy and so hard at the same time. I'm going to kill me self in just a minute ......

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: High and Low Game using limited resource

    Well, you know you'll need a loop that iterates as long as a correct guess was not made. How do you do that?

    Then you have some handy variables ready to store the lowest and highest values it can be, based on the answers you've received so far.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    Quote Originally Posted by KevinWorkman View Post
    Well, you know you'll need a loop that iterates as long as a correct guess was not made. How do you do that?

    Then you have some handy variables ready to store the lowest and highest values it can be, based on the answers you've received so far.
    am i going the right way with the loop?

    Java Code:
    boolean number, h, l, c;
    		
    		String pcGuess;
    		
    		h = true;
    		l = true;
    		c = true;
    		
    		System.out.print("Is the number 50? ");
    			pcGuess = keyboard.nextLine();
    		while(pcGuess == h)
    		{
    			if(pcGuess != h && pcGuess == l && pcGuess == c)
    			{	
    				System.out.print("is the number 25? ");
    					pcGuess = keyboard.nextLine();
    			}

  10. #10
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    small update - still working
    while(h == true)
    {
    System.out.print("is the number 75? ");
    pcGuess = keyboard.nextLine();
    if

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: High and Low Game using limited resource

    I'm not sure what h is supposed to represent. You want to loop while the guess is not equal to the answer. For that, you're going to need ints that store the last guess and the answer. I'm not sure what all those booleans are for.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    xcaldk74 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: High and Low Game using limited resource

    Kevin,

    first of all thank you for trying to help me with this exercise I really appreciate your "time" BUT think i need to go over loops and if statements again because i'ts not working.... here is the code just fyi:


    Java Code:
    public static void main(String [] args)
    	{	
    		Scanner keyboard = new Scanner (System.in);
    		
    		int highestValue = 100;
    		int lowestValue = 1;
    		int guesses = 1;
    		int count = 0;
    		int userNumber;
    		//int number;
    		
    		//char C,H,L;
    		
    		boolean number, h, l, c;
    		
    		String pcGuess;
    		
    		h = true;
    		l = true;
    		c = true;
    		
    		System.out.print("Is the number 50? ");
    			pcGuess = keyboard.nextLine();
    		while(h == true)
    		{
    			userValue = highestValue-51;
    			
    			System.out.print("is the number 75? ");
    					pcGuess = keyboard.nextLine();
    			if(h == true || h != c)
    			{
    				System.out.print("is the number 87? ");
    			}

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: High and Low Game using limited resource

    I think you're over-complicating things with all those extra booleans. What do they represent? By that, I mean, how do they translate to your description of how you'd do this with a friend? I understand the ints representing your changing idea of what the number could be, so you might want to focus on them instead.

    And if you want to go over loops again, the tutorials are your first stop: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    xcaldk74 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. problem with high low game
    By zlass in forum New To Java
    Replies: 2
    Last Post: 12-03-2011, 08:51 PM
  2. how do i make the integer limited to 10
    By andnlou2678 in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 03:20 PM
  3. Limited word Dictionary
    By Magic8ballfollower in forum New To Java
    Replies: 6
    Last Post: 09-21-2011, 01:17 AM
  4. jtable showing limited rows
    By pink123 in forum AWT / Swing
    Replies: 6
    Last Post: 04-26-2011, 06:29 PM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 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
  •