Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2013 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4470714 to your computer and use it in GitHub Desktop.
Save anonymous/4470714 to your computer and use it in GitHub Desktop.
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public abstract class Challenge115 {
/**
* Binary Search Robot
*
* @author SplashAttacks
*/
private static class RobotInputChallenge115 extends Challenge115 {
private Integer lastGuess = null;
private int max;
private int min = 1;
public RobotInputChallenge115(int range) {
super(range);
max = range;
}
@Override
protected int getGuess() {
if (hasGuessed()) {
if (lastGuess < getNumber()) {
min = lastGuess;
} else if (lastGuess > getNumber()) {
max = lastGuess;
} else {
min = lastGuess;
max = lastGuess;
}
}
int guess = ((max - min) / 2) + min;
// Fix for edge case, we should never guess the same number twice
// Happens because we are flooring fractional numbers.
if (hasGuessed() && guess == this.lastGuess) {
guess++;
}
lastGuess = guess;
return guess;
}
private boolean hasGuessed() {
return lastGuess != null;
}
@Override
protected boolean playAgain() {
return false;
}
}
private static class UserInputChallenge115 extends Challenge115 {
private final Scanner scanner = new Scanner(System.in);
public UserInputChallenge115(int range) {
super(range);
}
@Override
protected int getGuess() {
Integer guess = null;
do {
try {
guess = scanner.nextInt();
if (guess < 1 || guess > getRange()) {
System.out
.println("Please entery an integer between 1 and "
+ getRange() + ".");
}
} catch (InputMismatchException ime) {
System.out
.println("Please entery an integer between 1 and "
+ getRange() + ".");
// Throw away the non integer from the scanner.
scanner.next();
}
} while (guess == null || guess < 1 || guess > getRange());
return guess;
}
@Override
protected boolean playAgain() {
System.out.println("Play Again (y/n)? ");
Boolean result = null;
do {
String selection = scanner.next().toLowerCase();
if (selection.equals("y") || selection.equals("yes")) {
result = true;
} else if (selection.equals("n") || selection.equals("no")) {
result = false;
} else {
System.out.println("Play Again (y/n)? ");
}
} while (result == null);
if (!result) {
scanner.close();
}
return result;
}
}
public static void main(String[] args) {
// Challenge115 challenge = new RobotInputChallenge115(100);
Challenge115 challenge = new UserInputChallenge115(100);
challenge.play();
}
private int number;
private final Random random;
private int range;
public Challenge115(int range) {
this.random = new Random();
this.range = range;
}
public int getNumber() {
return number;
}
public int getRange() {
return range;
}
protected abstract int getGuess();
private void play() {
boolean playAgain = true;
while (playAgain) {
number = thinkOfANumber();
System.out
.println("Welcome to guess-that-numbers game! I have already picked a number in [1, "
+ range + "]. Please make a guess.");
int tries = 0;
int guess;
do {
guess = getGuess();
tries++;
if (guess < number) {
System.out.println(guess
+ " is wrong. That number is below my number.");
} else if (guess > number) {
System.out.println(guess
+ " is wrong. That number is above my number.");
}
} while (guess != number);
System.out.println(guess
+ " is correct! That is my number, you win! It took you "
+ tries + " tries.");
playAgain = playAgain();
}
System.out.println("Thanks for playing!");
}
protected abstract boolean playAgain();
private int thinkOfANumber() {
return random.nextInt(range) + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment