/
ClassicHanoi.java
575 lines (476 loc) · 12.3 KB
/
ClassicHanoi.java
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/**
*
* ClassicHanoi.java - Classic Implementaion of Towers of Hanoi
*
* @author Brian Etzel
* @version 1.0
*/
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.Stack;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Collections;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
import javax.swing.Timer;
@SuppressWarnings("unchecked")
public class ClassicHanoi extends JPanel implements MouseListener, MouseMotionListener, ActionListener{
private Stack<Rectangle> towerStack[] = new Stack[3];
private Stack<Color> diskColor[] = new Stack[3];
private Rectangle top;
private Color topColor;
private double diskXPos;
private double diskYPos;
private double diskWidth;
private boolean draggable;
private int clickedTower;
private int releasedTower;
public static int numMoves;
public static int minimumMoves;
private int numDisks;
private long startTime;
private long endTime;
private LinkedList<HighScore>[] scores = new LinkedList[8];
private Timer time;
private int seconds;
private int tens;
private int minutes;
private final int DISK_WIDTH_FACTOR = 25;
private final double DISK_HEIGHT = 20;
private final double STARTING_X = 165;
private final double STARTING_Y = 575;
private final int TOWER_ONE_AREA = 315;
private final int TOWER_THREE_AREA = 650;
private final int MAX_NAME_LENGTH = 12;
/**
* Constructor of GamePanel
*
* @param numDisks - The number of disks to play Towers of Hanoi with
*/
public ClassicHanoi(int _numDisks)
{
this.numDisks = _numDisks;
this.minimumMoves = (int)Math.pow(2, numDisks) - 1;
this.startTime = System.currentTimeMillis();
this.numMoves = 0;
this.diskXPos = 0.0;
this.diskYPos = 0.0;
this.diskWidth = 0.0;
towerStack[0] = new Stack<Rectangle>();
towerStack[1] = new Stack<Rectangle>();
towerStack[2] = new Stack<Rectangle>();
diskColor[0] = new Stack<Color>();
diskColor[1] = new Stack<Color>();
diskColor[2] = new Stack<Color>();
addMouseListener(this);
addMouseMotionListener(this);
seconds = 0;
tens = 0;
minutes = 0;
time = new Timer(1000, this);
time.start();
try {
scores = load();
} catch(Exception ex) {
}
launchGame();
}
/**
* Builds and launches Towers of Hanoi game panel
*/
public void launchGame()
{
Color colors[]={Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.DARK_GRAY, Color.WHITE, Color.CYAN};
for (int i = 0; i < numDisks; i++) {
Rectangle disk = new Rectangle();
int diskWidth = (numDisks * DISK_WIDTH_FACTOR) - (20 * i);
disk.setFrame(STARTING_X - diskWidth / 2, STARTING_Y - (i * 20), diskWidth, 20);
towerStack[0].push(disk);
diskColor[0].push(colors[i]);
}
repaint();
}
/**
* Invoked every second to update time
*/
public void actionPerformed(ActionEvent e)
{
seconds++;
if (seconds % 10 == 0) {
tens++;
seconds = 0;
if (tens % 6 == 0){
minutes++;
tens = 0;
}
}
repaint();
}
/**
* Invoked when mouse is pressed
*/
public void mousePressed(MouseEvent e)
{
Point point = e.getPoint();
int n = whichTower(point);
clickedTower = whichTower(point);
if (!towerStack[n].empty()){
top = towerStack[n].pop();
topColor = diskColor[n].pop();
}
}
/**
* Determines if a disk can be placed on top of tower
*/
public void mouseReleased(MouseEvent e)
{
int newTower;
double y = this.getHeight() - DISK_HEIGHT;
releasedTower = whichTower(e.getPoint());
newTower = releasedTower;
if (!towerStack[releasedTower].empty()){
if (towerStack[releasedTower].peek().getWidth() >= top.getWidth()){
y = towerStack[releasedTower].peek().getY() - DISK_HEIGHT;
if (hasMoved()){
numMoves++;
}
}
else{
JOptionPane.showMessageDialog(this, "Disks can only be placed on empty towers, or bigger disks!", "Tower Of Hanoi", JOptionPane.ERROR_MESSAGE);
numMoves++;
newTower = clickedTower;
if (!towerStack[clickedTower].empty()){
y = towerStack[clickedTower].peek().getY() - DISK_HEIGHT;
}
}
}
else{
y = getHeight() - DISK_HEIGHT;
if (hasMoved()) {
numMoves++;
}
}
placeDisk(newTower,y);
repaint();
if (gameOver()){
endGame();
}
}
/**
* Places a disk onto the proper tower
*
* @param tower - The tower the disk will be placed on
* @param y - the y value
*/
private void placeDisk(int tower, double y)
{
double x = (getWidth() / 6 + (getWidth() / 3) * tower - top.getWidth() / 2);
top.setFrame(x, y, top.getWidth(), top.getHeight());
towerStack[tower].push(top);
diskColor[tower].push(topColor);
top = null;
topColor = Color.WHITE;
}
/**
* Repaints disk as mouse is being dragged
*/
public void mouseDragged(MouseEvent e)
{
int mouseX = e.getX();
int mouseY = e.getY();
if (top != null){
top.setFrame(mouseX, mouseY, top.getWidth(), top.getHeight());
repaint();
}
}
/**
* Paints the Towers of Hanoi game panel
*/
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(Color.black);
g2d.fillRect(0, 0, getWidth(), getHeight());
int x1 = 165;
int y1 = getHeight() - 200;
int y2 = getHeight();
g2d.setColor(new Color(102,51,0));
g2d.setStroke(new BasicStroke(10));
g2d.drawLine(x1, y1 ,x1, y2);
g2d.drawLine((3 * x1), y1, (3 * x1), y2);
g2d.drawLine((5 * x1), y1, (5 * x1), y2);
g2d.drawLine(0, y2, getWidth(), y2);
g2d.setColor(topColor);
if (top != null){
g2d.fill(top);
}
drawTower(g2d,0);
drawTower(g2d,1);
drawTower(g2d,2);
//Score & Time information
Font font = new Font("monospace", Font.BOLD, 15);
g.setColor(Color.RED);
g.setFont(font);
g.drawString("Moves: " + String.valueOf(numMoves), 25, 25);
int stringWidth = g.getFontMetrics().stringWidth("Minimum Moves Required: ");
g.drawString("Minimum Moves Required: " + minimumMoves, getWidth() - stringWidth, 25);
g.drawString(minutes + ":" + tens + seconds, getWidth() / 2, 25);
}
/**
* Paints the towers, with disks, if necessary
*/
private void drawTower(Graphics2D g2d, int n)
{
if (!towerStack[n].empty()){
for (int i = 0; i < towerStack[n].size(); i++){
g2d.setColor(diskColor[n].get(i));
g2d.fill(towerStack[n].get(i));
}
}
}
/**
* Determines the area around the tower where mouse was released clicked
*
* @param p - Point object of where mouse was released/clicked
* @return An integer value determining which tower was acted upon
*/
private int whichTower(Point point)
{
int mouseX = (int) point.getX();
int whichTower = -1;
if (mouseX <= TOWER_ONE_AREA){
whichTower = 0;
}
else if (mouseX > TOWER_ONE_AREA && mouseX < TOWER_THREE_AREA){
whichTower = 1;
}
else if (mouseX >= TOWER_THREE_AREA){
whichTower = 2;
}
return whichTower;
}
/**
* Determines if a disk has moved from original tower for purposes of
* increasing the number of moves made
*
* @return Boolean determining if disk has moved.
*/
private boolean hasMoved()
{
boolean hasMoved = false;
if (clickedTower != releasedTower) {
hasMoved = true;
}
return hasMoved;
}
/**
* Determines if the game has been won
*/
private boolean gameOver()
{
boolean gameOver = false;
if (towerStack[2].size() == (numDisks)){
gameOver = true;
}
return gameOver;
}
/**
* Starts the process of ending a game
*/
private void endGame()
{
time.stop();
this.endTime = System.currentTimeMillis();
HighScore newScore = new HighScore(getPlayerName(), calculateScore());
scores[numDisks].add(newScore);
Collections.sort(scores[numDisks]);
try {
save(scores);
} catch (Exception ex) {
}
showScore();
}
/**
* Gets the name of the player for the high score list
*
* @return Name of the player as a string
*/
public String getPlayerName()
{
String playerName = "";
Object[] options1 = { "OK" };
JPanel panel = new JPanel();
panel.add(new JLabel("Enter your name for the high score list: "));
JTextField textField = new JTextField(10);
panel.add(textField);
int accepted = JOptionPane.showOptionDialog(null, panel, "Towers of Hanoi",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, null);
if (accepted == JOptionPane.YES_OPTION){
playerName = textField.getText();
}
if (playerName.length() > MAX_NAME_LENGTH){
playerName = trimName(playerName);
}
return playerName;
}
/**
* Trims the size of the name down to the maximum length if necessary
*
* @param longName - The name to be trimmed down
* @return new name string of the maximum length
*/
private String trimName(String longName)
{
String trimmedName = "";
for (int i = 0; i < MAX_NAME_LENGTH; i++){
trimmedName += longName.charAt(i);
}
return trimmedName;
}
/**
* Displays End Game window
*/
private void showScore()
{
Object[] options1 = { "Play again?",
"Exit Game" };
String display = buildDisplay();
JPanel panel = new JPanel();
panel.add(new JLabel(display));
int accepted = JOptionPane.showOptionDialog(null, panel, "High Scores",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options1, null);
if (accepted == JOptionPane.YES_OPTION){
playAgain();
}
else if (accepted == JOptionPane.NO_OPTION){
System.exit(0);
}
}
/**
* Formats the display for High Score list.
*
* @return String containing the High Score List display format
*/
private String buildDisplay()
{
String display = "<html><body>";
Iterator<HighScore> itr = scores[numDisks].iterator();
int i = 0;
while (i < 10){
if (itr.hasNext()){
display += "<br>" + (i + 1) + ": " + itr.next().toString();
}
i++;
}
display += "</body></html>";
return display;
}
/**
* Prepares panel for New Game if necessary
*/
private void playAgain()
{
while (!towerStack[2].empty()){
towerStack[2].pop();
}
startTime = System.currentTimeMillis();
numMoves = 0;
seconds = 0;
tens = 0;
minutes = 0;
time.start();
launchGame();
}
/**
* Calculates the score for the game.
*/
private int calculateScore()
{
int highScore;
int timeDifference = (int)(endTime - startTime);
highScore = ((numMoves - minimumMoves) + 1) * (timeDifference);
return highScore;
}
/**
* Saves a LinkedList of HighScore as a Serializable file
*
* @param scores - An array of LinkedList - LinkedList<HighScore>[]
*/
private void save(LinkedList<HighScore>[] scores) throws IOException, ClassNotFoundException
{
try {
FileOutputStream fos = new FileOutputStream("highscore");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(scores);
oos.flush();
} catch (Exception ex) {
}
}
/**
* Loads a Serializable instance of a LinkedList<HighScore>
*
* @return LinkedList<HighScore>
*/
@SuppressWarnings("unchecked")
private LinkedList<HighScore>[] load() throws IOException, ClassNotFoundException, InterruptedException
{
LinkedList<HighScore>[] scores = new LinkedList[8];
try {
FileInputStream fis = new FileInputStream("highscore");
ObjectInputStream ois = new ObjectInputStream(fis);
scores = (LinkedList<HighScore>[]) ois.readObject();
fis.close();
ois.close();
} catch (Exception e) {
//return null object if not found
}
return scores;
}
/**
* Get minimumMoves
*/
public int getMinimumMoves()
{
return minimumMoves;
}
/**
* Invoked when the mouse is clicked
*/
public void mouseClicked(MouseEvent e)
{
}
/**
* Invoked when the mouse is entered
*/
public void mouseEntered(MouseEvent e)
{
}
/**
* Invoked when the mouse is exited
*/
public void mouseExited(MouseEvent e)
{
}
/**
* Invoked when the mouse is moved
*/
public void mouseMoved(MouseEvent e)
{
}
}