[ Home | Suzanne Menzel | Sun Java Docs | 2003 Session 1 | DrJava ]
In this session we will demo the DrJava programming environment and cover some of the Collection classes in the context of an adventure game. We will also discuss a few teaching stategies and experiment with the Classroom Participation System (CPS) from eInstruction.
Source Files: [ Heart | Adventurer | Room | BackPack | World | Util | glass.au | CPS Lesson ]
We'll use the latest development release of DrJava as our development environment.
To describe and motivate the notion of a text-based adventure game, play StrongBad's game. Zork I, II and III are freely available at the Infocom home page. Here's information about how to play other Infocom games cheap, including my personal favorite: Hitchhiker's Guide to the Galaxy. For those of you who want to try out a text adventure game without having to bother downloading one, look no farther than emacs. If you type M-x dunnet (or ESC x dunnet), you'll start the game built into the program.
The first thing to do is to devise a map of the rooms in your world and their interconnections. We imagine that the exits in the room are labeled with some "direction" string. This could be something like "north" or "southeast" or something completely unexpected like "dennis".
The adventurer will be modeled by an Adventurer class. When we create an Adventurer we will, at the same time, create his BackPack. Ask yourself which of the following statements makes more sense:
When we create an Adventurer, he must be placed in some Room. We have no need to create Adventurers whose location is unspecified.
Our goal is to implement the following hierarchy of classes:
Especially useful Collection methods are size, isEmpty, add, remove, contains, and iterator.
The Iterator methods are hasNext, next, and remove.
Classes in java.util that implement the Collection interface are ArrayList, Vector, Stack, HashSet. Because the contents of our BackPack and each Room is unordered and without duplicates, we choose to represent them with a HashSet.
Argueably the worst design decision of all time occured when some programmer opted to implement Stack as an extension of Vector. Why was this the wrong choice and what should have been done instead?
HashMap is a data structure that represents an unordered sequence of associations, i.e., key-value pairs.
> pairs = new HashMap(); > pairs instanceof Collection false > pairs.isEmpty() true > pairs.put("lucy", "ethel"); > pairs.put("pat", "vanna") null > pairs.put("lucy", "ricky") "ethel" > pairs.get("pat") "vanna" > pairs.get("vanna") null
HashMap is an ideal data structure for representing the exit information associated with each Room.
CPS is a "poll the audience" type system that allows students to respond anonymously to multiple-choice questions and then provides immediate feedback, giving the correct answer and the percentages of responses to each option. This system is manufactured by eInstruction and was featured in a recent NY Times article.
> deadEnd = new Room("You are at a dead end of a dirt road."); > dungeon = new Dungeon(); > parapets = new Parapets(); > dungeon.getDescription() "You're in a cold, dark dungeon." > parapets.setExit("south", dungeon); > dungeon.setExit("dennis", new Dennis()); > dungeon instanceof HashSet true > dungeon.contents() "You see flask, scroll." > dungeon.getExit("dennis").contents() "The room is empty."
> raja = new Adventurer(dungeon); > raja.getStrength() 10 > raja.show() > raja.dance() "You shake and shimmy. It feels good."
> castle = new World(); > raja = castle.getAdventurer(); > raja.getLocation() == dungeon true
It takes a great deal of effort to be a good teacher. Often it helps to do something unusual, atypical, or memorable. Props are always good.
For a discussion of the benefits of using atypical classroom techniques in a computer science class, read Mixed Nuts by Sid Stamm or visit (and contribute to) his Atypical Techniques online repository.