CS 116 Lab 05

Inlab Overview

No new concepts this time around — instead, you'll work on integrating your IF engine with the monster combat system. In doing this, you should focus on reusing as much of your code as possible, ideally without copying and pasting between files (i.e. instead, you should simply import the packages/classes you need).

You should modify the room datafile so as to specify, on a room by room basis, what monsters may be encountered, and either the likelihood of their spawning or a cap on how many might spawn at a given location (or both).

Below we show you one possible way of structuring your datafile. Note that not all rooms have monsters:

5
.
1
Treasure room
a huge room, filled with gold coins and gems of all sorts imaginable. 
In the corner of the room there is a dragon chained to a radiator.
,
N 2 a small door
.
2
Cubby
a tiny cubby. There's nothing of consequence here.
,
N 3 a looming entrance to a cave
S 1 a small door
E 4 a wardrobe
,
Pterodactyl
0.5
.
3
Cavern
an enormous, pitch-black cavern. You can't see anything, but you hear a
distinct rumbling from the far side. It's probably a bad idea to stick 
around to find out what's producing it.
,
S 2 the cave entrance
,
Vampire Pterodactyl
0.3
.
4
Forest
a lush forest. The wardrobe clearly functions as a magical gateway 
between worlds. A narrow path through the forest stretches before you.
,
W 2 a shimmering wall
E 5 the forest path
.
5
Middle of the forest
a densely forested passage. It stretches out interminably before you.
,
W 4 the way back
E 5 a meandering path
.

Feel free to invent your own modifications.

If and when the player's character dies in battle, battle statistics should be printed out, just as at the end of our original monster game.

Scoring

  • 10 points for a functional implementation
  • 5 points for code reuse — your original Monster class and associated subclasses, at minimum, should not have required any modification

Addendum: Class and newInstance

In this lab you'll need to create objects (Monsters) based on class names stored in strings (which, in turn, are read from a file). You can certainly use if-else statements to do this, but a more elegant way is to use the Class class to load classes based on strings, then dynamically create instances. The following code illustrates the basic concept:

public static void main(String[] args) {
    try {
        String monsterClassName = "Vampire";
        String packagePrefix    = "edu.iit.cs.cs116.lab04.";
        Class monsterClass      = Class.forName(packagePrefix + monsterClassName);
        Monster monster         = (Monster)monsterClass.newInstance();
        System.out.println(monster.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}