Results 1 to 13 of 13
- 02-29-2012, 07:03 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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!"); } }
- 02-29-2012, 07:04 PM #2
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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.
- 02-29-2012, 07:15 PM #3
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!
- 02-29-2012, 07:29 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: High and Low Game using limited resource
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)
- 02-29-2012, 07:33 PM #5
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!
- 02-29-2012, 07:38 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 31
- Rep Power
- 0
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.
- 02-29-2012, 07:43 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: High and Low Game using limited resource
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 ......
- 02-29-2012, 07:50 PM #8
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!
- 02-29-2012, 08:10 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: High and Low Game using limited resource
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(); }
- 02-29-2012, 08:22 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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
- 02-29-2012, 08:27 PM #11
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!
- 02-29-2012, 08:34 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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? "); }
- 02-29-2012, 08:38 PM #13
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)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
-
problem with high low game
By zlass in forum New To JavaReplies: 2Last Post: 12-03-2011, 08:51 PM -
how do i make the integer limited to 10
By andnlou2678 in forum New To JavaReplies: 1Last Post: 11-15-2011, 03:20 PM -
Limited word Dictionary
By Magic8ballfollower in forum New To JavaReplies: 6Last Post: 09-21-2011, 01:17 AM -
jtable showing limited rows
By pink123 in forum AWT / SwingReplies: 6Last Post: 04-26-2011, 06:29 PM -
sample of guess high and low game
By pouria62 in forum AWT / SwingReplies: 1Last Post: 10-26-2008, 12:57 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks