ボード ゲームを作成する必要があり、配列 [3][0] "Ursa Major" の左下隅から始めました。「西」に移動すると、NullPointerException が発生します。[2][1]「北東」に斜めに移動するのにも助けが必要ですが、そのコードの書き方がわかりません。任意の助けをいただければ幸いです。
//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;