|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
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).
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
Clearly, another useful helper method
would be one which takes in an array
of
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
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 “
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; } |
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 ); |
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.)
This is a very open-ended assignment. Some possible extensions (in approximate order of complexity):
Add a weight limit, to how much an explorer can carry.
Right now a
One approach to implementing this would be having a
have a “parallel array” of (say, four) Strings
next to your array of four adjacent
Have
(One approach might be a single huge
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
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2010, Ian Barland, Radford University Last modified 2010.Dec.11 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |