我需要做一个棋盘游戏,我从数组[3][0]“Ursa Major”的左下角开始。当我向“西”移动时,我得到一个 NullPointerException。我还需要一些帮助,沿对角线移动到 [2][1] “northeast”,但不知道如何为它编写代码。任何帮助,将不胜感激。
//Declare Variables
Map map;
String input;
Scanner scan;
int row, col;
//Initialize Varibales
map = new Map();
scan = new Scanner(System.in);
row = 3; col = 0;
//Begin user dialog
System.out.println("Welcome to the Great Cal Poly Underground");
input ="";
while(!input.equals("quit"))
{
System.out.println(map.rooms[row][col].name);
System.out.print(">");
input = scan.nextLine().toLowerCase();
if (input.equals("w"))
{ if(map.rooms[row][col].isValidExit("w"))
col--;
else
System.out.println("You cant go that way");
}
else
if (input.equals("e"))
{ if(map.rooms[row][col].isValidExit("e"))
col++;
else
System.out.println("You cant go that way");
}
这是我的用户界面,这是我的地图
Room[][] rooms = new Room[4][4];
Map()
{
Room lectureHall = new Room();
Room cafeteria = new Room();
lectureHall.name = "Ursa Major";
lectureHall.exits = new String []{"e"};//can add north south west
cafeteria.name = "Los Olivos";
cafeteria.exits = new String []{"w"}; // can add north south east
rooms[3][0] = lectureHall;
rooms[2][1] = cafeteria;
这是我的房间
boolean isValidExit(String anExit)
{
boolean result = false;
int index = 0;
while (result == false && index < exits.length)
{
if(exits[index].equals(anExit))
result = true;
index++;
}
return result;