Using the objects created earlier (Location, Exit), we'll create a very simple text-adventure game. Its minimal in features, and will contain only three locations, but it does serve to show how easy it is to create rooms and exits, as well as showing the interactions between our two classes.
For the purposes of the demonstration, a Java applet has been written. Writing the demo as an applet allows readers to see the example working within a web browser. This is not to say that a text-adventure game can't be written as a stand-alone application; indeed for many games this would be the preferable approach.
The example applet is relatively straightforward. Providing you've written applets before, you'll understand event handling, and initialisation of applets. What is different from normal applets is the use of the Location and Exit objects, which is discussed below.
Code placed in the init() method of an applet is guaranteed to be executed once, and once only in the lifecycle of the applet. Contained within this method is the code to create three location objects, as well as exits to link them together.
// Create two locations Location l1 = new Location ("Entrance to hall", "You stand at the entrance....."); Location l2 = new Location ("End of hall", "You have reached the end....."); Location l3 = new Location ("Small study", "This is a small study...."); // Create an exit for l1 l1.addExit (new Exit(Exit.NORTH, l2)); l1.addExit (new Exit(Exit.EAST, l3)); // Create an exit for l2 l2.addExit (new Exit(Exit.SOUTH, l1)); // Create an exit for l3 l3.addExit (new Exit(Exit.WEST, l1));
Creation of the location objects is simple enough - the constructor takes as parameters a title and description string. Exit objects are also made simple, by using the predefined constants for directions (such as up, down, in, out, east, west, etc.)
For simplicity, the applet handles only one event, a button press. When the user has entered a direction, and clicked the 'go' button, the applet will parse the command and attempt to move in the requested direction. The applet knows which directions are valid, because each location can return a vector of Exits via the getExits() method.
In an earlier part of this tutorial, we covered how to insert and remove objects from a vector. Until this point, we've never had a need to get a reference to an object out from a vector. Since we need to search through the vector for a matching exit, we must traverse every element in the vector. To do this, we use an enumeration loop.
// Search for an exit match for (Enumeration e = currentLocation.getExits().elements(); e.hasMoreElements();) { Exit an_exit = (Exit) e.nextElement();
// Process each exit in some manner }
If we wish to check for an exit match, we can use the compare the command entered by the user with the direction name (and shortened direction name) stored within each Exit object. When an exit match is detected, we can assign to the currentLocation object reference the room that a particular exit leads to.
if ( (an_exit.getDirectionName().compareTo(command) == 0) || (an_exit.getShortDirectionName().compareTo(command) == 0 ) ) { // Set location to the location pointed to by exit currentLocation = an_exit.getLeadsTo(); // Show new location showLocation(); }
Using the applet is simple. We can place the applet into a new page, using the <applet> tag. We specify the name of the class to be executed in the 'code' parameter, and when the page is loaded by a browser, our demonstration can be viewed.