This repository was archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMindReader.java
More file actions
371 lines (310 loc) · 10.7 KB
/
Copy pathMindReader.java
File metadata and controls
371 lines (310 loc) · 10.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
/**
* Runs the Mind Reader game
*/
public class MindReader
{
/** Maximum numbers that the Mind Reader can guess, if value is 100, then it will ask you to think of a number from 1 to 100. */
public static final int MAX_NUMBER = 100;
/** Numbers to be displayed in each question. */
public static final int NUMBERS_PER_QUESTION = 25;
/** Current question number. */
private int currentQuestion = 0;
/** Holds all possible numbers that the user has in mind. */
private ArrayList<Integer> possibleNumbers = new ArrayList<Integer>();
/** Holds all denied numbers that is not in the user's mind. */
private ArrayList<Integer> deniedNumbers = new ArrayList<Integer>();
/** Holds current question numbers that are shown in the question (will be used for possible/denied numbers calculation). */
private ArrayList<Integer> currentQuestionNumbers = new ArrayList<Integer>();
/** Holds current question numbers that are visible to the user and some numbers might not be used for possible/denied numbers calculation. */
private ArrayList<Integer> currentQuestionNumbersWithPlaceholder = new ArrayList<Integer>();
/** The application frame. */
private JFrame frame;
/** Label to hold the text before starting the game. */
private JLabel introduction = new JLabel("Think of a number from 1 to 100, and then click on start.");
/** Start button. */
private JButton start = new JButton("Start");
/** Label for holding the current question number. */
private JLabel questionNo = new JLabel();
/** Label for holding the question. */
private JLabel question = new JLabel("Is the number you're thinking of mentioned below?");
/** Label for holding the visible numbers on the question. */
private JLabel questionNumbers = new JLabel();
/** Yes button. */
private JButton yes = new JButton("Yes");
/** No button. */
private JButton no = new JButton("No");
/** Label for holding the result text */
private JLabel resultText = new JLabel("The number that you was thinking of was:");
/** Label for holding the result number */
private JLabel result = new JLabel();
/**
* Starts up the application.
*/
public static void main(String[] args)
{
// Start application by building GUI
new MindReader().buildGUI();
}
/**
* Builds the GUI and all the components.
*/
public void buildGUI()
{
// The application's main frame
frame = new JFrame("Mind Reader");
frame.getContentPane().setLayout(new FlowLayout());
// Add start button listener
start.addActionListener(new StartButtonListener());
// Add components to frame
frame.getContentPane().add(BorderLayout.CENTER, introduction);
frame.getContentPane().add(BorderLayout.SOUTH, start);
// Set default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set size and make it visible
frame.setSize(650,100);
frame.setVisible(true);
}
/**
* Starts the Mind Reader by performing all the initializations, this method is called when the "Start" button is pressed.
*/
public void start()
{
// Remove main page components
start.setVisible(false);
introduction.setVisible(false);
// Initiate possibleNumbers by adding all the numbers to possibleNumbers
for(int n=1; n <= MAX_NUMBER; n++)
{
possibleNumbers.add(n);
}
// Shuffle possibleNumbers
Collections.shuffle(possibleNumbers);
// Show first question
question();
}
/**
* Shows the next question.
* This is called at the end of the start() method or after a question has been answered.
*/
public void question()
{
// Increment question number
currentQuestion++;
// If this is the first question, set up GUI components for question
if(currentQuestion == 1)
{
// Add components to frame, otherwise just edit values
frame.getContentPane().add(BorderLayout.CENTER, questionNo);
frame.getContentPane().add(BorderLayout.CENTER, question);
frame.getContentPane().add(BorderLayout.CENTER, questionNumbers);
frame.getContentPane().add(BorderLayout.SOUTH, yes);
frame.getContentPane().add(BorderLayout.SOUTH, no);
// Add listeners to buttons
yes.addActionListener(new YesButtonListener());
no.addActionListener(new NoButtonListener());
}
// Question No (for output)
String questionNoString = "Question "+currentQuestion+":";
questionNo.setText(questionNoString);
// Check if possibleNumbers more than NUMBERS_PER_QUESTION
if(possibleNumbers.size() > NUMBERS_PER_QUESTION)
{
// Loop possibleNumbers based on numbersPerQuestion
for(int i = 0; i < NUMBERS_PER_QUESTION; i++)
{
// Copy current element from possibleNumbers to currentQuestionNumbers
currentQuestionNumbers.add(possibleNumbers.get(i));
}
// Create string to show question numbers in output
String questionNumbersString = "";
// Loop all the currentQuestionNumbersWithPlaceholder and add them to the output string
int iterationCounter = 1;
for (int i : currentQuestionNumbers)
{
// Check if this is the last number
if(iterationCounter != NUMBERS_PER_QUESTION)
{
// If not last number, add a comma after the number
questionNumbersString += i + ", ";
}
else
{
// If last number, don't add a comma after the number
questionNumbersString += i;
}
iterationCounter++;
}
// Add question numbers to a label
questionNumbers.setText(questionNumbersString);
}
else
{
// Take half of the numbers from possibleNumbers
int numbersToTake = (int) (possibleNumbers.size() * 0.5);
for(int i = 0; i < numbersToTake; i++)
{
// Copy current element from possibleNumbers to currentQuestionNumbers
currentQuestionNumbers.add(possibleNumbers.get(i));
// Also copy to placeholder array
currentQuestionNumbersWithPlaceholder.add(possibleNumbers.get(i));
}
// Shuffle denied numbers
Collections.shuffle(deniedNumbers);
// Take placeholder numbers from deniedNumbers to complete NUMBERS_PER_QUESTION
int placeholderNumbersToTake = NUMBERS_PER_QUESTION - numbersToTake;
for(int i = 0; i < placeholderNumbersToTake; i++)
{
// Copy current element from deniedNumbers to currentQuestionNumbersWithPlaceholder
currentQuestionNumbersWithPlaceholder.add(deniedNumbers.get(i));
}
// Shuffle currentQuestionNumbersWithPlaceholder
Collections.shuffle(currentQuestionNumbersWithPlaceholder);
// Create string to show question numbers in output
String questionNumbersString = "";
// Loop all the currentQuestionNumbersWithPlaceholder and add them to the output string
int iterationCounter = 1;
for (int i : currentQuestionNumbersWithPlaceholder)
{
// Check if this is the last number
if(iterationCounter != NUMBERS_PER_QUESTION)
{
// If not last number, add a comma after the number
questionNumbersString += i + ", ";
}
else
{
// If last number, don't add a comma after the number
questionNumbersString += i;
}
iterationCounter++;
}
// Add question numbers to a label
questionNumbers.setText(questionNumbersString);
}
}
/**
* Takes an answer and processes it accordingly.
*
* @param answer True if user answered the question as "Yes", false if the user answered the question as "No".
*/
public void answer(boolean answer)
{
// If user clicked "Yes"
if(answer == true)
{
// Remove all elements from possibleNumbers and deniedNumbers
possibleNumbers.clear();
deniedNumbers.clear();
// Copy all numbers from currentQuestionNumbers to possibleNumbers
for(int i = 0; i < currentQuestionNumbers.size(); i++)
{
// Add number to possibleNumbers
possibleNumbers.add(currentQuestionNumbers.get(i));
}
// Shuffle possibleNumbers
Collections.shuffle(possibleNumbers);
// Add all numbers to deniedNumbers except those in possibleNumbers
for(int i = 0, number = 1; i < MAX_NUMBER; i++, number++)
{
// If number is in possibleNumbers
if(possibleNumbers.contains(number))
{
// Stop current iteration and don't add to deniedNumbers
continue;
}
// Add to deniedNumbers
deniedNumbers.add(number);
}
// Shuffle deniedNumbers
Collections.shuffle(deniedNumbers);
}
else
{
// If user answered "No"
// Remove the currentQuestionNumbers from possibleNumbers
for(int i = 0; i < currentQuestionNumbers.size(); i++)
{
// Remove first element because when we remove one element.
// The next element will be the first element in the next iteration.
possibleNumbers.remove(0);
}
// Move from currentQuestionNumbers to deniedNumbers
for(int i = 0; i < currentQuestionNumbers.size(); i++)
{
// Copy current element from currentQuestionNumbers to deniedNumbers
deniedNumbers.add(currentQuestionNumbers.get(i));
}
}
// Check if one element remains from possibleNumbers
if(possibleNumbers.size() == 1)
{
// Show result
result();
}
else
{
// Clean up arrays for next question
currentQuestionNumbers.clear();
currentQuestionNumbersWithPlaceholder.clear();
// Show next question
question();
}
}
/**
* Shows the result to the user.
* The answer() method will call this method when possible numbers has only 1 remaining number.
*/
public void result()
{
// Remove question components
questionNo.setVisible(false);
question.setVisible(false);
questionNumbers.setVisible(false);
yes.setVisible(false);
no.setVisible(false);
// The first element from possibleNumbers is the result
// Normally, there would be only 1 number in possibleNumber when we reach here
int resultNumber = possibleNumbers.get(0);
result.setText(String.valueOf(resultNumber));
// Add components for result
frame.getContentPane().add(BorderLayout.CENTER, resultText);
frame.getContentPane().add(BorderLayout.CENTER, result);
}
/**
* A button listener for starting the game by invoking the start() method when the user clicks on the "Start" button.
*/
public class StartButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
// Execute start() method when start button is clicked
start();
}
}
/**
* A button listener for telling the answer() method that the user clicked on the "Yes" button.
*/
public class YesButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
// Execute answer() method with true (yes) as parameter when "Yes" button is clicked
answer(true);
}
}
/**
* A button listener for telling the answer() method that the user clicked on the "No" button.
*/
public class NoButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
// Execute answer() method with false (no) as parameter when "No" button is clicked
answer(false);
}
}
}