Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:无法读取属性'#';简单JavaScript游戏中未定义的_Javascript_Typeerror - Fatal编程技术网

TypeError:无法读取属性'#';简单JavaScript游戏中未定义的

TypeError:无法读取属性'#';简单JavaScript游戏中未定义的,javascript,typeerror,Javascript,Typeerror,我正在和我的一个朋友做一个小型战舰游戏。我们认为这是一个很好的实践。我们有一个10X10的二维数组,所有的数组都是零。我们将在我们的船将要去的地方把那些0改为1。我做了一个函数来放置一艘1X5飞船。函数提示用户输入两个坐标,并将这些坐标指定给函数外部的变量。它获取起点的坐标,并询问玩家希望船的其余部分转向的方向。如果没有障碍物,则应放置船舶。如果存在一些问题,则应重新启动该功能。问题是我犯了这个错误。在询问垂直坐标位置的提示中,将#替换为数字 这里是错误。 TypeError:无法读取战舰游戏中

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

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

下面是函数

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
函数firstShip(){
警告(“我们将放置你的1×5长的船。注意你在10×10的板上玩。”);
userX=parseInt(提示(“船舶第一单元的水平坐标位置”)-1);
userY=parseInt(提示(“船舶第一单元的垂直坐标位置”)-1);
direction=prompt(“现在选择您希望船的其余部分朝向的方向。您可以使用上、下、左或右。”).toLowerCase();
//确保船能装得下,而且什么都没有!
如果((userX+4)>9&&direction==“right”){
window.alert(“您离板的右边缘太近,无法进行此操作。正在重新启动…”);
第一船();
}
如果((userX-4)9&&direction==“向下”){
window.alert(“您离板的底部边缘太近,无法进行此操作。正在重新启动…”);
第一船();
}

else if((userY-4)发生这种情况是因为您使用的是负数组索引

如果您在此处尝试代码:

在问题的答案右边输入1,1,您将看到script.js的第28行出现错误:

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

Uncaught TypeError: Cannot read property '0' of undefined 

发生的情况是userX=0,i=1,因此我们正在评估
用户[-1][0]
。因为用户[-1]是未定义的,这就是您看到该错误的原因。

请为新代码创建一个fiddle或plnkr,并让我知道您看到的新错误。我将代码放在这里并获得此错误。我已修复该错误,以便它永远不会在数组中运行负数。未捕获的TypeError:无法读取Undefinedeah的属性“0”。我刚刚发布你仍然有负数:很好,这很令人满意。现在你已经开始工作了,你可能想研究重构你的代码。有很多重复,你可以去掉大多数(如果不是全部的话)带有不同问题模型的if语句。这是一个很好的建议,但我计划在f中成功的时候把事情清理干净完成整个游戏。我现在正在努力解决另一个问题,那就是如何将不同的div标签与阵列中的不同插槽连接起来。我试图在屏幕上显示飞船的位置。我唯一的想法是给每个div一个单独的id或class。为20个div创建200个class或id会很痛苦屏幕上有0个框。即使这样,我也必须弄清楚如何将ID链接到阵列的插槽。