我为多余的代码表示歉意,但是我完全迷失了我的问题所在。我有四节课,应该有一个玩老式迷宫游戏的程序。您从一个房间开始,应该能够移动房间,直到带着圣杯进入房间。因此,在程序中,控制器类创建房间。它包含一串房间详细信息。我可以确认该字符串已正确解析。我的问题是相邻的房间似乎没有固定好。我感觉这些房间都存储在播放器类中,但是当我尝试访问相邻的房间时,它们都设置为0。
Room.java
public class Room {
public static int DIRECTONS = 4;
public static int NORTH = 0;
public static int EAST = 1;
public static int SOUTH = 2;
public static int WEST = 3;
private boolean hasGrail;
private String description;
private Room adjacentRoom;
private static Room[] roomNeighbors = new Room[4];
public Room() {
description = "";
hasGrail = false;
}
public Room(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setAdjacent(int direction, Room adjacentRoom) {
this.adjacentRoom = adjacentRoom;
roomNeighbors[direction] = adjacentRoom;
}
public Room getAdjacent(int direction) {
return roomNeighbors[direction];
}
public boolean hasGrail() {
return hasGrail;
}
public void putGrail() {
hasGrail = true;
}
public String toString() {
String topbottom = "+-------------------+";
String floor = "%n| |";
String floorEastExit = "%n| _";
String floorWestExit = "%n_ |";
String floorWestAndEastExit = "%n_ _";
String topbottomWithExit = "%n+--------- ---------+";
String top, bottom, side = "";
if (getAdjacent(0) == null)
top = topbottom;
else
top = topbottomWithExit;
if (!(getAdjacent(1) == null) && !(getAdjacent(3) == null))
side = floorWestAndEastExit;
else if (!(getAdjacent(1)== null))
side = floorEastExit;
else if (!(getAdjacent(3)== null))
side = floorWestExit;
if (getAdjacent(2) == null)
bottom = "%n" + topbottom;
else
bottom = "%n" + topbottomWithExit;
if (hasGrail)
bottom = bottom + "%n" + "The grail";
return String.format(top + floor + floor + side + floor + floor + bottom);
}
}
Controller.java
import java.util.Scanner;
public class Controller {
int numberOfRooms;
Room[] gameRooms;
public Controller() {
numberOfRooms = 0;
}
public Controller(int numberOfRooms) {
this.numberOfRooms = numberOfRooms;
gameRooms = new Room[numberOfRooms];
for (int x= 0; x < numberOfRooms; x++)
gameRooms[x] = new Room();
}
public void buildRoom(int roomNumber, String fullDesc) {
String description;
description = fullDesc.substring(1, fullDesc.indexOf("| "));
gameRooms[roomNumber] = new Room(description);
fullDesc = fullDesc.substring(fullDesc.indexOf("| ") + 2);
if (fullDesc.contains("true"))
gameRooms[roomNumber].putGrail();
fullDesc = fullDesc.substring(fullDesc.indexOf(" "));
String[] roomString = fullDesc.split(" ");
Integer[] adjRoomNum = new Integer[roomString.length];
for (int i = 1; i < roomString.length; i++) {
//roomString[i].replaceAll("\\s+", "");
adjRoomNum[i - 1] = Integer.parseInt(roomString[i]);
//System.out.println(adjRoomNum[i - 1] + " " + i);
//System.out.println((roomString[i]));
}
if (adjRoomNum[0] >= 0)
gameRooms[roomNumber].setAdjacent(0, gameRooms[Integer.valueOf(adjRoomNum[0])]);
else if (adjRoomNum[0] == -1)
gameRooms[roomNumber].setAdjacent(0, null);
if (adjRoomNum[1] >= 0)
gameRooms[roomNumber].setAdjacent(1, gameRooms[Integer.valueOf(adjRoomNum[1])]);
else if (adjRoomNum[1] == -1)
gameRooms[roomNumber].setAdjacent(1, null);
if (adjRoomNum[2] >= 0)
gameRooms[roomNumber].setAdjacent(2, gameRooms[Integer.valueOf(adjRoomNum[2])]);
else if (adjRoomNum[2] == -1)
gameRooms[roomNumber].setAdjacent(2, null);
if (adjRoomNum[3] >= 0)
gameRooms[roomNumber].setAdjacent(3, gameRooms[Integer.valueOf(adjRoomNum[3])]);
else if (adjRoomNum[3] == -1)
gameRooms[roomNumber].setAdjacent(3, null);
System.out.println(roomNumber);
}
public Room getStartRoom() {
int startRoom;
do {
startRoom = ((int)(Math.random()) % 4) + 1;
}while(gameRooms[startRoom].hasGrail()==true);
return gameRooms[startRoom];
}
}
Game.java
import java.util.Scanner;
public class Game {
private static Player player;
private static void commandLoop() {
Scanner iHateThisProject = new Scanner(System.in);
String choice;
String direction;
do {
System.out.println(player.getCurrent().getDescription());
System.out.println(player.toString());
System.out.print("What is your bidding(h for help)? ");
choice = iHateThisProject.next();
if ((!choice.equals("h")) && (!choice.equals("l")) && (!choice.equals("g")) && (!choice.equals("q"))){
System.out.print("Error, wrong input.");
}
if (choice.equals("h")) {
System.out.println("h(elp) - print this text");
System.out.println("l(ook) - show current location");
System.out.println("g(o) direction - go into direction N, E, S, or W");
System.out.println("q(uit) - exit game");
}
if (choice.equals("l"))
System.out.println(player.getCurrent().getDescription());
if (choice.equals("q"))
return;
if (choice.equals("g")) {
System.out.print("Please enter the direction(N,E,S,W): ");
direction = iHateThisProject.next();
if ((direction == "N") && (player.getCurrent().getAdjacent(0) == null))
System.out.println("Cannot go this way");
else if (direction == "N")
player.setCurrent(player.getCurrent().getAdjacent(0));
if ((direction == "E") && (player.getCurrent().getAdjacent(1) == null))
System.out.println("Cannot go this way");
else if (direction == "E")
player.setCurrent(player.getCurrent().getAdjacent(1));
if ((direction == "S") && (player.getCurrent().getAdjacent(2) == null))
System.out.println("Cannot go this way");
else if (direction == "S")
player.setCurrent(player.getCurrent().getAdjacent(2));
if ((direction == "W") && (player.getCurrent().getAdjacent(3) == null))
System.out.println("Cannot go this way");
else if (direction == "W")
player.setCurrent(player.getCurrent().getAdjacent(3));
}
}while (!(player.getCurrent().hasGrail()));
iHateThisProject.close();
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String thisName;
// Dungeon: 4 rooms (0 through 3)
String[] rooms = {
"|You're in a large dark room| false 1 -1 -1 2 ",
"|You're in a dark large room| false -1 0 -1 -1 ",
"|You're in a large room, very dark| false 0 0 3 0 ",
"|You're in a dark room, very small| true 0 0 0 0 "
};
// Set the dungeon up according to the input
Controller c = new Controller(rooms.length);
for (int i = 0; i < rooms.length; i++) {
c.buildRoom(i, rooms[i]);
}
System.out.print("Please enter your name: ");
thisName = scnr.next();
// Create and position player
player = new Player(thisName);
player.setCurrent(c.getStartRoom());
System.out.println(player.getCurrent().getAdjacent(0).getDescription());
System.out.println(player.getCurrent().getAdjacent(1).getDescription());
System.out.println(player.getCurrent().getAdjacent(2).getDescription());
System.out.println(player.getCurrent().getAdjacent(3).getDescription());
// Print room description and off you go!
System.out.println(player.getCurrent().getDescription());
commandLoop();
}
}
Player.java
public class Player {
private String name;
private Room currentRoom;
public Player() {
name = "";
}
public Player(String name){
this.name = name;
}
public Room getCurrent() {
return currentRoom;
}
public void setCurrent(Room room){
this.currentRoom = room;
}
public String getname() {
return name;
}
public String toString() {
return currentRoom.toString();
}
}
当前输出
Please enter your name: asfads
0
1
2
3
You're in a large dark room
You're in a large dark room
You're in a large dark room
You're in a large dark room
You're in a dark large room
You're in a dark large room
+--------- ---------+
| |
| |
_ _
| |
| |
+--------- ---------+
What is your bidding(h for help)?
这些行被打印在Game.java类的主体中。 Game.java类通过创建控制器类的实例来启动游戏。启动控制器类时,将在控制器类中创建房间。控制器类通过创建房间来控制房间列表。玩家类包含有关玩家当前所在房间的信息。游戏类控制轮流进行的循环。
此片段在游戏开始时是正确的。它将初始化控制器,然后放置一个启动室。之后,我想将相邻的房间呼叫起始房间,以检查它们是否存在。
在我的代码中,有一个随机的起始空间。我尝试过设置一间起居室,所以我会知道相邻的可用房间是什么。那没用。无论我尝试什么,每次尝试进入相邻房间时,我都会收到同一房间。正确/错误后面的4个数字表示可访问的房间。 0-3分别代表北,东,南,西。 -1表示该房间没有连接。因此,例如,第一个房间的描述为“您在一个大型武器室中”,该房间连接到北部的Room-1和南部的Room-2。因此,我的四行重复代码是每个连接房间的定义。它应该描述从北,东,南和西连接的房间。
似乎返回的每个相邻房间的房间值都在0位置。
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String thisName;
// Dungeon: 4 rooms (0 through 3)
String[] rooms = {
"|You're in a large dark room| false 1 -1 -1 2 ",
"|You're in a dark large room| false -1 0 -1 -1 ",
"|You're in a large room, very dark| false 0 0 3 0 ",
"|You're in a dark room, very small| true 0 0 0 0 "
};
// Set the dungeon up according to the input
Controller c = new Controller(rooms.length);
for (int i = 0; i < rooms.length; i++) {
c.buildRoom(i, rooms[i]);
}
System.out.print("Please enter your name: ");
thisName = scnr.next();
// Create and position player
player = new Player(thisName);
player.setCurrent(c.getStartRoom());
System.out.println(player.getCurrent().getAdjacent(0).getDescription());
System.out.println(player.getCurrent().getAdjacent(1).getDescription());
System.out.println(player.getCurrent().getAdjacent(2).getDescription());
System.out.println(player.getCurrent().getAdjacent(3).getDescription());