-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
70 lines (61 loc) · 2.04 KB
/
Copy pathGuessingGame.java
File metadata and controls
70 lines (61 loc) · 2.04 KB
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
import TreePackage.DecisionTreeInterface;
import TreePackage.DecisionTree;
import java.util.Scanner;
public class GuessingGame
{
private DecisionTreeInterface<String> tree;
private Scanner in;
public GuessingGame(String question, String noAnswer, String yesAnswer)
{
DecisionTree<String> no = new DecisionTree<>(noAnswer);
DecisionTree<String> yes = new DecisionTree<>(yesAnswer);
tree = new DecisionTree<>(question, no, yes);
in = new Scanner(System.in);
}
public void play()
{
if(tree.isAnswer())
{
System.out.println("My guess is " + tree.getCurrentData() + ", Am I right?");
String res = in.nextLine();
if(res.toUpperCase().equals("YES"))
System.out.println("I win!");
if(res.toUpperCase().equals("NO"))
learn();
System.out.println("Play again?");
res = in.nextLine();
if(res.toUpperCase().equals("YES"))
{
tree.resetCurrentNode();
play();
}
else
return;
}
else
{
System.out.println(tree.getCurrentData());
String ans = in.nextLine();
if(ans.toUpperCase().equals("YES"))
{
tree.advanceToYes();
play();
}
else if(ans.toUpperCase().equals("NO"))
{
tree.advanceToNo();
play();
}
}
}
public void learn()
{
String no = tree.getCurrentData();
System.out.println("I give up; what were you thinking of?");
String yes = in.nextLine();
System.out.println("Give me a question whose answer is yes for " + yes + " and no for " + no);
String question = in.nextLine();
tree.setCurrentData(question);
tree.setResponses(no, yes);
}
}