我正在尝试制作一个随机地图生成器。
地图是24x24。
错误并不总是显示。当它显示时它位于函数末尾的“Do / While”循环中。
特别是在“while”行。
function randKeyRoomsLoc(tempMap, dir, locked, room, displayRooms, before){
var location = [];//creates array to store rooms location
if (dir == "n" || dir == "w") {//if directions is NORTH or west
if (before) {
before = false;//if before is true turn to false
}else {
before = true;// if before is false turn to true
}
}
if (dir == "w" || dir == "e") {// if directions is W or E
var lockedRoom = locked[1];
}else {
var lockedRoom = locked[0];
}
for (var i = 0; i < room.length; i++) {//loops thru the rooms
if (before) {//are the rooms being place before locked?
var tempX = lockedRoom + 2;//limist the 2 spaces from the LR
x = Math.floor(Math.random()*(22 - tempX+1)+tempX);//limits the spawn
y = Math.floor((Math.random() * 24));
}else {//are the rooms being place after locked?
var tempX = lockedRoom - 2;//limist the 2 spaces from the LR
x = Math.floor(Math.random()*(tempX - 0 + 1) + 0);//limits
y = Math.floor((Math.random() * 24));
}
if (dir == "w" || dir == "e") {//needs to exchange X & Y if W or E
var tempX = y;
var tempY = x;
y = tempY;
x = tempX
}
if (tempMap[x][y] == "#") {//checks to see the room is empty
tempMap[x][y] = jQuery.extend(true, {}, room[i]);
displayMap[x][y] = displayRooms[i];
}else {//if its not empty
if(dir == "w" || dir == "e"){// IF W OR E--------------------
do {//excecute this once
if (x < 21) {//if Y is less than the end
x++;// we move Y one to the right
if (y < lockedRoom) {//if X is less than the X of locked room
y++;//moves X up
}else {//if X is = or more than the X of lock
y--;//moves it down
}
}else {//if Y is close to the end
x--;//we move it back 5 places
if (y < lockedRoom) {//if X is less than the X of locked room
y++;//moves X up
}else {//if X is = or more than the X of lock
y--;//moves it down
}
}
} while (tempMap[x][y] != "#");//check if room is empty,not loops
tempMap[x][y] = jQuery.extend(true, {}, room[i]);
displayMap[x][y] = displayRooms[i];
}else {// IF S OR N---------------------------------------
do {//excecute this once
if (y < 21) {//if Y is less than the end
y++;// we move Y one to the right
if (x < lockedRoom) {//if X is less than the X of locked room
x--;//moves X up
}else {//if X is = or more than the X of lock
x++;//moves it down
}
}else {//if Y is close to the end
y--;//we move it back 5 places
if (x < lockedRoom) {//if X is less than the X of locked room
x--;//moves X up
}else {//if X is = or more than the X of lock
x++;//moves it down
}
}
} while (tempMap[x][y] != "#");//check if room is empty,not loops
tempMap[x][y] = jQuery.extend(true, {}, room[i]);
displayMap[x][y] = displayRooms[i];
}
}
location.push([x,y]);//saves the location of the room
}
return location;//returns the locations of all the rooms
}