/**
* In this game, I will first choose a secret random number, and one of us (either you or me) must guess it iteratively... <br/>
* All I will reveal for a guess is whether the guess is higher or lower than the secret random number in my mind.<br/>
* You may use everything that is provided to you in this class, but<br/>
* DO NOT CHANGE ANYTHING except for the bodies of the methods marked with "ToDo"!
*
*/
public class ChildrensGame {
/**
* The smallest possible number I may choose.
*/
protected final int MIN;
/**
* The greatest possible number I may think of.
*/
protected final int MAX;
/**
* Just for statistics...<br/>
*/
protected long numberOfGuesses = 0;
/**
* The secret number between {@link #MIN} and {@link #MAX} (both inclusive) to be guessed.
*/
protected final int toBeGuessed;
/**
* Let the games begin (here)!<br/>
* There is no need for you to change this method in any way (but you should test your code with different min/max-values...)!<br/>
* The secret number to be guessed is chosen between {@link #MIN} and {@link #MAX} (both inclusive).<br/>
* @see <a href="http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Math.html#random()">Math.random()</a>
* @param args If you don't provide one (whatever), I'll play by myself - otherwise you can puzzle it out...
*/
public static void main(String[] args) {
int min = 1;
int max = 100;
int toBeGuessed = min + ((int)(Math.random() * (max - min + 1)));
ChildrensGame childrensGame = new ChildrensGame(min, max, toBeGuessed);
if (args.length > 0) {
childrensGame.playWithYou();
} else {
childrensGame.playByMyself();
}
}
/**
* Initialises a new game.<br/>
*/
protected ChildrensGame(int min, int max, int toBeGuessed) {
this.MIN = min;
this.MAX = max;
this.toBeGuessed = toBeGuessed;
}
/**
* Checks whether {@code myGuess} is less than, equal or greater than the real secret number {@link #toBeGuessed} and returns the "encoded result" correspondingly.
* @param myGuess This is the guess to be evaluated.
* @return
* <table>
* <tr><td>-1</td><td>: if {@code myGuess} is less than the secret value {@link #toBeGuessed}.</td></tr>
* <tr><td>0</td><td>: if {@code myGuess} is exactly the the secret value <i>(yeah!)</i></td></tr>
* <tr><td>1</td><td>: if {@code myGuess} is greater than the secret value.</td></tr>
* </table>
*/
protected int checkGuess(int myGuess) {
int result;
//oben das ist vorgegeben und den block hier hab ich selbst verfasst
if(myGuess < toBeGuessed)
result = -1;
else if(myGuess > toBeGuessed)
result = 1;
else
result = 0;
// das ist wieder vorgegeben
System.out.println("The " + ++numberOfGuesses + ". guess is \"" + myGuess + "\" and it is " + (result < 0 ? "too small." : result > 0 ? "too high." : "PERFECT!"));
return result;
}
/**
* I'll choose the secret number {@link #toBeGuessed} and a human player must try to guess it.
*/
protected void playWithYou() {{
//das ist wieder selbst verfasst
int toBeGuessed;
int myGuess;
int numberOfGuesses;
boolean checkGuess = false;}
public playWithYou()
numberOfGuesses = 1;
generateRandomNum();
System.out.println("Bitte geben Sie ihre Zahl ein: ");
while(checkGuess=false){
readInput();
compare(); // Vergleicht in jedem Schleifendurchgang die neue Eingabe des Nutzers mit der Zufallszahl.
}}
private void compare() {
}
private void readInput() {
}
public void generateRandomNum(){
System.out.println("***Zufallszahl wurde erzeugt***");
Random ran = new Random();
toBeGuessed = ran.nextInt(101);
public void readInput();{ /* Hier wird die Eingabe des Nutzers eingelesen.
try{
myGuess = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
}catch(Exception e){
e.printStackTrace();
}
}
public void compare();
if (myGuess < toBeGuessed){
System.out.println("Ihre Zahl ist zu klein!");
numberOfGuesses++;
}
else if (myGuess > toBeGuessed){
System.out.println("Ihre Zahl ist zu groß!");
numberOfGuesses++;
}
else{
System.out.println("Sie haben richtig geraten! :)\Sie benötigten dafür " + numberOfGuesses + " Versuche.");
numberOfGuesses++;
checkGuess = true;
}}}
// das ist wieder vorgegeben
/**
* I'll just pretend that I don't know the value {@link #toBeGuessed} and puzzle myself over it just like a human player.<br/>
* I will do my best to find the secret number within as few steps as possible!
*/
protected void playByMyself(){
}
// TODO
/**
* Helper infrastructure, used to read human input.
*/
protected int inputInt() {
try {
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
while(true) {
System.out.print("Tell me your guess: ");
String inputString = reader.readLine();
if (inputString == null) throw new java.io.EOFException();
try {
return Integer.parseInt(inputString);
} catch (NumberFormatException e) {
System.out.println("- Your input (\"" + inputString + "\") is not a valid number! Try again...");
}
}
} catch (Throwable throwable) {
System.err.println();
System.err.println("- Sorry, it just doesn't work here with you... I'll shut down now!");
System.exit(1);
return 0;
}
}