RU beehive logo ITEC dept promo banner
ITEC 120
2012fall
dbraffitt
ibarland

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

hw10
gRUe

Due Dec.07 (Fri) 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.

  1. Make a new project(directory), which includes class Explorer and class Treasure. You can make new files and copy/paste from the old ones, or (if using BlueJ) you can use Edit > Add Class from File….
  2. Explorers carry Treasures

    1. Every Explorer has a knapsack, which is an array of Treasures. (In the example transcript above, the knapsack size was 3, but changing the knapsack to hold 97 items should just require just one change.)
      Add a field accordingly.
    2. Modify the constructor so that a brand-new Explorer's knapsack is initially full of pieces of lint.
    3. Add a method inventory, which sets an explorer's status to be “You are carrying” followed by a string representing all the Treasures in the knapsack.
    4. Modify Explorer's handleOneCommand to allow for handling a user typing “inv” (or “inventory”).
  3. Rooms


    Make a class Room:
    1. Read the transcript above, to see what sort of information each different room needs to contain, and make fields appropriately.
      For now: Do not deal with Rooms being connected to other Rooms. That will be left for extra credit.
    2. Include a method which returns a description of the room.
      hint: Both Rooms and Explorers need to get a string that represents an array of Treasures. You won't repeat code1, of course; instead you will have a method to do this one sub-task. Which of your three different classes should best hold a method that takes in an array of Treasure, and returns a String description?
    3. Write a method createAStartingRoom(), which just returns one particular room (which in turn contains at least three specific Treasures).
      We will call this method later, to create the very-first-room for an Explorer to start in. (To think about: Should this method be static? Why or why not?)
  4. Every Explorer has a current-room:

    1. We'll go back to our Explorer class, and add a field to reflect this.
    2. Have the Explorer constructor initially set a new Explorer's room to the result of calling createAStartingRoom.
    3. Update the Explorer's method look, so that it returns a description of the explorer's current room.
    4. Write an Explorer method String swapItems(int knapsackIndex, int roomItemIndex ), which swaps the contents of an explorer's knapsack and one of the items in their current room. It returns a string describing the swap that just happened.
    5. Write an Explorer method swap(), which prompts the user for item-numbers, and then effects the swapItems. (You may presume that the user enters valid item-numbers, as in the transcript above.)
      Note:This method supercedes the “grab” and “drop” methods from hw07's class Explorer. You will want to update the handleOneCommand accordingly.

      Note how between this method and the previous, we are de-coupling input/output (I/O) from actually updating the This is important if we ever (say) have an Explorer controlled by an AI — they still need to swapItems, but they never prompt for input. We have separated the model (objects) of the game from the controller (I/O) of the game.

I should be able to run your program by typing java Explorer from the command line.


  1. Extra Credit:

    Connecting Rooms

    Handling the connection of rooms is slightly tricky. 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.

What next?

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


1 After making this task its own separate helper method, be sure to remove any now-redundant old copies of this code from Explorer, if you had put it there for step (Ic) above.      

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


©2012, Ian Barland, Radford University
Last modified 2012.Dec.12 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme