Men der er Java highlightning.
- package dyreleg;
-
- public class Animal implements Question {
-
- private String Animal;
-
- public Animal(String animalName) {
- Animal = animalName;
- }
-
- /**
- * Asks the user if this animalName is the right one. If its not, a new animalName
- * and a new question will be created based on the users feedback
- *
- * @return returns a self reference if the user answers yes, otherwise a
- * new object of CondQuestion will be returned.
- */
- @Override
- public Question ask() {
- if (Main.user.getAnswerYesNo("Is it "+Animal+"?")) {
- Main.user.tell("I win. Pretty smart, aren't I?");
- return this;
- } else {
- // the user won, now a new Question and a new Animal will be created
- Main.user.tell("You win. Help me learn from my mistake before you go...");
- String newAnimal = Main.user.getAnswer("What animal were you thinking of?");
- String newQuestion = Main.user.getAnswer("Please give me a question to distinguish "+newAnimal+" from "+Animal+".");
-
- boolean newAnswer = Main.user.getAnswerYesNo("For "+newAnimal+", what is the answer to your question?");
- if (newAnswer) {
- return new CondQuestion(newQuestion, new Animal(newAnimal), this);
- } else {
- return new CondQuestion(newQuestion, this, new Animal(newAnimal));
- }
- }
- }
- }