Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs

hw07:
gRUe, pt. III

Due Dec.01 (Fri)02 (Sat) 17:00, on WebCT. (Turn in printout on Monday.)
Still, work on it before then, as you get a chance.

2006.Dec.02 (Sat) 16:00: I changed the ordering of some of the problems, and flat-out removed some of the commentary. I suspect these were adding to some people's confusion. I will accept the homework up to Monday 17:00 without late penalty. However, hw07 won't be accepted more than 3 days after the Saturday deadline (that is, not after Tues 17:00).

  1. Copy your hw06 to a new project, to work on this homework. (Or, you may also use the posted hw06 solution (.jar); be sure to modify the comments in each file to include your name, as well as cite the original author, briefly mentioning which author wrote what.)
  2. Add interface Describable to your project, and adjust your Treasure and Room classes, so that they implement Describable. (See lecture notes/book, on implementing an interface.) Of course, your test method already includes calls to getName and getDescription.
  3. Write a class TextIO which has the following three methods:
    1.   /** Display game info to the player.
         * @param  msg	The message to display.
         */
        void display( String msg );
      
    2.   /** Print a message to the user, and have them enter a response.
         * @param msg A message to prompt the user with.
         *    or a space, e.g. "What month is your birthday? ".
         * @return The user's response -- just one word.
         */
        String prompt( String msg );
      
    3.   /** Have the user select an item from a list.
         * @param instruction A message describing what is being selected, e.g. "Choose your favorite treasure:"
         * @param items The list of items to get a choice from.  The list will not be modified.
         * @return an index into items, or the sentinel value -1, for no-choice-made.
         *    (The -1 might indicate that the user entered an invalid choice, or 
         *    maybe the changed their mind and cancelled the selecting.)
         */
        int select( String instruction, java.util.ArrayList<? extends Describable> items );
      
      The syntax “? extends Describable” is admittedly very odd; it means “everything in the list is Describable; moreover, everything in the list is of the same (unspecified, more specific) type”.1 Ian — where is a good place to put the static describablesToString, so that it's not specific to TextIO? Really, Describable should have been an abstract class from the outset, never an interface?
    Some things to consider/remember:
  4. Create an interface named IO (for “input/output”), which just declares the three methods above. Remember that an interface contains only method signatures — no actual code. 90% of the interface will be made by copy/pasting the above specs. Make sure TextIO implements IO.
  5. Now for the fun part: We want to have the user give commands interactively. Write a method which prompts the player for a String, and runs an appropriate command. It should do this repeatedly, until the user types quit.

    More precisely: write a method explore inside of class Explorer. This method will

    1. declare a variable of type IO , and initialize it to a new TextIO.
    2. Repeatedly:
      • prompt for a command, and
      • call the appropriate function. (It may need to first have the user select a particular Treasure from the current Room, in the case of “grab”.) Your loop will presumably include code along the lines of:
          if (actionToDo.equals("look")) {
            myIO.display( this.look() );
            }
          else if (actionToDo.equals("grab")) {
            /* ... now have the user select one of the treasures in the
             * room, and then call  this.grab(...), passing it
             * the selected treasure.
             */
            }
        
        (You don't need to follow the above code precisely, but it gives you a flavor of how to do it. The variables used in this snippet are only suggestive names; you'll need to figure out exactly what types of variables you need, and how to get values for them.)

    Once you get your program to accept input like “look” and “grab”, extend your if-else-if statement to let the user type in other commands: “drop-left”, “drop-right”, “inventory”. You might want to add a command “help”, which prints out all the (other) valid commands. Note that the similarity between what the user types (“look”) and the name of your method “look” is quasi-coincidental; in general they don't have to be the same at all.

    After getting this method working, you can have your friends play your gRUe program, just reading the console window and typing text into that window. (They won't type anything into BlueJ's code pad.) The game will look similar to the sample dialogues from on previous homeworks. Admittedly, the game won't be very interesting, until we add connections between Rooms

    This method should only communicate with the user via methods in IO; this method must not call System.out.println directly.

We have quite a few classes going on, in this homework. Be sure to be thinking/remembering, which methods go with which classes. (I find it helpful to frequently refer to a class's "interface" view, and/or use the BlueJ option “Build Project Documentation”.) Here is my class diagram from my solution:

class diagram for gRUe pt. III

Note that BlueJ tried to add other lines, which I deleted by right-clicking. It was my test cases that confused BlueJ: Just because my test method for (say) TextIO happened to make a Treasure, it doesn't mean that in general TextIO has-a Treasure. We'll see, in the final lab, a better way to write test cases, which will coincidentally not confuse BlueJ.

For this assignment, be sure to use the names in the interfaces exactly as specified. (I should be able to replace TextIO with any other class implementing IO, and your code should still run just fine.)

For fun: For your program, you'll submit a class TextIO. However, for fun, you can use a different class implementing IO: SimpleGuiIO.java. Once you put this file inside your own program, you'll be able to run your program via pop-up windows — just replace new TextIO() with new SimpleGuiIO().

Furthermore: once your program doesn't rely on printing to the console, you can make it a stand-alone application (you don't need to run it from inside BlueJ). See lab14 for information on saving your BlueJ project as a (standalone) jar file.


1

If you are getting a Java compiler error message about “incompatible types” error, it may be because you are passing a method (say) an ArrayList<Treasure> where it expected an ArrayList<Describable>. The difference between ArrayList<Describable> and ArrayList<? extends Describable> is that in the second case, it's clear that you can't add a Room to the list, where as in the former case this would be allowed.

If you are getting a warning about “potentially unsafe operation”, this is probably okay but come by office hours to have me check it out.

     back

ITEC120-ibarland (incl. office hrs)infolectureslabshwsjava.lang docsjava.util docs


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