-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRGG.java
More file actions
249 lines (228 loc) · 6.18 KB
/
RGG.java
File metadata and controls
249 lines (228 loc) · 6.18 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package restrictedGame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import UnrestrictedGame.Menu;
import datastructures.BinaryTreeNode;
import datastructures.DefaultBinaryTree;
/**
* This class maintains the model of the restricted guessing game
*
* @author Ching2 Huang
*
*/
public class RGG extends JFrame implements ActionListener {
// two buttons: yes/no and restart
protected JButton yes, no, restart, menu;
protected JLabel question;// label for questions
protected JTextArea answers; // text area for answers
protected DefaultBinaryTree<String> tree; // tree from file reader
protected FileReader file; // file reader
protected BinaryTreeNode<String> current; // current node
/**
* Constructor set up the frame for the game and start the game with root
* node
*/
public RGG() {
file = new FileReader();
tree = file.getTree(); // get the tree
current = tree.getRoot(); // start from the root
setTitle("Restrcited Guessing Game"); // name the frame
setFrame(); // set up the frame
}
/**
* set up the frame
*/
private void setFrame() {
setSize(400, 400); // set the size of the frame
setResizable(false); // unable to resize the frame
setLocationRelativeTo(null);// default to the middle of the screen
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// shut down the game when the window is closed
add(panel());
setVisible(true);// set everything visible
}
/**
* big panel to put everyting in
*
* @return the panel
*/
private JPanel panel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout()); // use border layout
panel.add(northPanel(), BorderLayout.NORTH);
// add north panel with element
panel.add(centerPanel(), BorderLayout.CENTER);
// add center panel with element
panel.add(southPanel(), BorderLayout.SOUTH);
// add south panel with element
return panel;
}
/**
* northPanel contains a intro label
*
* @return the panel
*/
private JPanel northPanel() {
// intro of the game
JLabel intro = new JLabel(
"Pick one animal and I'll guess what's in your mind");
// panel to put the intro
JPanel northPan = new JPanel();
// add panel to the north panel
northPan.add(intro);
return northPan;
}
/**
* center panel contains possible answers and questions
*
* @return the panel
*/
private JPanel centerPanel() {
JPanel aPan = new JPanel(); // panel for the answers
aPan.add(textArea());// add answers to the panel
// panel for questions
JPanel qPan = new JPanel();
question = new JLabel(current.getData().toString()); // get question
qPan.add(question); // add question to label
// panel to contain both answers and question
JPanel center = new JPanel();
center.setLayout(new GridLayout(2, 1)); // use grid layout
// add questions and answers to the panel
center.add(qPan);
center.add(aPan);
return center;
}
/**
* Create a text area to contain all possible answers
*
* @return text area
*/
protected JTextArea textArea() {
// the options to choose
answers = new JTextArea(tree.printLeaves(current));
// divide to multi line
answers.setLineWrap(true);
answers.setWrapStyleWord(true);
// set size
answers.setRows(5);
answers.setColumns(30);
// unable to edit
answers.setEditable(false);
return answers;
}
/**
* south panel contains yes and no buttons
*
* @return the panels
*/
public JPanel southPanel() {
// Create two buttons: yes/no restart
yes = new JButton("YES");
no = new JButton("NO");
restart = new JButton("RESTART");
menu = new JButton("MENU");
// tell buttons what to do
yes.addActionListener(this);
no.addActionListener(this);
restart.addActionListener(this);
menu.addActionListener(this);
// panel for buttons
JPanel panel = new JPanel();
// add buttons to the panel
panel.add(yes);
panel.add(no);
panel.add(restart);
panel.add(menu);
return panel;
}
/**
* tell the buttons what to do
*/
@Override
public void actionPerformed(ActionEvent e) {
// if click button yes
if (e.getSource().equals(yes)) {
yes(); // go to left subtree
} else if (e.getSource().equals(no)) {
no(); // go to right subtree
// if click button restart
} else if (e.getSource().equals(restart)) {
restart(); // restart the game
} else { // if click menu button
// go back to the menu
menu();
}
// update answers
answers.setText(tree.printLeaves(current));
}
/**
* actions for yes button
*/
public void yes() {
// if the node has left child
if (current.getLeftChild() != null) {
// if the node's left child is not a leaf
if (!current.getLeftChild().isLeaf()) {
// update current
current = current.getLeftChild();
// update question
question.setText(current.getData());
} else { // if the node's left child is a leaf, means it's an answer
// update current
current = current.getLeftChild();
// update answer
question.setText("Is your answer: " + current.getData() + "?");
}
} else {
// guess the right answer
question.setText("YEAH!!!!");
}
}
/**
* actions for no button to perform
*/
public void no() {
// if the node has right child
if (current.getRightChild() != null) {
// if the right child is not a lead
if (!current.getRightChild().isLeaf()) {
// update current and the question
current = current.getRightChild();
question.setText(current.getData());
} else {// if right child is a leaf, the node is an answer
// update current
current = current.getRightChild();
// print the answer
question.setText("Is your answer: " + current.getData() + "?");
}
} else {
// update text when guess wrong
question.setText("oh no");
}
}
/**
* action for restart button to perform
*/
protected void restart() {
// reset current as the root
current = tree.getRoot();
// reset the question
question.setText(current.getData());
}
/**
* actions for menu button to perform
*/
protected void menu() {
// go back to the menu
new Menu();
// dispose the game
dispose();
}
}