从房间中取出物品并将其添加到库存中

时间:2016-02-20 15:49:35

标签: java text-based adventure

在我的基于文本的冒险游戏中,我目前遇到了一些麻烦。我有一个项目列表(键,手电筒,电池和地图),我已将这些项目放在我创建的Locale数组中的某些房间中。使用命令T,当按下此键时,玩家必须从房间中取出物品并将其添加到他们的库存中,可以通过按下字母I看到。当成功拍摄物品时,应该添加5点他们的分数并从区域设置中删除该项目,以便它不能被采取两次。我需要一些I和T命令的帮助,因为我知道需要做什么,我只是不确定如何执行它。

提前感谢您的帮助!! :)

这是我的主要课程:

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

} //课程结束 这是我的Player类:

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;
        }
    }   

这是我的Locale类:

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;
}
}

1 个答案:

答案 0 :(得分:1)

一种方法是添加move方法,如下所示:

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.");
    }
}

然后你就可以使用它:

            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);
            }