-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
218 lines (203 loc) · 7.03 KB
/
GuessingGame.java
File metadata and controls
218 lines (203 loc) · 7.03 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
/**
* lab group : Mark McArdle, Cade Haas, Jeremiah Niedzielski, Owen Colburn
* @author Mark McArdle
* CS1131 L02
*/
public class GuessingGame implements Game {
boolean playable = true;
LinkedBinaryTreeNode<String> root;
String filename;
static Scanner s = new Scanner(System.in);
public GuessingGame(String filename) {
this.filename = filename;
root = (LinkedBinaryTreeNode<String>) loadTree(filename);
}
private static Boolean YN(Scanner s) {
String in = s.nextLine();
if (in.toLowerCase().equals("y")) {
return true;
}
if (in.toLowerCase().equals("n")) {
return false;
}
return null;
}
private static void o(String o) {
System.out.println(o); // I am tired of writing System.out.println();
}
/**
* Loads a BinaryTree file that has been saved as a preorder traversal.
* Interior nodes are prefixed by "Q:".
* Leaf nodes are prefixed by "G:"
*
* @param filename
* @return the root node
*/
@Override
public BinaryTreeNode<String> loadTree(String filename) {
FileReader file;
try {
file = new FileReader(filename);
} catch (FileNotFoundException e) {
System.out.println("\"" + filename + "\" not found");
playable = false;
return null;
}
Scanner s = new Scanner(file);
ArrayList<String> lines = new ArrayList<String>();
while (s.hasNextLine()) {
lines.add(s.nextLine());
}
s.close();
root = (LinkedBinaryTreeNode<String>) loadHelper(lines.iterator());
return root;
}
private BinaryTreeNode<String> loadHelper(Iterator<String> lines) {
if (!lines.hasNext()) {
return null;
}
if (!playable) {
return null; // don't want to add anything to a list that is incorrect to avoid errors
}
LinkedBinaryTreeNode<String> node = null;
String line = lines.next();
if (line.length() > 2) {
if (line.substring(0, 2).toLowerCase().equals("q:")) {
node = new Question<String>(line.substring(2));
node.setLeft(loadHelper(lines));
node.setRight(loadHelper(lines));
} else if (line.substring(0, 2).toLowerCase().equals("g:")) {
node = new Guess<String>(line.substring(2));
} else {
System.out.println("Issue parsing file for the line : " + line + " : could not find \"q:\" or \"g:\"");
playable = false;
return null;
}
} else {
System.out.println("Issue parsing file for the line : " + line + " : line not long enough");
playable = false;
return null;
}
return node;
}
@Override
public void saveTree(String filename) {
try {
FileWriter tmp=new FileWriter(filename);
String toFile="";
ArrayList<LinkedBinaryTreeNode<String>> nodes=new ArrayList<LinkedBinaryTreeNode<String>>();
root.traversePreorder(cheese -> {nodes.add((LinkedBinaryTreeNode<String>)cheese);});
int x=0;
while(x<nodes.size()-1){
toFile=toFile+nodes.get(x)+"\n";
x++;
}
toFile=toFile+nodes.get(x);
tmp.write(toFile);
tmp.close();
} catch (IOException e) {
o("Something went wrong with saving the tree.");
}
}
@Override
public BinaryTreeNode<String> getRoot() {
return root;
}
@Override
public void play() {
if(!playable){
return;
}
while (true) {
o("Shall we play a game? (y/n)");
Boolean b=YN(s);
if (b==null) {
continue;
}
if(!b){
break;
}
LinkedBinaryTreeNode<String> current = root;
while (!current.isLeaf()) {
o(current.data);
b = YN(s);
if (b != null) {
if (b) {
current = (LinkedBinaryTreeNode<String>) current.getRight();
} else {
current = (LinkedBinaryTreeNode<String>) current.getLeft();
}
if (current == null) {
o("Something went wrong! There may be an issue with the tree structure.");
return;
}
} else {
o("Please only enter y or n.");
}
}
o("Are you thinking of : " + current.data + " ? (y/n)");
b = YN(s);
while (b == null) {
o("Please only enter y or n.");
b = YN(s);
}
if (b) {
o("I win! \n\n\n");
} else {
o("You win!\nWhat are you thinking of? (anything or \"exit\")");
String in = s.nextLine();
if (in.toLowerCase().equals("exit")) {
o("\n\n\n");
continue;
}
o("What question separates : " + current.data + " from : " + in + " ?");
String qin = s.nextLine();
Question<String> tmp = new Question<String>(qin);
o("Is " + in + " correct when the answer to \"" + qin + "\" is yes? (y/n)");
b = YN(s);
while (b == null) {
o("Please only enter y or n.");
b = YN(s);
}
if (current.getParent() != null) {
if (current.getParent().getLeft() == current) {
current.getParent().setLeft(tmp);
} else {
current.getParent().setRight(tmp);
}
}
if (b) {
tmp.setLeft(current);
tmp.setRight(new Guess<String>(in));
} else {
tmp.setLeft(new Guess<String>(in));
tmp.setRight(current);
}
root=(LinkedBinaryTreeNode<String>) current.getRoot();
// root.traversePreorder(data -> {
// System.out.println(data);
// });
saveTree(filename);
o("\n\n\n");
}
}
s.close();
}
public static void main(String[] args) {
GuessingGame game;
if (args.length > 0) {
game = new GuessingGame(args[0]);
} else {
System.out.println("Please enter the filename of the tree that you want to use.");
game = new GuessingGame(s.nextLine());
}
game.play();
}
}