0
var map = [
        ["Blank", "Blank", "Blank"],
        ["Blank", "Player", "Blank"],
        ["Blank", "Blank", "Blank"]
];

我在遍历数组时遇到问题。这里主要是这个功能。假设在矩阵中将“播放器”向下移动一个。

当玩家的位置在顶部时,我尝试“玩家”的所有内容总是会下降到最后一行。我也遇到了奇怪的越野车问题。有时我根本不会更改代码(或者至少我是这么认为的)。然后代码不会有这个问题,然后它会再次出现。同样,现在我无法让“播放器”向左移动,然后再向中间移动。我将在最后展示该函数的代码。感谢您尝试提供帮助。

function moveDown() {
        for (y = map.length - 1; y >= 0 ; y--) {
            for (x = map[y].length - 1; x >= 0; x--) {
                var posX = map[y].indexOf("Player");

                if (posX > -1 && y == 0) {
                    map[1].splice(posX, 1,"Player");
                    map[y].splice(posX, 1,"Blank");
                    return;
                } else if (posX > -1 && y == 1) {
                    map[2].splice(posX, 1,"Player");
                    map[y].splice(posX, 1,"Blank");
                    return;
                } else if (posX > -1 && y == 2) {
                    return;
                }
            }
        }

    }

这是我所有的代码。如果你没有时间,不要全部阅读。我现在的主要问题是 moveDown() 函数。(我认为)

var map = [
        ["Blank", "Blank", "Blank"],
        ["Blank", "Player", "Blank"],
        ["Blank", "Blank", "Blank"]
];

var run = true;

menu();
printLoop(map);


while (run) {
    var input = prompt();

    if (input == "left") {
        movePlayer("left");
    } else if (input == "right") {
        movePlayer("right");
    } else if (input == "up") {
        movePlayer("up");
    } else if (input == "down") {
        movePlayer("down");
    }

    switch (input) {
      case "menu":
        menu(); break;
      case "quit": 
        run = false; break;
    }
    menu();
    printLoop(map);
}


function movePlayer(direction) {
    for (y=0; y<map.length; y++) {
        var playerPos = map[y].indexOf("Player");

        if (movableRight(playerPos)) {
            if (direction == "right") {
                map[y].splice(playerPos, 1,"Blank");
                map[y].splice(playerPos + 1, 1,"Player");
            }
        } else if (movableLeft(playerPos)) {
            if (direction == "left") {
                map[y].splice(playerPos, 1,"Blank");
                map[y].splice(playerPos - 1, 1,"Player");
            }
        }

        if (direction == "up") {
            moveUp();
        } else if (direction == "down") {
            moveDown();
        }

    }

}

/*function getX(obj) {
    for (x = 0; x < map.length; x++) {
        for (y = 0; y < map[x].length; y++) {
            if (map[x][y] == obj) {
                return x;
            }
        }
    }
}

function getY(obj) {
    for (x = 0; x < map.length; x++) {
        for (y = 0; y < map[x].length; y++) {
            if (map[x][y] == obj) {
                return y;
            }
        }
    }
}*/

function movableLeft(pos) {
    if (pos <= 0) {
        console.log(pos + "<= 0");
        return false;
    } else if (pos > map.length - 1) {
        console.log(pos + "> map.length - 1");
        return false;
    } else {
        console.log(pos + "true");
        return true;
    }
}

function movableRight(pos) {
    if (pos < 0) {
        return false;
    } else if (pos >= map.length - 1) {
        return false;
    } else {
        return true;
    }
}

function moveUp() {
    for (y = 0; y < map.length; y++) {
        for (x = 0; x < map[y].length; x++) {
            var posX = map[y].indexOf("Player");
                if(posX > -1) {
                    switch (y) {
                        case 1:
                            map[0].splice(posX, 1,"Player");
                            map[y].splice(posX, 1,"Blank");
                            break;
                        case 2:
                            map[1].splice(posX, 1,"Player");
                            map[y].splice(posX, 1,"Blank");
                }
            }
        }   
    }

}

function moveDown() {
    for (y = map.length - 1; y >= 0 ; y--) {
        for (x = map[y].length - 1; x >= 0; x--) {
            var posX = map[y].indexOf("Player");

            if (posX > -1 && y == 0) {
                map[1].splice(posX, 1,"Player");
                map[y].splice(posX, 1,"Blank");
                return;
            } else if (posX > -1 && y == 1) {
                map[2].splice(posX, 1,"Player");
                map[y].splice(posX, 1,"Blank");
                return;
            } else if (posX > -1 && y == 2) {
                return;
            }
        }
    }

}


function printLoop(array) {
    var line0 = "";
    var line1 = "";
    var line2 = "";

    for (y = 0; y < array.length; y++) {
        for (x = 0; x < array[y].length; x++) {
            switch (y) {
                case 0:
                    line0 += array[y][x] + ", "; break;
                case 1:
                    line1 += array[y][x] + ", "; break;
                case 2:
                    line2 += array[y][x] + ", "; break;
            }
        }
    }
    console.log(" ");
    console.log(line0);
    console.log(line1);
    console.log(line2);
    console.log(" ");

}

function menu() {
    console.log("===============================");
    console.log("up - down - right - left - quit");
    console.log("===============================");
}
4

1 回答 1

2

除非我误解了你的问题,否则你把事情复杂化了。这就是你所需要的:

function moveDown() {
    for (y = 0; y < map.length; y++) {
        var posX = map[y].indexOf("Player");
        if (posX < 0) continue;
        if ( y == map.length-1 ) break;

        map[y][posX] = "Blank";
        map[y+1][posX] = "Player";
        break;
    }
}

遍历顶层数组,直到找到包含播放器的行。然后,一旦你找到了播放器,如果它已经在底部,你就完成了。否则,用 覆盖它"Blank",并用 覆盖下一行中的相应位置"Player"

于 2013-04-03T01:35:21.200 回答