简体   繁体   English

循环通过嵌套数组

[英]Looping through a nested array

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

I am having problems looping through an array. 我在循环数组时遇到问题。 Mainly this function here. 这里主要是这个功能。 It is suppose to move the "Player" down one within the matrix. 假设在矩阵内将“播放器”向下移动一个。

Everything I try the "Player" will always drop down to the bottom row when the players position is on the top. 当玩家位置在顶部时,我尝试“玩家”的所有内容总是会下降到最底行。 Also I have been having weird buggy issues. 我也遇到了奇怪的错误问题。 Some times I will not change the code at all (or at least I think so). 有时我根本不会改变代码(或者至少我认为是这样)。 And then the code will not have this issue, and then it will again. 然后代码将不会有这个问题,然后它将再次出现。 Also right now I can't get the "Player" to move any farther to the left then the middle. 现在我也无法让“玩家”向左移动到中间。 I'll show the code for that function at the end. 我将在最后展示该功能的代码。 Thank you for trying to help. 感谢您的帮助。

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;
                }
            }
        }

    }

Here is all my code. 这是我的所有代码。 Don't read it all if you don't have time. 如果你没有时间,请不要全部阅读。 My main issue right now is with the moveDown() function. 我现在的主要问题是使用moveDown()函数。 (I think) (我认为)

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("===============================");
}

Unless I'm misunderstanding your question, you are overcomplicating things. 除非我误解了你的问题,否则你会使事情过于复杂。 This is all you need: 这就是你所需要的:

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;
    }
}

Iterate through the top-level array until you find the row which contains the player. 遍历顶级数组,直到找到包含播放器的行。 Then, once you have found the player, if it's already at the bottom you're done. 然后,一旦你找到了玩家,如果它已经在底部你已经完成了。 Otherwise, overwrite it with a "Blank" , and overwrite the corresponding position in the next row with "Player" . 否则,用"Blank"覆盖它,并用"Player"覆盖下一行中的相应位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM