文字冒险游戏:Take and Inventory

时间:2016-02-23 20:19:20

标签: java adventure

我需要完成几项任务,我需要一些指导。

首先,我必须使用T命令,允许玩家获取他们所在的当前房间中的项目。每个房间的项目都在各自的Locale阵列中。通过使用itemPresent,它将获取player1当前房间最初设置的项目,并且整个游戏中的房间不会更新,而是在move()方法中更新。我想使用roomData [2]以这种方式获取项目,但由于我无法从另一种方法中获取变量,因此我无法执行此操作。那么,我怎么能从当前房间拿走这件物品呢?

其次,我需要在拍摄后将物品从房间中取出,以便物品不能被拍两次。

第三,我必须将此项目添加到我尝试使用ArrayList设置的玩家库存中。我不确定如何设置代码,以便我可以获取刚刚找到的项目并将其添加到库存中。

感谢您的帮助!!

这是我的主要课程:

import java.util.Scanner;
import java.util.ArrayList;



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[][] roomMap = {
            //   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)
                };

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


  static int currentLocation = player1.currentRoom;

  //take method
    //String[] currentInventory = {};
    // player1.inventory = currentInventory;
  /*
    private static int[] itemNames = {"map","key","flashlight","battery"};
    for(int i=0; i<itemNames.length; i++){
        if(itemNames[i].equals("map")){

        }
    }
    */

  //inventory
  ArrayList<String> inventory = new ArrayList<String>();

  //static String[] itemPresent = roomData[2];


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

  private static void move(int dir) {
      int dest = roomMap[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]);
          String itemPresent = roomData[2];
      } 
      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(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);
              break;
          }
          else if(response.equalsIgnoreCase("E")){
              move(1);
              break;
          }
          else if(response.equalsIgnoreCase("S")){
              move(2);
              break;
          }
          else if(response.equalsIgnoreCase("W")){
              move(3);
              break;
          }
          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

这是我的Player类

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


    //Player class must have name, location, inventory, and score
    //Instead of array of descriptions, use array of Locale objects

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

0 个答案:

没有答案
相关问题