|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
Due Apr.16 (Mon)18 (Wed) 17:00
(but accepted by start of class Thu, due to the tragedy at Tech)
(1) a small nugget -- Upon close inspection, the nugget glints of gold! (2) a fancy pen -- This pen can write in purple, green, and plaid. (3) chocolate egg -- The size of an ostrich egg, but made entirely of dark chocolate. |
We will write a class TextIO, which will help communicate between the gRUe world, and somebody playing the game.
lack of automated tests:: Because there is no return value, there is nothing to assertEquals, in unit tests, so you don't need to turn in any unit tests for TextIO methods. (However, it's possible to still make unit tests which read input from a fixed String, and you can inspect the terminal window manually.)
/** Display game info to the player. * @param msg The message to display. */ void display( String msg ) |
/** Print a message to the screen, and get a response. * @param msg A message to print to the screen, * for example "What month is your birthday? ". * @return The response -- just one word, which is taken * from this TextIO's internal Scanner. (If that Scanner happens * to be reading from System.in, then this function is getting * an answer interactively.) */ 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 Treasures to have the user choose 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.) * The input is taken from the scanner. */ int select( String instruction, LinkedList<Treasure> items ) |
if (!in.hasNextInt()) { // We reach this if a non-number was entered. // We call in.next(), which clears that non-number 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. */ } else if (actionToDo.equals(…) |
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.
For fun: Later, we will provide a class GuiIO, which has exactly these same methods, but gets its answers in different ways. But your gRUe program should run equally well if you use the GuiIO in place of the TextIO. That is why your explore method doesn't interact directly with the keyboard or screen: I can write an entirely different interface, which can interact entirely with your Explorer methods!
1 It's equally easy to even have a file with all the input text, and make a TextIO which reads from that java.io.File. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |