RU beehive logo ITEC dept promo banner
ITEC 120
2010fall
ibarland

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs

hw10
gRUe

Due Dec.09 (Thu) at the start of class

When playing the Grand Role-playing Underground Expedition (“gRUe”), an explorer wanders around different rooms, gathering treasures. An explorer can carry three treasures in their backpack; if they ever grab one treasure they will have to drop some other treasure. (Initially, an explorer's treasures are just three pieces of lint. The growth potential is huge!) A game session might look something like this:

Welcome to the world of gRUe.

Davis 225 lab:
This room is dark, the only light coming from an eerie screen saver.
Lying on the ground, there is:
[1] some caffeinated mints (0.2 lbs).
[2] a thumb drive (0.1 lbs)
[3] a 72" golden iMac (45.0 lbs)
[4] a whiteboard marker (0.2 lbs)


>> What do you wish to do? help

Please type one of the following commands:
  look
  inventory
  swap
  go
  help
  quit
  

>> What do you wish to do? look

Davis 225 lab:
This room is dark, the only light coming from an eerie screen saver.
Lying on the ground, there is:
[1] some caffeinated mints (0.2 lbs).
[2] a thumb drive (0.1 lbs)
[3] a 72" golden iMac (45.0 lbs)
[4] a whiteboard marker (0.2 lbs)


>> What do you wish to do? inventory

You are carrying:
[1] lint (0.0 lbs)
[2] lint (0.0 lbs)
[3] lint (0.0 lbs)


>> What do you wish to do? swap

You are carrying:
[1] lint (0.0 lbs)
[2] lint (0.0 lbs)
[3] lint (0.0 lbs)
What item do you wish to drop? 2
Lying on the ground, there is:
[1] some caffeinated mints (0.2 lbs).
[2] a thumb drive (0.1 lbs)
[3] a 72" golden iMac (45.0 lbs)
[4] a whiteboard marker (0.2 lbs)
What item do you wish to grab? 1

You drop lint (0.0 lbs) -- a ball of fuzz.
You grab some caffeinated mints (0.2 lbs) -- they smell like peppermint coffee.


>> What do you wish to do? inventory

You are carrying:
[1] lint (0.0 lbs)
[2] some caffeinated mints (0.2 lbs)
[3] lint (0.0 lbs)


>> What do you wish to do? look

Davis 225 lab:
This room is dark, the only light coming from an eerie screen saver.
Lying on the ground, there is:
[1] lint (0.0 lbs)
[2] a thumb drive (0.1 lbs)
[3] a 72" golden iMac (45.0 lbs)
[4] a whiteboard marker (0.2 lbs)


>> What do you wish to do? go

The adjacent rooms are:
[1] Davis 2nd floor hallway
[2] sidewalk between Davis and Stuart
Which room would you like to go to? 2

sidewalk between Davis and Stuart:
The sidewalk is covered in leaves.
Lying on the ground, there is:
[1] A scrap of paper.


>> What do you wish to do? quip

I'm sorry, I don't know how to quip.

What do you wish to do? quit

Bye!

Read all the instructions below before starting; re-read them periodically as you are writing your code.

Your program must contain at least four different rooms, and must have at least five different treasures. Your program should have classes and methods as appropriate; I should be able to run your program by typing java Explorer from the command line.

Start by just getting inventory working: that is, have an explorer contains some treasures. Then, I recommend that by Saturday evening, you have a program which has an explorer in a single room, able to do all of the commands except “swap” and “go”, and by Sunday evening having “swap” working. That leaves you with a couple of days to add several rooms, connect them, and get “go” fully working (with chances for office hours, if a snag is hit).

Overall design

As always: start by figuring out the data definitions. What classes do you want to have, for this program? For each class, what fields will it need? Define classes and constructors, and make some examples of the data before proceeding. After that, think about what methods you are likely to need, and create their signatures.

For example, your class Treasure from hw04 is a start (although we wrote that back when every method was static). Where in the above transcript is the text coming from toPrettyString? Will you also need a similar-but-more-thorough method to print more detailed information? When will you be calling that?
Clearly, another useful helper method would be one which takes in an array of Treasures, and returns a String of numbered descriptions. (Which actions will make use of this method?)

Although our explorer has only three items in their backpack, you must be able to change this capacity to 173 items just by changing a few characters of your code!

Hint: Have each of the explorer's possible actions correspond to a method. Moreover, have that method return a String describing the result1. For instance, when a user types “look”, you will call a method which ends up returning the current room's description. Similarly, you'll have a method for swap, which will return a string describing the dropped and grabbed items. You can test these methods alone, without needing the interactive loop.

Hint: When asking for the user to choose an item from an array, I recommend a helper method which takes in the array, and returns the (user-selected) index into that array. (That is, return the index, not the chosen item itself.)

Although the example transcript is just the results of print statements, don't be misled: My solution contains the words “System.out.println” no more than four2 times.

Connecting Rooms

Handling the connection of rooms is slightly tricky; I recommend doing this after you have the code working for picking up and dropping objects in the explorer's current room.

I recommand creating room-connections in two passes: First create many rooms, but don't (yet) initialize the field for its neighbors. Then, once all your rooms are mostly-initialized, you can add connections. Something along the lines of:

  /** Create all the rooms in the game, connect them, and return
   * the particular room that a player will start in.
   * This method should be called exactly once, at the very start of the game.
   */
  static Room setUpWorld() {
    Room rm1 = new Room (  );  // rm1's "neighbors" field not yet initialized.
    Room rm2 = new Room (  );
    Room rm3 = new Room (  );
    Room rm4 = new Room (  );
    Room rm5 = new Room (  );

    Room[] neighborsOfRm1 = { rm4, rm2, rm5 };
    r1.neighbors = neighborsOfRm1;
    

    return r1;
  }
Of course, connections don't have to be symmetric (you can jump out the window from Da225 to the Stuart/Davis sidewalk, but once on the sidewalk you can't move directly back), and a room might even lead right back to itself.

Sharing your Treasures and Rooms

This game only gets interesting if there are many rooms to explore, and many treasures to find. If several people request it, I will set up a wiki page, where you all can write down constructor-calls for Treasures in a way which is easy for others to paste into their programs. For example,

Treasure chocEgg = 
  new Treasure( "chocolate egg",
                "The size of an ostrich egg, but made entirely of dark chocolate.",
                35.0,
                "", // URL
                "Ian Barland" // creator
                );
Room Davis225 = 
  new Room( "Davis 225 lab",
            "This room is dark, the only light coming from an eerie screen saver.",
            "Ian Barland" // creator
             );
  
(Of course, you might need to tweak what is written on the wiki to make it match exactly what your own code requires.)

If you add or take anything from such a page, you must include a creator. Your own program must then include a creator field, for rooms and treasures. (You can overload the constructor, to maintain compatability with code you've already written.)

What next?

This is a very open-ended assignment. Some possible extensions (in approximate order of complexity):


1This is admittedly a violation of “a method should change state or return a value, but not both”. in this case, the primary purpose a method corresponding to “go” or “swap” is to change state; the returned value is basically a token confirmation of what has changed.      

2 Two uses were in almost identical code that can't quite be factored out because one time it deals with Treasure[], and another time it deals with Room[], but it's otherwise identical. We'll see in the last couple of lectures, how that can actually be simplified.      

homeinfolectslabsexamshws
tutor/PIsbreeze (snow day)
Object120 + its docsjava.lang docsjava.util docs


©2010, Ian Barland, Radford University
Last modified 2010.Dec.11 (Sat)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme