ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
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).
/** Display game info to the player. * @param msg The message to display. */ void display( String msg ); |
/** 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 ); |
/** 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 ); |
if (!in.hasNextInt()) { // We reach this if a non-number was entered. // We call in.next(), which clears that from the input stream. System.out.println( "I don't understand \"" + in.next() + "\"." ); // ...do something?... } else { // We have a number: read it, and... answer = in.nextInt(); // ...check whether 'answer' is a valid index, perhaps printing // a message if it isn't. } |
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
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. */ } |
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:
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.
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.
backITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
©2006, Ian Barland, Radford University Last modified 2006.Dec.09 (Sat) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |