我正在尝试创建一个网格 4 列 X 5 行形状的地图,并将每个房间连接在一起。下面的代码可以编译,但是当我尝试运行它时会出现错误。谁能告诉我哪里出错了?谢谢。
public static final int NUM_ROOMS = 20;
public static final int NUM_COLS = 4;
private ArrayList<Room> myRooms;
public Map(){
int row =0;
ArrayList<Room> myRooms = new ArrayList<Room>();
while (row<NUM_COLS){
int i =0;
i = row % NUM_COLS;
while(i+1<NUM_COLS){
while (i+1 <(row+1)% NUM_COLS){
Room r1 = myRooms.get(i);
Room r2 = myRooms.get(i+1);
r1.connectTo(r2);
r2.connectTo(r1);
i++;
}
row++;
}
int col = 0;
while (col<NUM_COLS){
i = col;
while (i+4<NUM_ROOMS){
i = i+NUM_COLS;
}
col++;
}
}
}