1

In my text-based adventure game I am currently having a little trouble. I have a list of items (key, flashlight, battery, and map) and I have placed these items in certain rooms in the Locale array I have created. Using the command T, when this key is pressed, the player must take the item from the room and add it to their inventory which can be seen by pressing the letter I. When an item is successfully taken, it is supposed to add 5 points to their score and remove the item from the Locale so that it can't be taken twice. I need some help with the I and T commands as I know what needs to be done, I'm just not sure how to execute it.

Thank you in advance for all help!! :)

This is my main class:

import java.util.Scanner;

public class GameEngine {

  static Scanner userInput = new Scanner(System.in);

  static String[] playerInfo = {"playerName"};

  static Player player1 = new Player("playerName", null, 0, 0);


 //Welcome Message
 public static void displayIntro(){
    System.out.println("\tWelcome to Power Outage!");
    System.out.println("=================================================");
    System.out.print("\tLet's start by creating your character.\n\tWhat is your name? ");

    //Will read what name is entered and return it
     playerInfo[0]= userInput.nextLine();

    System.out.println("\tHello, " +playerInfo[0]+ ". Let's start the game! \n\tPress any key to begin.");
    System.out.print("=================================================");

    //Will move to next line when key is pressed
    userInput.nextLine();

    System.out.print("\tYou wake up in your bedroom. \n\tThe power has gone out and it is completely dark. \n"
            +"\tYou must find your way to the basement to start the generator. \n\tMove in any direction by typing, "
            + "'N', 'S', 'E', or 'W'.\n\tType 'H' at any time for help and 'Q' "
            + "to quit the game. Good luck!\n\tPress any key.");

    userInput.nextLine();
 }

 //Locations {roomName, description, item}
 static String[][] Locale ={
         {"bedroom","You see the outline of a bed with your childhood stuffed bear on it.","map"},
         {"hallway","A carpeted floor and long pictured walls lie ahead of you.",null},
         {"kitchen","The shining surface of your stove reflects the pale moonlight coming in the window over the sink.","battery"},
         {"bathroom","You find yourself standing in front of a mirror, looking back at yourself.","flashlight"},
         {"living room","You stub your toe on the sofa in the room, almost falling right into the TV.","battery"},
         {"dining room","You bump the china cabinet which holds your expensive dishes and silverware.","key"},
         {"office","The blinking light from the monitor on your desk can be seen in the dark",null},
         {"library","The smell of old books surrounds you.","battery"},
         {"basement","You reach the top of some stairs and upon descending down, you find the large metal generator.",null},     
 };

 //Matrix for rooms
 static int[][] map = {
            //   N,E,S,W
                {6,1,-1,-1}, //Bedroom (room 0)
                {4,2,3,0}, //Hallway (room 1)
                {-1,-1,5,1}, //Kitchen (room 2)
                {1,-1,-1,-1}, //Bathroom (room 3)
                {-1,7,1,-1}, //Living Room (room 4)
                {2,-1,-1,-1}, //Dining Room (room 5)
                {-1,-1,0,-1}, //Office (room 6)
                {8,-1,-1,4}, //Library (room 7)
                {-1,-1,7,-1} //Basement (room 8)
                };

  static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;

  //Items
  private String paperMap = "map";
  private String key = "key";
  private String flashlight = "flashlight";
  private String battery = "battery";


  static int currentLocation = player1.currentRoom;
  static int destination = map[currentLocation][NORTH];

  //take method
  private static String[] itemNames = {"map","key","flashlight","battery"};
  static String itemPresent = Locale[currentLocation][2];

  //move method
  private static String[] dirNames = {"North", "East", "South", "West"};

  private static void move(int dir) {
      int dest = map[currentLocation][dir];
      if (dest >= 0 && dest != 8) {
          System.out.println("=================================================");
          System.out.println("\tYou have moved " + dirNames[dir]);
          currentLocation = dest;
          String[] roomData = Locale[currentLocation];
          System.out.println("\tYou are in the "+roomData[0]+".");
          System.out.println("\t"+roomData[1]);
      } 
      else if (dest == 8){
          System.out.println("\tCongratulations!! You have found the basement and turned on the generator! \n\tYou have won the game!");
            System.out.println("\tTHANKS FOR PLAYING!!!!!!");
            System.out.println("\tCopyright 2016 \n\n");
            stillPlaying = false;
      }
      else {
          System.out.println("\tThere is no exit that way, please try again.");
      }
  }

  //All possible responses to keys pressed
  public static void pressedKey(){
      while(stillPlaying){
      String response = userInput.nextLine();
      if(destination != -1 && player1.currentRoom != 8 && !response.equalsIgnoreCase("Q") ){
          if(response.equalsIgnoreCase("M")){
              //only print if the player has the map
              System.out.println("Here is your map: \n\n\t\t\tbasement\n\noffice\t  living room\tlibrary\n\nbedroom\t   hallway\tkitchen\n\n\t   bathroom \tdining room");
              break;
          }
          else if(response.equalsIgnoreCase("T")){
              // check if there is an item in the player's current room
              if( itemPresent != null){
                  // if there is, then add it to the player's inventory and remove it from the locale
                  System.out.println("You have gained: " +itemPresent+ ". This adds the item to your inventory and you get 5 points added to your score!");
                  //add to inventory--- player1.inventory itemPresent;
                  player1.score = +5;
                  //remove item from the locale  
              }
              else{
                  System.out.println("There is no item to take in this room.");
              }
          }
          else if(response.equalsIgnoreCase("H")){
              System.out.println("To move, type 'N' for North, 'S' for South, 'W' for West, or 'E' for East."
                    +"\nTry to navigate to the basement to turn on the generator."
                    +"\nTake an item from the room you are in by pressing 'T'."
                    +"\nCheck your inventory by pressing 'I'."
                    + "\nYou can type 'Q' at any time to quit the game.\n");
              break;
          }
          else if(response.equalsIgnoreCase("I")){
              System.out.println("These are the items in your inventory: "+player1.inventory+".");
          }
          else if(response.equalsIgnoreCase("N")){
              move(0);
          }
          else if(response.equalsIgnoreCase("E")){
              move(1);
          }
          else if(response.equalsIgnoreCase("S")){
              move(2);
          }
          else if(response.equalsIgnoreCase("W")){
              move(3);
          }
          else{
              System.out.println("\tInvalid command!");
          }

      }//End of if statement
      else if(response.equalsIgnoreCase("Q")){
          System.out.println("Thanks for playing!\n\n");
          stillPlaying = false;
      }

    }//End of while 
  }//End of pressedKey method


static boolean stillPlaying = true; //When this is true, the game continues to run


public static void main(String[] args) {    
    displayIntro();
    System.out.println("=================================================");
    System.out.println("\tYou are in the bedroom.");

    while(stillPlaying){//This makes the game continue to loop

        System.out.println("=================================================");
        System.out.println("\tMove in any direction.");

        pressedKey();           

    } //End of while
} //End of main

} //End of class This is my Player class:

public class Player {
    //import java.util.Scanner;


    //Player class must have name, location, inventory, and score

        private String playerName;
        public String inventory;
        public int score;
        public int currentRoom;

        public Player(String playerName, String inventory, int score, int currentRoom){
            this.playerName = playerName;
            this.inventory = inventory;
            this.score = 0;
            this.currentRoom = currentRoom;
        }
    }   

This is my Locale class:

public class Locale {
//Locale must have name, description, and item


private String roomName;
private String description;
private String item;

public Locale(String roomName, String description, String item){
    this.roomName = roomName;
    this.description = description;
    this.item = item;
}
}
Katie
  • 19
  • 5

1 Answers1

1

One way is to add a move method like this:

private static String[] dirNames = {"North", "East", "South", "West"};

private static void move(int dir) {
    int dest = map[currentLocation][dir];
    if (dest >= 0) {
        System.out.println("You have moved " + dirNames[dir]);
        currentLocation = dest;
        String[] roomData = Locale[currentLocation];
        System.out.println(roomData[0]);
        System.out.println(roomData[1]);
    } else {
        System.out.println("There is no exit that way, please try again.");
    }
}

Then you can use it like:

            else if (response.equalsIgnoreCase("N")) {
                move(0);
            } else if (response.equalsIgnoreCase("E")) {
                move(1);
            } else if (response.equalsIgnoreCase("S")) {
                move(2);
            } else if (response.equalsIgnoreCase("W")) {
                move(3);
            }
Gregory Bush
  • 326
  • 1
  • 6