2

我正在和我的一个朋友制作一个小型战舰游戏。正如我们所学到的,我们认为这将是一个很好的做法。我们有一个 10X10 的二维数组,全部用零填充。我们将在我们的船将去的地方将这些零更改为一。我做了一个功能来放置一艘 1X5 船。该函数提示用户输入两个坐标并将它们分配给函数外部的变量。它获取起点的坐标,并询问玩家希望船的其余部分去向的方向。如果没有障碍物,则应放置船。如果有一些功能应该重新启动。问题是我得到了这个错误。# 替换为提示中询问垂直坐标位置的数字。

这是错误。类型错误:无法读取战舰游戏中未定义的属性“#”

这是功能

function firstShip(){
    window.alert("We will be placing your 1 by 5 length ship.  Take note that you are playing on a 10 by 10 board.");
    userX = parseInt(prompt("Horizontal Coordinate position for the first unit of a ship")-1);
    userY = parseInt(prompt("Vertical Coordinate position for the first unit of a ship")-1);
    direction = prompt("Now choose the direction you want the rest of your ship to face.  You may   use the words up, down left, or right.").toLowerCase(); 

    //Making sure the ship will fit and nothing is already there!
    if ((userX+4)>9 && direction=== "right"){
     window.alert("You are too close to the right edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userX-4)<0 && direction=== "left"){
     window.alert("You are too close to the left edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userY+4)>9 && direction=== "down"){
     window.alert("You are too close to the bottom edge of the board to do that. Restarting...");
     firstShip();
    }
    else if ((userY-4)<0 && direction=== "up"){
     window.alert("You are too close to the top edge of the board to do that. Restarting...");
     firstShip();
    }
    else if (user[userX][userY] === 1) {
        window.alert("Coordinate already used. Please try again");
        firstShip();
    } 

    else if (user[userX][userY] === null || user[userX][userY] === ""){
        window.alert("That coordinate isn't on the board. Restarting...");
        firstShip();
    }

    else if(direction !=="up" || direction !=="down" || direction !=="left" || direction !=="right") {
        for(i=1; i<5; i++){
            if(user[userX+i][userY] === 1 && direction=== "right"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX-i][userY] === 1 && direction=== "left"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX][userY+i] === 1 && direction=== "down"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(user[userX][userY-i] === 1 && direction=== "up"){
                window.alert("Can't place your ship in that direction, another ship is in your way.");
                isThere=true;
            }
            if(isThere===true){
                isThere = false;
                firstShip(); 
                return false;
            }
            else{
                user[userX][userY] = 1;
            }
        }

    }
    else{
        window.alert("Sorry but you didn't type in the direction you wanted your ship to go correctly. Restarting...");
        firstShip();
    }


    // Building Ship 1x5
    for(i=1; i<4; i++){

        if (direction==="up"){
            user[userX][userY-i] =1;
        }
        else if (direction==="down"){
            user[userX][userY+i] =1;
        }
        else if (direction==="left"){
            user[userX-i][userY] =1;
        }
        else if (direction==="right"){
            user[userX][userY+i] =1;
        }
    }
}
firstShip();
//Here is the new cpu creation. There may be a way to shorten the if statements, but other than that, its all set
4

1 回答 1

1

This is happening because you are using a negative array index.

If you try the code here: http://plnkr.co/edit/g4i1QAY6COU18XTK6QZI?p=preview

and enter 1, 1, right for the answers to the questions, you will see that you get an error on line 28 of script.js:

if(user[userX-i][userY] === 1 && direction=== "left"){

The error is:

Uncaught TypeError: Cannot read property '0' of undefined 

What happens is that userX = 0 and i = 1, so we are evaluating user[-1][0]. Since user[-1] is undefined, that's why you're seeing that error.

于 2013-08-08T00:22:08.747 回答