# Guessing Game Help? yes again but its different AGAIN

Discussion in 'Mac Programming' started by neelvaka, Sep 17, 2007.

1. ### neelvaka macrumors newbie

Joined:
Sep 15, 2007
#1
JAVA

Code:
```

import java.io.*;

public class game {

public static void main(String args[])

throws IOException, NumberFormatException {

int upper, lo, hi, guess, count;

String input;

System.out.println("____________________");

System.out.println("\\ \\");

System.out.println(" \\ Guessing Game \\");

System.out.println(" \\___________________\\");

do {

System.out.print("\nWould you like to view the instructions, (y)es or (n)o? ");

if (input.equals("y") || input.equals("Y")) {

System.out.println("\n\tYou will set an upper bound and think of a number between");

System.out.println("\t1 and that number. I will try to guess your number in as");

System.out.println("\tfew guesses as possible.");

}

} while ( ! (input.equals("y") || input.equals("Y") || input.equals("n") || input.equals("N")) );

System.out.print("\nEnter the upper bound: ");

System.out.println();

count = 0;

lo = 1;

hi = upper;

guess = upper / 2;

do {

System.out.print("\tMy guess is " + guess + ". Is that (l)ow, (h)igh, or (c)orrect? ");

if (input.equals("l") || input.equals("L")) {

count++;

lo = guess;

guess = (hi + lo) / 2;

} else {

if (input.equals("h") || input.equals("H")) {

count++;

hi = guess;

guess = (hi + lo) / 2;

} else {

if (input.equals("c") || input.equals("C")) {

count++;

System.out.println("\n\tI guessed your number in only " + count + " tries!");

System.out.println("\nThank you for playing Guessing Game.");

break;

}

}

}

} while ( ! (input.equals("c") || input.equals("C")) );

}

}
```

ok this game guesses YOUR secret number
but i need to add something that helps it realize that the user of the program is cheating. how would i do that?

like if my number is 43 and it guesses 43 but i chagne it then and the program should be able to detect that the user has changed his/her number (it should no its not wrong). HELP?

eclipe platform used

2. ### CANEHDN macrumors 6502a

Joined:
Dec 12, 2005
Location:
Eagle Mountain, UT
#2
You just need to set a variable that is set once the game guesses the number. Then the only way to verify that person is "cheating" is compare the variable the the newly guessed number. If they're the same then the person is "cheating". I don't really understand how the person would be cheating. I'm confused.

3. ### neelvaka thread starter macrumors newbie

Joined:
Sep 15, 2007
#3
thats exactly what im tryin to do but how would i code that and where

... really confused rite now srry

4. ### zimv20 macrumors 601

Joined:
Jul 18, 2002
Location:
toronto
#4
good god, don't you do any of your own work? how do you think it's gonna work in the real world?

5. ### neelvaka thread starter macrumors newbie

Joined:
Sep 15, 2007
#5
actually i would like it more if while helping me you could also explain it to me. I do understand it its just my head isnt working rite now

6. ### kainjow Moderator emeritus

Joined:
Jun 15, 2000
#6
Unless you can read the person's mind, it'd be impossible. The user thinks of a number in their head, and enters low, high, or correct to the computer. So they could enter correct every single time on the first try. You just have to have faith

7. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#7
You'll know that you have a cheater when you exhaust a binary search and don't find it.

I gave you a ton of code in your last thread. You're on your own for this one.

Also, use the String.compareToIgnoreCase(String s) function instead of String.equals(String s) for both the upper and lower versions of each input character. If you want to be super-slick, extract the first char from the string and evaluate it. That'll let you accept anything starting with a 'y' or an 'n'.

Good luck.

8. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#8
If I understand what you mean by cheating, try something along the lines of,

int minGuess;
int maxGuess;

Update these as the guesses are made. If the user keeps saying it's higher than the maxGuess, or lower than the minGuess, then they're cheating. You just keep constricting the range of possibles. When all of the range has been extinguished either they find the right number or the user was cheating ... or you programmed something wrong

9. ### toddburch macrumors 6502a

Joined:
Dec 4, 2006
Location:
Katy, Texas
#9
Simply write a log out of past prompts and responses when you detect the user is cheating, thus proving to the user they changed their answer.

Todd

10. ### ChrisBrightwell macrumors 68020

Joined:
Apr 5, 2004
Location:
Huntsville, AL
#10
You guys are over-complicating this. You don't need to keep logs and you don't need to track/update a handful of additional variables.

All you need to do is pay attention to the binary search algorithm.

If you finish the binary search on a finite set of integers (say, 1 to 10 or 1 to 100) and don't find the number, it's obvious that the user changed their answer at some point and you can react accordingly.

Suppose, for example, that a user selects a number between 1 and 100 (inclusive).

Your first guess will be 50. The second guess will be either 25 or 75. From there you have a bit of room to wiggle, but it's gonna be a multiple of 12.5 (excl. 25, 50, and 75), rounded up or down to the nearest integer. One a set of 100 integers, you should need no more than 7 or 8 guesses to hit the number.

If, after the first guess, I change my number from 25 to 26, it doesn't matter that I changed my number. If I change from 25 to 75, though, you'll eventually get to 49, which will be to high. I already told you that 50 is too low but, since you're using a binary search, the search will end with the target value not found.

The result? I cheated. It's pretty simple, logically.

11. ### toddburch macrumors 6502a

Joined:
Dec 4, 2006
Location:
Katy, Texas
#11
You are absoluely correct. I read into the initial question that he needed to prove to the player that the guessed number had changed. However, reading it again, that obviously is something I conjured up in my head.

Todd

12. ### toddburch macrumors 6502a

Joined:
Dec 4, 2006
Location:
Katy, Texas
#12
I got to thinking about this some more. A week or two ago, I wrote an applet that centers a string in a window. It uses a binary lookup to find the optimum font size to fit in the window. Not much code at all.
I use two variables to keep track of things. One is called "temp" and the other is "previous_font_size".

Todd

13. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#13
No you don't need to create new variables, but you still have to make use of them (though your comment may not have been directed at my post, I gave different names to variables he already had). For the variables he gives, you can do a check at the end of your loop that checks if (lo >= hi), and if so you've reached the end of the search, and if "correct" was not reached, then the user is cheating.