完了する必要があるタスクがいくつかあり、少しガイダンスが必要です。
まず、プレイヤーが現在いる部屋にあるアイテムを取得できるようにする T コマンドが必要です。各部屋のアイテムは、それぞれの Locale 配列にあります。itemPresent を使用すると、player1 の現在のルームに最初に設定されたアイテムが取得され、ルームはゲーム全体で更新されず、代わりに move() メソッド内で更新されます。roomData[2] を使用してアイテムを取得したかったのですが、別のメソッドから変数を取得できないため、これを行うことができません。では、どうすれば現在の部屋からアイテムを取り出すことができるでしょうか?
2 つ目は、アイテムを 2 回取得できないように、一度取得したアイテムを部屋から削除する必要があります。
3 番目に、取得したアイテムを、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;
}
}