In: Computer Science
JAVA
1) You are provided an implementation of a text-based adventure game. Currently the game responds to directional movements that allow you to move your adventurer around a maze (that is read from a text file). Add the ability to save (and load) the game using object serialization. Write the serialization of the file out to a default filename SavedGame.dat. Make sure that you take care of any exceptions that may occur when reading or writing to a file (for example: your program should not crash if a save file does not exist!). Errors should be written to the standard error output stream.
You should begin by diving into the provided implementation and getting a feel for how the game works. Start in Game.java and work your way down. Once you’ve compiled the code, you can run the game by typing in your shell: cat d1.txt - | java AdventureGame
Type w, s, a, or d to move around. You can optionally pass in the name of a systemSetting String (see ConfigFactory.java for more details) to get a fullycolored version of the game. To add the ability to save and load, you should add a new Action enum type corresponding to SAVE and LOAD. Then follow the logic of the application to determine where best to add your serialization features. The Dungeon class should be updated to implement Serializable and your Game class should be able to update its dungeon variable when a new game is loaded. It is generally good program design to separate the user interface (UI) from the model (the classes that “do the computation”) as much as possible. You’ll note that this design rule has been implemented in the starter code. The thing you should be serializing here is whatever the object / objects are that contain a representation of the “state” of the program. The UI, if designed properly, should not have state that effects the logic of the game. Given that, what should you serialize that contains EVERYTHING relating to the state of the game??
(BONUS) – When the user chooses to save or load, allow the user to specify the filename of the save file. Exception handling is also expected, and this code should enforce that the filename ends in “.dat”. (15 points)
AdventureGame.java
package AdventureGame;
import java.io.*;
public class AdventureGame {
private Adventure theCave;
private Player thePlayer;
private int convertDirection(String input){
char d = input.charAt(0);
int theDirection = 9999;
switch(d){
case 'n': case 'N': theDirection = 0;break;
case 's': case 'S': theDirection = 1;break;
case 'e': case 'E': theDirection = 2;break;
case 'w': case 'W': theDirection = 3;break;
case 'u': case 'U': theDirection = 4;break;
case 'd': case 'D': theDirection = 5;break;
}
return theDirection;
}
/** choosePickupItem determines the specific item
that a player wants to pick up. */
private Item choosePickupItem(Player p, BufferedReader keyB)
throws IOException{
Item[] contentsArray = (p.getLoc()).getRoomContents();
String inputString = "prepare";
int theChoice = -1;
do {
System.out.println("The room has:");
for (int i = 0; i < =contentsArray.length ; i++)
System.out.println((i+1) + ": "
+contentsArray[i].getDesc());
System.out.print("Enter the number of the item to catch: ");
inputString = keyB.readLine();
System.out.println('\n');
if (inputString.equals("")) inputString = " ";
try {
theChoice = Integer.parseInt(inputString);
} catch (NumberFormatException e) {
System.out.println("Invalid input.");
theChoice = -1;
}
if (theChoice < 0 || theChoice > contentsArray.length)
System.out.print("This item is not in the room.");
} while (theChoice > contentsArray.length || theChoice < 0
);
return contentsArray[theChoice-1];
}
/** chooseDropItem determines the specific item
that a player wants to drop */
private int chooseDropItem(Player p, BufferedReader keyB)
throws IOException{
String inputString = "prepare";
int theChoice = -1;
do {
System.out.println("You are carrying: " +
p.showMyThings() + '\n');
System.out.print("Enter the number of the item to leave: " );
inputString = keyB.readLine();
try {theChoice = Integer.parseInt(inputString);}
catch (NumberFormatException e) {
System.out.println("Invalid input.");
theChoice = -1;
}
if (theChoice > p.numItemsCarried() || theChoice < 0 )
System.out.print("Wrong choice.");
} while (theChoice < 0 || theChoice >
p.numItemsCarried());
return theChoice;
}
public void startQuest() throws IOException{
Player thePlayer = new Player();
Adventure theCave = new Adventure();
Room startRm = theCave.createAdventure();
thePlayer.setRoom(startRm);
/** Create the keyboard to control the game; we only need one
*/
BufferedReader keyboard
= new BufferedReader(new InputStreamReader(System.in));
String inputString = "prepare";
/* The main query user, get command, interpret, execute cycle.
*/
while (inputString.charAt(0)!='q') {
System.out.println(thePlayer.look());
System.out.println("You are carrying: " +
thePlayer.showMyThings() + '\n');
/* get next move */
int direction = 9;
System.out.println("Which way (n,s,e,w,u,d)," +
" or catch (g) or toss (t) an object," + 155 " or close
(q)?" + '\n');
inputString = keyboard.readLine();
System.out.println('\n');
if (inputString.equals("")) inputString = " ";
char key = inputString.charAt(0);
switch (key){ 161 // Go
case 'n': case 'N': case 's': case 'S':
case 'e': case 'E': case 'w': case 'W':
case 'u': case 'U': case 'd': case 'D':
direction = convertDirection(inputString);
thePlayer.go(direction);
break;
// Grab Item
case 'g': case 'G':
if (thePlayer.handsFull())
System.out.println("Your hands are full.");
else if ((thePlayer.getLoc()).roomEmpty())
System.out.println("The room is empty.");
else {
Item itemToGrab =
choosePickupItem(thePlayer,keyboard);
thePlayer.pickUp(itemToGrab);
(thePlayer.getLoc()).removeItem(itemToGrab);
}
break;
// Drop Item
case 't': case 'T':
if (thePlayer.handsEmpty())
System.out.println("You have nothing to drop.");
else {
int itemToToss =
chooseDropItem(thePlayer,keyboard);
thePlayer.drop(itemToToss);
}
}
}
}
public static void main(String args[])
throws IOException{
System.out.println("Welcome to the Adventure Game,\n" );
AdventureGame theGame = new AdventureGame();
theGame.startQuest();
}
}