Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 14, 2014 05:09
Show Gist options
  • Select an option

  • Save rayjcwu/9542419 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9542419 to your computer and use it in GitHub Desktop.
Akinator like
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Game {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] argv) throws Exception {
Game game = new Game();
game.entry();
}
public void entry() throws Exception {
Node root = new Branch("Is it an animal?", null);
while (true) {
System.out.println("new round");
askQuestion(root);
}
}
private void askQuestion(Node node) throws Exception {
while (node != null) {
node.ask();
String replyString = br.readLine().toLowerCase();
Node nextNode = node.reply(replyString);
if (nextNode == node) { // entity, guessing success
break;
} else if (nextNode == null) {
// might be empty branch
// or wrong entity
if (node instanceof Branch) {
System.out.println("I lose, please provide new entity");
String input = br.readLine();
if (replyString.equals("yes") || replyString.equals("y")) {
((Branch)node).yes = new Entity(input, node);
} else {
((Branch)node).no = new Entity(input, node);
}
} else if (node instanceof Entity) {
System.out.println("I lose, please provide [question],[entity],[yes/no], split by ','");
String[] tokens = br.readLine().split(",");
Branch newBranch = new Branch(tokens[0], node);
Entity newEntity = new Entity(tokens[1], node);
String yesNo = tokens[2];
Branch parentNode = (Branch)(node.parent);
if (parentNode.yes == node) {
// entity is on yes branch
parentNode.yes = newBranch;
} else {
// entity is on no branch
parentNode.no = newBranch;
}
if (isYes(yesNo)) {
newBranch.yes = newEntity;
newBranch.no = node;
} else {
newBranch.yes = node;
newBranch.no = newEntity;
}
}
break;
} else /* branching */ {
node = nextNode;
}
}
}
private boolean isYes(String str) {
return str.equals("yes") || str.equals("y");
}
}
public abstract class Node {
Node parent;
public Node(Node parent) {
this.parent = parent;
}
public abstract void ask() throws Exception;
public abstract Node reply(String str) throws Exception;
}
public class Branch extends Node {
String question;
Node yes;
Node no;
public Branch(String question, Node parent) {
super(parent);
this.question = question;
}
@Override
public void ask() throws Exception {
System.out.println(question);
}
@Override
public Node reply(String str){
// go to branch
if (str.equals("yes") || str.equals("y")) {
return yes;
} else {
return no;
}
}
}
public class Entity extends Node {
String name;
public Entity(String name, Node parent) {
super(parent);
this.name = name;
}
@Override
public void ask() throws Exception {
System.out.println(String.format("It's %s", name));
}
@Override
public Node reply(String str) {
// failed on guessing
if (!str.equals("yes")) {
return null;
} else {
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment