CS 132: Intro to Computer Science II

Spring 1999

Assignment 9

Applets and User Interfaces: The Wumpus Applet

Due Monday April 5

For this week's lab, you will adapt your Maze and game code to run "Hunt the Wumpus" as an applet with a graphical user interface. To complete this lab, you will need to modify the Maze class, design a graphical user interface, and implement your design to work with the modified Maze class.

Modifying the Maze Class

A good program design separates out the internal functions (e.g. the Maze class) from the interface (e.g. the Wumpus class). This way, the user interface can be redisigned without touching the class that provides the internal functions for running the program. Unfortunately, the Maze class does not fully honor this separation. In particular, the toss and move methods write output to the screen describing the results of performing the action.

Rather than trying to work the user interface into the Maze class, you should modify the Maze class so that it returns information so that the output can be done elsewhere. To start, first copy your code from lab6 into the new lab8 directory. Then you will need to remove the print statements from the move and toss methods and have these methods return strings. Your Applet class will then display the strings for the user to see what happened.

Multiple lines of text can be returned from a method if the subsequent lines are added to the string, separated by the character '\n'. Then, once the lines have all been added, return the string. Here's an example:

String phrase = "The grenade explodes";
// some intermediate code
phrase += "\n...and hits a wumpus in the adjacent room!";
Writing out this string would produce the following output:
The grenade explodes
...and hits a wumpus in the adjacent room!

You will also need to replace the constructor in the Maze class. The old constructor reads from a file, which is not allowed for an applet downloaded from a server. The new constructor reads the file from a Web-based URL. This new constructor is available for you in the file ~miller/cs132/lab9/AppletMazeConstructor.java. Assuming that your configuration file is "caves.dat", the new constructor is called with the code new Maze(getCodeBase() + "caves.dat"). Note that getCodeBase() is an applet method and can only be called from an applet.

Designing the User Interface

Before you start writing code for the interface, you should draw a sketch of how you would like the interface to look. Here you decide what components are appropriate (e.g. buttons, lists, text fields, text areas, check boxes, etc.) for running the adventure game. To some extent you are limited because much of the game interaction is text-based. Still, there is plenty of freedom to design a good interface.

Keep the user in mind when designing your interface. Before you start your implementation, have the TA or the instructor review your design. In addition to some feedback on your design's usability, you will probably receive some suggestions on how to proceed with the implementation.

Implementing your Design

Call your applet class WumpusApplet and implement it in the file WumpusApplet.java.

In implementing your user interface, the FlowLayout manager may not provide adequate control for placing your components. While you may want to start by trying this layout manager, eventually you may choose to incorporate some combination of panels and alternate layout managers (e.g. GridLayout and BorderLayout). We will discuss these approaches in class.

Finally, you should review your design and implementation by asking at least one other person to play your game. You may learn that your design still has some aspects that make it difficult to use. If possible, respond to this feedback and modify your implementation so that the game is easier and more enjoyable to play.

Submission

Turn in hardcopies of the new Maze.java and WumpusApplet.java. You should also leave these files in your lab9 directory so that they can be tested online.