Boggle Iteration 2: Modified BoggleTray, Boggle, and a GUI

kitewormsSoftware and s/w Development

Nov 3, 2013 (4 years and 6 days ago)

490 views


1

Boggle
Iteration
2:
Modified
BoggleTray
, Boggle, and a GUI


Collaboration
This is the second iteration of Boggle
to

be completed
with the same partner from part 1

(or solo
if
you
had
not registered on
time
)
:

Do not use, show, or look at any other person o
r team's code.


For this second iteration of Boggle, you are asked to add two methods to
BoggleTray
, complete
all of
class
Boggle,
and write an event
-
driven GUI.


Boggle Rules Needed Now


For a Boggle player to get points (incorrect words result in a score

penalty of
-
1), a word input by the user




must be found in the dice tray following the letter connection rules of Boggle



must exist in our official list of words:

http://www.cs.
arizona.edu/people/mercer/CodeDemos/BoggleWords



must contain 3 through 16 char
acters (
1

and
2
letter word guesses
should be
-
1
)



must not already be counted (no duplicate words allowed)


The player's score is determined by summing the points for all words
found in a dictionary according to these
Boggle rules (there
is a 1 penalty
points for incorrect guesses).


Word Length


Points

3


1

4


1

5


2



6


3

7


5

8+


11


Your game must simulate Boggle's dice rolls with the 16 provided dice. The

same die must not always appear in
the same location. Here are the letters of each of the 16 six
-
sided dice where six consecutive letters represent
one die (L R Y T T E are the letters on one Boggle die for example):


L R Y T T E V T H R W E E G H W
N E S E O T I S

A N A E E G I D S Y T T O A T T O W M T O I C U

A F P K F S X L D E R I H C P O A S E N S I E U

Y L D E V R Z N R N H L N M I H U Qu O B B A O J


1)
Add a Default Constructor and a toString() method to
Boggle
Tray


The
BoggleTray

is responsible for storing a set of characters that represent the showing sides of the 16
Boggle dice. It must add these two new methods (the first two,
public

BoggleTray
(
char
[][]
array
)
and
public

boolean

foundInDiceTray
(String search
)

should have been completed
and graded via WebCat already).



// Default constructor creates a random dice tray. It must simulate rolling of the


// actual 16 boggle dice where each die ends up in a random location and each die


// contains the letter
s that come with the actual game (see page 1 for the 16 dice)


public

BoggleTray
() {


}



// Return the
BoggleTray

values on 4 lines spaces between letters like this


// H A H I


//


// V Qu O S


//


// E E S E


//


// T I P W


publ
ic

String toString() {


return

"TODO"
;


}


2

2) Implement
class Boggle


As a preview, here is one test method that shows the expected behavior of most required methods

(shown in
boldface
)

for class Boggle:



@Test


public

void

testGetWordsFoundAfterPre
pareResultsCalledWithSetBoggleTray() {



char
[][] tray = {




{
'E'
,
'R'
,
'H'
,
'I'

},




{
'T'
,
'C'
,
'O'
,
'Z'

},



{
'I'
,
'E'
,
'S'
,
'E'

},



{
'V'
,
'E'
,
'V'
,
'W'

} };



BoggleTray dt =
new

BoggleTray(tray);


Boggle game =
new

Boggle
(
);


game.
setBoggleTray
(dt);


game.
addGuess
(
"see"
);


game.addGuess(
"see"
);


game.addGuess(
"see"
);


game.addGuess(
"tEeS"
);


game.addGuess(
"Receives"
);


game.addGuess(
"Sort"
);


game.addGuess(
"cites"
);


game.addGuess(
"seCreTive"
);



game.addGuess(
"NotHere"
);


game.addGuess(
"NotHere"
);


game.addGuess(
"sew"
);



assertEquals
(27, game.getScore());


assertEquals
(
"[cites, receives, secretive, see, sew, sort, tees]"
,


game.
getWordsFound
().toString());



ass
ertEquals
(
"[nothere]"
,
game.getWordsIncorrect
().toString());


assertTrue
(game.
getWordsNotGuessed
().contains(
"secret"
));


assertTrue
(game.
getWordsNotGuessed
().contains(
"recite"
));

}


Y
ou
are asked
to implement class Boggle using the design given below
.

B
y following the given design of class
Boggle, it will be easy to implement the
last piece of this system: the event
-
driven GUI
.


The Boggle class should encapsulate all the Boggle rules and the collection of words considered to be in the list
of possibl
e words (we need to use some 'dictionary'). Therefore class Boggle must have the two instance
variables shown (plus others) and the seven methods shown below.
You may c
opy and paste the code below
.


import

java.util.List;


public

class

Boggle {




privat
e

BoggleTray

b
oggleTray
;
// Boggle will use your well
-
tested
BoggleTray


// Other instance variables will be needed to store collections such


// as the 80,000+ BoggleWords ...



// Initialize a game with 16 dice to simulate the real game with the real


// Boggle dice. You will need java.util.Random to get a random side. Also make


// sure you shuffle the dice so the dice appear in seemingly random locations!


public

Boggle() {


//
TODO
: Complete this method


}



// Allow testers to set the
Boggl
eTray

to a known one (not random).


public

void

set
BoggleTray
(
BoggleTray

dt) {


this
.
b
oggleTray

= dt;


}



3


// Return the
BoggleTray

object as a textual representation as a String


public

String get
BoggleTray
AsString() {


return

b
oggleTray
.toStrin
g();


}



// Record one word (a string with no whitespace) as one of the users' guesses.


// Do what you want with it, but oneGuess should be processed so all methods


// can return the correct values such as getScore() and getWordsFound().


public

vo
id

addGuess(String oneGuess) {


//
TODO
: Complete this method


}



// Return a list of all words the user entered that count for the score.


// The found words must be in their natural ordering. This method should


// return an empty list until addG
uess(String) is called at least once.


// The returned List<E> could also be empty if no attempts actually


// counted for anything.


//


// There must be no duplicates!


public

List<String> getWordsFound() {


//
TODO
: Complete this method


ret
urn

null
;


}



// Return a list of all words the user entered that do not count for the score


// in their natural order. This list may be empty with no words guessed or all


// guessed words actually count for points.


// There must be no duplicates!


public

List<String> getWordsIncorrect() {


//
TODO
: Complete this method


return

null
;


}



// All words the user could have guessed but didn't in their natural order.


// This list could also be empty at first or if the user guessed ALL words


// in the board and in the list of 80K words (unlikely).


// There must be no duplicates!


public

List<String> getWordsNotGuessed() {


//
TODO
: Complete this method


return

null
;


}




// Return the correct score
. Fo
r every incorrect word
added
, deduct 1 point.


// Ant word guessed that has length less than 2 will count as
-
1 since those


// words are not in our BoggleWords


public

int

getScore() {


//
TODO
: Complete this method


return

0;


}

}


3) Complete a
n Event Driven Program with

a GUI

First thing to do is to layout the graphical user interface components. We will use a null layout manager which
means you will have to
setSize(int, int)
and
setLocation(int, int)

for every component before
adding it to the
JFrame
. Here is the begin
ning of class
BoggleGUI.java

that will place a
JTextArea

to hold the
toString()

version of the
DiceTray

in the upper left of the
JFrame
.


// GUI components you will need

import

jav
a.awt.Color;

import

java.awt.Font;

import

java.awt.event.ActionEvent;

import

java.awt.event.ActionListener;

import

java.util.List;

import

java.util.Scanner;


import

javax.swing.JButton;


4

import

javax.swing.JFrame;

import

javax.swing.JLabel;

import

javax.swi
ng.JOptionPane;

import

javax.swing.JTextArea;


/**


* An event driven graphical user interface for the Boggle game using the


* design of DiceTray and Boggle classes during Rick Mercer's C Sc classes at the UofA


*/

@SuppressWarnings
(
"serial"
)

public

clas
s

BoggleGUI
extends

JFrame {



public

static

void

main(String[] args) {


// having a main method means we can run this class as a Java Application


BoggleGUI theView =
new

BoggleGUI();


theView.setVisible(
true
);


}



private

Boggle
game
;



// GUI com
ponents you will need


private

JTextArea
diceTrayArea
;


private

JButton
newGameButton

=
new

JButton(
"New Game"
);
// will be a warning


private

JButton
endButton

=
new

JButton(
"End game"
);
// will be a warning


private

JTextArea
userInputArea

=
new

JT
extArea(10, 25);
// will be a warning



public

BoggleGUI() {


setUpModel();
// given below


layoutWindow();
// started below


setupListeners();
// will be a compile time error until you add the helper method


startNewGame();
// will be a c
ompile time error until you add the helper method


}



private

void

setUpModel() {


game

=
new

Boggle();


}


// Add five GUI components to this JFrame


private

void

layoutWindow() {


// Set up the JFrame


this
.setSize(500, 270);


this
.setResizable(
false
);


setLocation(10, 10);


setTitle(
"Boggle"
);


setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE
);




// With null layout, you must set the size and location of every component.


setLayout(
null
);



// Layout the dice tray area as a JTextArea



diceTrayArea

=
new

JTextArea();


diceTrayArea
.setEditable(
false
);


diceTrayArea
.setBackground(Color.
cyan
);


diceTrayArea
.setFont(
new

Font(
"Courier"
, Font.
BOLD
, 24));



// Every GUI component must receive a setSize and setLocation


// message an
d then be added to the this JFrame


diceTrayArea
.setSize(210, 220);


diceTrayArea
.setLocation(10, 10);


add(
diceTrayArea
);



// Declare and setSize and setLocation of a JLabel for "Enter your words below"


// setSize and setLocation of the user
input area. Also set line wrap true


// setSize and setLocation of the newGameButton


// setSize and setLocation of the endButton


// If you haven't already done so, add all graphical components to this JFrame

}



5

Pixels


20

260 360 500


|


| |


|



20
-











210
-

270
-


Add Two Listeners and Register them to list
en

t
o the two buttons


You will need
2

inner classes that implement
ActionListener

to respond to the two button clicks.


1.


Each time this button is clicked, create a new game with a randomly generated
DiceTray and show it. The JTextArea for the user input mus
t be set to empty. You will need the
setText(String) method for both JTextAreas.

2.



Each time this button is clicked, show a JOptionPane.
showMessageDialog

with the
results shown as below (use one big String with "
\
n"). The output should look like this with

the game
played as shown above. When this dialog box is closed,
being
a new game.




Grading Criteria

Modified
BoggleTray
, Boggle, and

an Event
-
Driven GUI


____/
65

WebCat turnin to Boggle with All Lists: Prob
lem and Code Coverage as Graded By WebCat


____/

20 GUI looks a lot like the one shown and we can play the game as specified