Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

neelvaka

macrumors newbie
Original poster
Sep 15, 2007
16
0
JAVA

Code:
import java.io.*;

 

public class game {

public static void main(String args[]) 

throws IOException, NumberFormatException {

BufferedReader IN = new BufferedReader(new InputStreamReader(System.in));

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? ");

 

input = IN.readLine();

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: ");

upper = Integer.parseInt(IN.readLine());

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? ");

input = IN.readLine();

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?:confused:


eclipe platform used
 

CANEHDN

macrumors 6502a
Dec 12, 2005
855
0
Eagle Mountain, UT
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.
 

neelvaka

macrumors newbie
Original poster
Sep 15, 2007
16
0
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.
thats exactly what im tryin to do but how would i code that and where

... really confused rite now srry
 

neelvaka

macrumors newbie
Original poster
Sep 15, 2007
16
0
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 :(
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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 ;)
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
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.
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
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 :D
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
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.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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.

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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

angelwatt

Moderator emeritus
Aug 16, 2005
7,852
9
USA
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.