I'm writing an adventure game composed of 4 classes. Typical beginning java assignment. You are a player moving through a series of rooms and you can pick up and describe objects, etc... The details are not so important here but the issue bothering me are the behavior of the game class pasted below. The code is a bit cumbersome and not very eloquent. I apologize as I am not by any stretch a competent programmer. I would appreciate some design suggestions because I have *4* major actions that have to occur; the first being the
instantiation of
new room objects (I have subset the original set to 4 rooms for simplicity); the second is to
instantiate new
items held in an ArrayList and add them to rooms; the third thing is to
set the orientation of the rooms (which room is north, east, south, and west of the current room); and the fourth thing is
move the player through the room picking up and discarding objects along the way. Just want to see if someone has suggests for layout and design.
My biggest problem now is that I can't figure out how to continue the program once a new room is initialized from the user input. This program basically quits without printing the text specified in the bottom("You are currently in (the room that was selected);" It only prints the name of the room selected and then quits (because code is missing of course).
I would love for someone to show me how to take the repetitive actions like prompting the user options and place these in a separate method (there are some design problems that prevent me from doing this right now) so that when a new room is selected I don't have to repeat the if/else if. More importantly I want to get this so that when a room is selected, the program doesn't simply knock the player out with prompting. I don't have the next set of questions set up yet, but I can already tell that the program is not going to work as I have coded it. Any suggestion would be better than nothing including whether I should use a switch case rather than an if/else if for the questions. Thanks in advance. The focus is on the Game class because I know that the other classes are behaving as expected. I included the room class at bottom but am not having issues with that. I apologize for the detail.
the adventure game class (I launch from main method but not important to include)
public class Game
{
public void playGame(){
//set the room names and their descriptions.
Room r1 = new Room(); //instantiates new room r1
r1.setName("Hallway"); //sets r1's name to Hallway
r1.setDescription("Long and dark. There are exits to the north, east and west");
Room r2 = new Room(); //new room2, etc...
r2.setName("Bathroom");
r2.setDescription("Bathroom stinks. There are no doors other than the entry");
Room r3 = new Room();
r3.setName("Kitchen");
r3.setDescription("This is where the food is cooked");
Room r4 = new Room();
r4.setName("Master Bedroom");
r4.setDescription("This is where the Master Sleeps");
//instantiates new Item objects and sets the name and description
Item i1 = new Item();
i1.setName("Keys");
i1.setDescription("A set of gold keys opening an unknown doorway");
Item i2 = new Item();
i2.setName("Toothbrush");
i2.setDescription("An electric toothbrush. Good for fighting caveties");
Item i3 = new Item();
i3.setName("Deodorant");
i3.setDescription("Tom's of Maine all Natural Deodorant");
//setup the orientation of rooms relative to r1
r1.setEast(r2);r1.setWest(r4);r1.setNorth(r3);
// this adds the items to each room
r1.addItem(i1);r2.addItem(i2);r2.addItem(i3);r3.addItem(i4)
System.out.println("Welcome to my silly game");
Creature player1 = new Creature();
Scanner ms = new Scanner(System.in);
System.out.println("What is your name?\n");
player1.setCreatureName(ms.next());
System.out.printf("Welcome %s\n", player1.getCreatureName());
System.out.println();
System.out.printf("You are currently in the %s", r1.getName(),".\n");
System.out.println();
System.out.println("There are exits on the north end of the\n" +
"hallway and midway on both sides\n");
System.out.println("To move you can type the following: \n"+
"(n) to move north\n" +
"(e) to move east\n" +
"(w) to move west\n" +
"(s) to move south\n");
player1.setCurrentRoom(r1);
Room current = player1.getCurrentRoom();
System.out.println("Where would like to go? Your choices are east, west, and north.\n" +
"Enter e, n, or w to move. To display objects in room type d or to display personal\n" +
"objects type p");
Scanner ns = new Scanner(System.in);
String input = ns.next();
Room nextRoom = null;
ArrayList displayObject = null;
ArrayList x = current.getItems();
ArrayList y = player1.getItems();
if (input.equals("e")) {
nextRoom = current.getEast();
}
else if (input.equals("n")) {
nextRoom = current.getNorth();
}
else if (input.equals("w")) {
nextRoom = current.getWest();
}
else if (input.equals("d")) {
if (x.isEmpty())
System.out.println("There are no items in this room");
else if (x!=null)
displayObject = current.getItems();
}
else if (input.equals("p")) {
if (y.isEmpty())
System.out.println("You have no items presently");
else if (y!=null)
displayObject = player1.getItems();
}
else if (input.equals("")) {
System.out.println("Illegal Move");
}
player1.setCurrentRoom(nextRoom);
System.out.printf("You are currently in %s", current.getName()); //this is not printing. I only get the name and description printed
}}
The Room class. This sets up the rooms and their orientation.
public class Room extends Holder{
private Room north;
private Room south;
private Room east;
private Room west;
public Room getNorth() { return north; }
public Room getSouth() { return south; }
public Room getEast() { return east; }
public Room getWest() { return west; }
public void setNorth(Room dir) { north = dir; }
public void setSouth(Room dir) { south = dir; }
public void setEast(Room dir) { east = dir; }
public void setWest(Room dir) { west = dir; }}