public
anonymous / Challenge115.java
Created

  • Download Gist
Challenge115.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.