-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoggleGUI.java
626 lines (544 loc) · 19.3 KB
/
BoggleGUI.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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/**
* Copyright (C) 2002 Michael Green <[email protected]>
*
* Copyright (C) 2002 Paul Kube <[email protected]>
*
* Copyright (C) 2005 Owen Astrachan <[email protected]>
*
* Copyright (C) 2011 Hoa Long Tam <[email protected]> and Armin Samii
*
* This file is part of CS Boggle.
*
* CS Boggle is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* CS Boggle is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* CS boggle. If not, see <http://www.gnu.org/licenses/>.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.ProgressMonitor;
import javax.swing.ProgressMonitorInputStream;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
/**
* A GUI for the game of Boggle.
*
* BoggleGUI defines and implements methods to do these things:
*
* 1. Draw the boggle board. 2. Draw and update player word lists. 3. Update the
* scoreboard.
*
* These depend only on BoggleModel and BogglePlayer, see those classes for
* methods this GUI/View rely on.
*
* @author Michael Green
* @author Paul Kube
* @author Owen Astrachan
*
*/
public class BoggleGUI extends JFrame {
private final LexiconInterface myLexicon;
private AutoPlayerLexiconFirst computerPlayer;
private Player humanPlayer;
private final WordOnBoardFinder myFinder;
private BoggleBoard myBoard;
private BoggleBoardPanel myBoardPanel4;
private BoggleBoardPanel myBoardPanel5;
private BoggleBoardPanel myBoardPanel;
private PlayerView humanArea, computerArea;
private WordEntryField wordEntryField;
private int myBoardSize;
public BoggleGUI(LexiconInterface lex, WordOnBoardFinder finder,
InputStream stream, AutoPlayerLexiconFirst compPlayer) {
super("Welcome to Boggle!");
myLexicon = lex;
myBoardSize = 5;
myFinder = finder;
initLexicon(stream);
initPanels();
initPlayers(compPlayer);
setUpMenuBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
newGame();
}
/**
* Get ready for a new game, given a letter list specification of the Boggle
* board.
*/
public void newGame() {
myBoard = BoggleBoardFactory.getBoard(myBoardSize);
// set up views for new game
if (myBoardSize == 4) {
if (myBoardPanel != myBoardPanel4) {
myBoardPanel = myBoardPanel4;
getContentPane().remove(myBoardPanel5);
getContentPane().add(myBoardPanel, BorderLayout.CENTER);
}
} else {
if (myBoardPanel != myBoardPanel5) {
myBoardPanel = myBoardPanel5;
getContentPane().remove(myBoardPanel4);
getContentPane().add(myBoardPanel, BorderLayout.CENTER);
}
}
myBoardPanel.newGame();
humanArea.setReady();
computerArea.setReady();
myBoardPanel.unHighlightAllDice();
wordEntryField.setReady();
((JPanel)getContentPane()).revalidate();
repaint();
}
private void initPlayers(AutoPlayerLexiconFirst compPlayer) {
computerPlayer = compPlayer;
computerPlayer.setView(computerArea);
humanPlayer = new HumanPlayer(myLexicon, "Human");
humanPlayer.setView(humanArea);
}
/**
* Let the computer player take its turn.
*/
public void computerPlay() {
computerArea.setName("Thinking!");
computerArea.paintImmediately(computerArea.getVisibleRect());
int minLength = myBoardSize == 4 ? 3 : 4;
computerPlayer.findAllValidWords(myBoard, myLexicon, minLength);
computerArea.setName("Computer");
for (String s : computerPlayer) {
computerArea.showWord(s, myFinder.cellsForWord(myBoard, s),
computerPlayer);
}
myBoardPanel.unHighlightAllDice(); // leave board unhighlighted when
// done
}
/**
* Read word list from file with name WORDLISTFILENAME, and pass a Set
* containing those words to the computer player to intialize its lexicon.
*/
private void initLexicon(InputStream stream) {
ProgressMonitorInputStream pmis;
ProgressMonitor progress = null;
pmis = new ProgressMonitorInputStream(this, "reading words", stream);
progress = pmis.getProgressMonitor();
progress.setMillisToDecideToPopup(10);
Scanner s = new Scanner(pmis);
myLexicon.load(s);
try {
pmis.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Error Closing Stream", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void initPanels() {
Container contentPane = getContentPane();
humanArea = new PlayerView("Me");
computerArea = new PlayerView("Computer");
myBoardPanel4 = new BoggleBoardPanel(4, 4);
myBoardPanel5 = new BoggleBoardPanel(5, 5);
myBoardPanel = myBoardPanel5;
wordEntryField = new WordEntryField();
contentPane.add(wordEntryField, BorderLayout.SOUTH);
contentPane.add(humanArea, BorderLayout.WEST);
contentPane.add(myBoardPanel, BorderLayout.CENTER);
contentPane.add(computerArea, BorderLayout.EAST);
contentPane.add(makeProgressBar(), BorderLayout.NORTH);
}
private JPanel makeProgressBar() {
JPanel panel = new JPanel();
return panel;
}
public void gameOver() {
repaint();
wordEntryField.setUnready();
computerPlay();
}
public void showError(String message) {
JOptionPane.showMessageDialog(this, message, "Boggle Error",
JOptionPane.ERROR_MESSAGE);
}
private void setUpMenuBar() {
// Set Up Menu Bar
JMenuBar menu = new JMenuBar();
// Game Menu
JMenu gameMenu = new JMenu("Game");
menu.add(gameMenu);
JMenuItem newRandom = new JMenuItem("New Game");
gameMenu.add(newRandom);
newRandom.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
Toolkit.getDefaultToolkit()
.getMenuShortcutKeyMask()));
newRandom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newGame();
}
});
gameMenu.addSeparator();
ButtonGroup bg = new ButtonGroup();
JRadioButtonMenuItem size4 = new JRadioButtonMenuItem("4x4 board");
size4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myBoardSize = 4;
}
});
size4.setSelected(false);
bg.add(size4);
gameMenu.add(size4);
JRadioButtonMenuItem size5 = new JRadioButtonMenuItem("5x5 board");
size5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myBoardSize = 5;
}
});
size4.setSelected(true);
bg.add(size5);
gameMenu.add(size5);
gameMenu.addSeparator();
JMenuItem quitGame = new JMenuItem("Quit");
gameMenu.add(quitGame);
quitGame.setMnemonic('Q');
quitGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// Help menu
JMenu helpMenu = new JMenu("Help");
menu.add(helpMenu);
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem aboutGame = new JMenuItem("About...");
helpMenu.add(aboutGame);
aboutGame.setMnemonic(KeyEvent.VK_A);
aboutGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(BoggleGUI.this,
"Compsci Boggle, brought to you\n"
+ "by educators, students,\n"
+ "the Great Courtney Wang,\n"
+ "and, of course, you.",
"About Game", JOptionPane.PLAIN_MESSAGE);
}
});
setJMenuBar(menu);
}
/**
* A class defining the visual appearance of a Boggle board, and defining some
* related methods.
*/
private class BoggleBoardPanel extends JPanel {
public final Color BACKGROUNDCOLOR = new Color(255, 219, 13);
private final DiePanel theDice[][]; // draw the Dice here
private final int rows, cols;
BoggleBoardPanel(int rows, int cols) {
this.rows = rows;
this.cols = cols;
// create a JPanel with rowsXcols GridLayout to hold the DiePanels
JPanel innerPanel = new JPanel();
innerPanel.setLayout(new GridLayout(rows, cols, 1, 1));
innerPanel.setBackground(BACKGROUNDCOLOR);
// Create Individual DiePanels, and add them
theDice = new DiePanel[rows][cols];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
theDice[row][col] = new DiePanel();
innerPanel.add(theDice[row][col]);
}
}
innerPanel.setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10,
BACKGROUNDCOLOR));
this.add(innerPanel);
}
/**
* There's a new game, update display to reflect this new game.
*/
public void newGame() {
// Set the DiePanels with given letters
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
String s = myBoard.getFace(row, col).toUpperCase();
if (s.length() > 1) {
s = "Qu";
}
theDice[row][col].setFace(s);
}
}
}
public void highlightDice(java.util.List<BoardCell> locations) {
if (locations == null) {
return;
}
unHighlightAllDice();
for (BoardCell cell : locations) {
highlightDie(cell.row, cell.col);
}
this.paintImmediately(getVisibleRect());
}
/**
* Highlight the specified die, given row and column.
*/
public void highlightDie(int row, int column) {
theDice[row][column].highlight();
}
/**
* Unhighlight all dice.
*/
public void unHighlightAllDice() {
for (int row = 0; row < theDice.length; row++) {
for (int col = 0; col < theDice[row].length; col++) {
theDice[row][col].unHighlight();
}
}
this.paintImmediately(getVisibleRect());
}
// For displaying one Die on the board
private class DiePanel extends JPanel {
private String face;
private boolean isHighlighted;
private final JLabel faceLabel;
private final Color DIECOLOR = new Color(230, 230, 230);
private final Color FACECOLOR = new Color(3, 51, 217);
private final Font FACEFONT = new Font("SansSerif", Font.PLAIN, 24);
private final int DIESIZE = 50;
public DiePanel() {
face = new String("");
faceLabel = new JLabel("", SwingConstants.CENTER);
setLayout(new BorderLayout());
add(faceLabel, BorderLayout.CENTER);
setBackground(BACKGROUNDCOLOR);
setSize(DIESIZE, DIESIZE);
}
@Override
public Dimension getPreferredSize() {
return (new Dimension(DIESIZE + 1, DIESIZE + 1));
}
@Override
public Dimension getMinimumSize() {
return (getPreferredSize());
}
public void setFace(String chars) {
if (chars.length() > 1) {
face = chars.substring(0, 1) + chars.substring(1).toLowerCase();
} else {
face = chars;
}
}
public String getFace() {
return face;
}
/**
* Draw one die including the letter centered in the middle of the die. If
* highlight is true, we reverse the background and letter colors to
* highlight the die.
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int centeredXOffset, centeredYOffset;
// Draw the blank die
g.setColor((isHighlighted) ? FACECOLOR : DIECOLOR);
g.fillRoundRect(0, 0, DIESIZE, DIESIZE, DIESIZE / 2, DIESIZE / 2);
// Outline the die with black
g.setColor(Color.black);
g.drawRoundRect(0, 0, DIESIZE, DIESIZE, DIESIZE / 2, DIESIZE / 2);
Graphics faceGraphics = faceLabel.getGraphics();
faceGraphics.setColor(isHighlighted ? DIECOLOR : FACECOLOR);
Color myColor = isHighlighted ? DIECOLOR : FACECOLOR;
faceLabel.setForeground(myColor);
faceLabel.setFont(FACEFONT);
faceLabel.setText(face);
}
public void unHighlight() {
isHighlighted = false;
}
public void highlight() {
isHighlighted = true;
}
}
} // class BoggeBoard
// Maintains name, score, and word list information for one player
class PlayerView extends JPanel implements PlayerViewInterface {
private String playerName;
private final Font ScoreFont = new Font("SansSerif", Font.PLAIN, 18);
private final Font WordFont = new Font("Geneva", Font.PLAIN, 9);
private final Font LabelFont = new Font("Helvitica", Font.PLAIN, 9);
private final JPanel topPanel, wordPanel, namePanel, scorePanel;
private final ExpandableList myWordList;
private final JLabel nameText, scoreText;
public PlayerView(String player) {
playerName = new String(player);
// Set-Up Top of Score Area
namePanel = new JPanel();
nameText = new JLabel(player);
nameText.setFont(ScoreFont);
namePanel.setLayout(new BorderLayout());
namePanel.add(nameText, BorderLayout.CENTER);
scorePanel = new JPanel();
scoreText = new JLabel(" 0");
scoreText.setFont(ScoreFont);
scorePanel.setLayout(new BorderLayout());
scorePanel.add(scoreText, BorderLayout.CENTER);
topPanel = new JPanel();
BoxLayout layout = new BoxLayout(topPanel, BoxLayout.LINE_AXIS);
topPanel.setLayout(layout);
topPanel.add(namePanel);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
topPanel.add(scorePanel);
topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
// Create bordering for top panel
Border raisedBevel, loweredBevel, compound;
raisedBevel = BorderFactory.createRaisedBevelBorder();
loweredBevel = BorderFactory.createLoweredBevelBorder();
compound = BorderFactory.createCompoundBorder(raisedBevel, loweredBevel);
topPanel.setBorder(compound);
// Set-Up area to display word list
wordPanel = new JPanel();
Border etched = BorderFactory.createEtchedBorder();
TitledBorder etchedTitle = BorderFactory.createTitledBorder(etched,
"Word List");
etchedTitle.setTitleJustification(TitledBorder.RIGHT);
wordPanel.setBorder(etchedTitle);
myWordList = new ExpandableList();
myWordList.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String word = e.getActionCommand();
java.util.List<BoardCell> list = myFinder.cellsForWord(myBoard, word);
myBoardPanel.highlightDice(list);
}
});
wordPanel.add(new JScrollPane(myWordList,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
setLayout(new BorderLayout(30, 30));
add(topPanel, BorderLayout.NORTH);
add(wordPanel, BorderLayout.CENTER);
}
@Override
public void showError(String word, String error) {
JOptionPane.showMessageDialog(this, word + ": " + error + "!!!", error,
JOptionPane.ERROR_MESSAGE);
wordEntryField.clear();
}
public void setReady() {
resetScore(); // zero out score
myWordList.clear(); // remove words from Panel/list
paintImmediately(getVisibleRect());
}
@Override
public void setName(String newName) {
playerName = newName;
nameText.setText(playerName);
repaint();
}
@Override
public void showWord(String word,
java.util.List<BoardCell> letterLocations, Player player) {
myWordList.add(word);
scoreText.setText(player.getScore() + "");
scoreText.paintImmediately(scoreText.getVisibleRect());
myWordList.paintImmediately(myWordList.getVisibleRect());
wordEntryField.clear(); // clear the wordEntryField text
}
public void resetScore() {
scoreText.setText(0 + "");
scoreText.paintImmediately(scoreText.getVisibleRect());
}
} // class ScoreArea
class WordEntryField extends JPanel {
private final JTextField textField;
private final JButton myDoneButton;
private final StringBuilder myString;
public WordEntryField() {
// Set up for human player's Text Entry Field
textField = new JTextField(30);
myString = new StringBuilder("");
// Add listener to text entry field
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String s = textField.getText().toLowerCase();
java.util.List<BoardCell> list = myFinder.cellsForWord(myBoard, s);
myString.delete(0, myString.length());
for (BoardCell cell : list) {
myString.append(myBoard.getFace(cell.row, cell.col));
}
if (!myString.toString().equals(s)) {
showError(s + " not on board");
} else if (humanPlayer.add(textField.getText().toLowerCase())) {
humanArea.showWord(s, list, humanPlayer);
myBoardPanel.highlightDice(list);
}
}
});
myDoneButton = new JButton("DONE");
myDoneButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
gameOver();
}
});
this.add(new JLabel("Enter word: "));
this.add(textField);
this.add(myDoneButton);
setUnready();
}
public void clear() {
textField.setText("");
}
public void setUnready() {
clear();
textField.setEditable(false);
paintImmediately(getVisibleRect());
// attempt to give up focus to top-level frame
BoggleGUI.this.requestFocus();
}
public void setReady() {
textField.setEditable(true);
textField.requestFocus();
}
} // class WordEntryField
}