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

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure which issue you mean then. This works fine no matter where you start the player. If there's a specific error case you're having, reproduce it on jsfiddle.net, and drop a link.
I've never heard of jsfiddle.net, thanks for the link. I just figured out what it was though... I had a for loop in the movePlayer function which I didn't need. But thank you, you made things a lot cleaner and it now works!
I was just wondering. could you explain what the purpose is for the if (posX < 0) continue; ? I don't understand how or why that works...
@Alpha Sure. Basically it's doing what you were doing before: if (posX == -1). That says: if the player is not found in this row, skip down to the next row. indexOf returns -1 if the search key isn't found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.