Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
Javascript 停止函数、数组和整数检查_Javascript_Arrays_Function_If Statement - Fatal编程技术网

Javascript 停止函数、数组和整数检查

Javascript 停止函数、数组和整数检查,javascript,arrays,function,if-statement,Javascript,Arrays,Function,If Statement,所以我编写了在战舰游戏中创建5艘战舰的代码。我成功地编写了一些代码,展示了所有玩家的飞船。它没有虫子。我让玩家写下他想把船放在哪里的绳子。然后,它将在船的位置写入二维数组中的相应部分,这就是游戏地图。。当然,跳线必须是整数,否则它会崩溃并烧坏 然后我做了一些检查,在做其他事情之前,坐标是否是整数。如果不是,它将重新启动函数生成,以便函数的其余部分不会运行。如果你把数字写对了,就没有问题了。问题是,如果不写入数字,函数确实会重新启动,但函数或其某些部分必须仍在运行,因为某个ship会无缘无故写入阵

所以我编写了在战舰游戏中创建5艘战舰的代码。我成功地编写了一些代码,展示了所有玩家的飞船。它没有虫子。我让玩家写下他想把船放在哪里的绳子。然后,它将在船的位置写入二维数组中的相应部分,这就是游戏地图。。当然,跳线必须是整数,否则它会崩溃并烧坏

然后我做了一些检查,在做其他事情之前,坐标是否是整数。如果不是,它将重新启动函数生成,以便函数的其余部分不会运行。如果你把数字写对了,就没有问题了。问题是,如果不写入数字,函数确实会重新启动,但函数或其某些部分必须仍在运行,因为某个ship会无缘无故写入阵列。甚至还没有为它指定跳线,所以我不知道这怎么可能

下面是我用来检查它是否为整数并重新启动函数的代码

userYTest = parseInt(prompt("Horizontal Coordinate position for the first unit of a ship"));
userXTest = parseInt(prompt("Vertical Coordinate position for the first unit of a ship"));

        if(userXTest % 1 === 0 && userYTest % 1 === 0) {
            userY = userYTest-1;
            userX = userXTest-1;
            direction = prompt("Now choose the direction you want the rest of your ship to face.  You may   use the words left, right up, or down.").toLowerCase(); 
        }
        else{
            window.alert("You must enter a number between one and ten for the two coordinates.");
            ship();
//ship is the name of the function
        }
这是所有的代码

//These are all the different game boards you need to keep track of. Two possible values in each position 1 or 0
var user = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var cpu = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var userGuessed = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var userHit = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var cpuGuessed = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];
var cpuHit = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];

var clearBoard = [[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0]];

// These are just used to set left the game board.
// I counted 10 by 10 - it should be 10 by 10
var userY = 0;
var userX = 0;
var cpuX = 0;
var cpuY = 0;
var cpuDir = 0;
var cpuWork = false;
var direction = "";
var isThere = false;
var i=0;
var userXTest;
var userYTest;

// In battleship, there is 1x5 length ship, 1x4 length ship, 2 1x3 length ship, and 1x2 length ship. down now it checks how many units are covered to see if you have all the ships. Later we need to add so they ships are the down shape


//User will add their ships here one by one.  If you can think of a better have a go at it!

for(i=0;i<4;i++){
    if (i===0){
        window.alert("We will be placing your 1 by 5 length ship.  Take note that you are playing on a 10 by 10 board.");
        ship();
    }
    if (i===1){
        window.alert("We will be placing your 1 by 4 length ship.  Take note that you are playing on a 10 by 10 board.");
        ship();
    }
    if (i===2){
        window.alert("We will be placing your two 1 by 3 length ships.  Take note that you are playing on a 10 by 10 board.");
        ship();
        ship();
    }
    if (i===3){
        window.alert("We will be placing your 1 by 2 length ship.  Take note that you are playing on a 10 by 10 board.");
        ship();
    }
    function ship(){
        userYTest = parseInt(prompt("Horizontal Coordinate position for the first unit of a ship"));
        userXTest = parseInt(prompt("Vertical Coordinate position for the first unit of a ship"));

        if(userXTest % 1 === 0 && userYTest % 1 === 0) {
            userY = userYTest-1;
            userX = userXTest-1;
            direction = prompt("Now choose the direction you want the rest of your ship to face.  You may   use the words left, right up, or down.").toLowerCase(); 
        }
        else{
            window.alert("You must enter a number between one and ten for the two coordinates.");
            ship();
        }
        //Making sure the ship will fit and nothing is already there!
        if ((userY+4-i)>9 && direction=== "down"){
         window.alert("You are too close to the down edge of the board to do that. Restarting...");
         ship();
        }
        else if ((userY-4-i)<0 && direction=== "up"){
         window.alert("You are too close to the up edge of the board to do that. Restarting...");
         ship();
        }
        else if ((userX+4-i)>9 && direction=== "right"){
         window.alert("You are too close to the bottom edge of the board to do that. Restarting...");
         ship();
        }
        else if ((userX-4-i)<0 && direction=== "left"){
         window.alert("You are too close to the top edge of the board to do that. Restarting...");
         ship();
        }
        else if (user[userY][userX] === 1) {
            window.alert("Coordinate already used. Please try again");
            ship();
        } 

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

        else if(direction ==="left" || direction ==="right" || direction ==="up" || direction ==="down") {
            for(var a=1; a<5-i; a++){

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

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


        // Building Ship 1x5
        for(var b=1; b<5-i; b++){

            if (direction==="left"){
                user[userY][userX-b] =1;
            }
            else if (direction==="right"){
                user[userY][userX+b] =1;
            }
            else if (direction==="up"){
                user[userY-b][userX] =1;
            }
            else if (direction==="down"){
                user[userY+b][userX] =1;
            }
        }
}

}

console.log(user);
//这些都是您需要跟踪的不同游戏板。每个位置有两个可能的值1或0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0[0,0,0,0,0,0,0,0,0,0,0];
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,[0,0,0,0,0,0,0,0,0,0,0];
0,0,0,0 0 0,0 0,0,0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0];
0,0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0 0,0 0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,[0,0,0,0,0,0,0,0,0,0,0];
//这些只是用来设置左侧的游戏板。
//我数了10乘10——应该是10乘10
var userY=0;
var userX=0;
var-cpuX=0;
var cpuY=0;
var-cpuDir=0;
var cpuWork=false;
var方向=”;
var isThere=false;
var i=0;
var-userXTest;
var-userYTest;
//战列舰上有1x5长、1x4长、1x3长和1x2长的战舰。现在它会检查覆盖了多少单位,看看你是否拥有所有的飞船。稍后我们需要添加,使其成为向下形状
//用户将在这里逐个添加他们的船只。如果你能想出更好的办法,那就试试吧!
对于(i=0;i9&&direction==“向下”){
window.alert(“您离电路板下边缘太近,无法进行此操作。正在重新启动…”);
ship();
}
如果((userY-4-i)9&&direction==“right”){
window.alert(“您离板的底部边缘太近,无法进行此操作。正在重新启动…”);
ship();
}

否则,如果((userX-4-i)出现错误,则无法递归。请尝试以下操作:

var done;
while (!done) {
    if(userXTest % 1 === 0 && userYTest % 1 === 0) {
        userY = userYTest-1;
        userX = userXTest-1;
        direction = prompt("Now choose the direction you want the rest of your ship to face.  You may   use the words left, right up, or down.").toLowerCase(); 

        ... All the other tests that are after the else part ...

        else { // if its a good answer
            done = true;
        }
    }
    else{
        window.alert("You must enter a number between one and ten for the two coordinates.");
    }

}

你会想让他们说他们也想退出。

首先,要明白调用函数本身并不会停止运行原始函数。试着看看它是如何工作的:

var i = 0;
function askDogsName() {
    var dogsName = prompt("What is the dog's name?");
    if (dogsName != "Rover") {
        askDogsName();
    }
    i++;
    document.body.innerHTML += "i = " + i
        + "; dog's name: " + dogsName + '<br />';
}
askDogsName();

不过,我可以对其他每一个条件进行递归。这和这之间有什么区别?@Zachooz在开始其他条件之前,您正在设置
userY=userYTest-1;userX=userXTest-1;
。因此,当它们出错时,结果只会将船设置在同一位置两次。对于第一个条件,当它发生错误时rong,它在以前定义过的
userX
userY
的地方设置ship,例如(0,0).谢谢,我明白我的错误了,我想如果我再次调用函数,它会重新启动。它实际上只是在if语句中调用它,然后继续导致它运行两次,而我认为它只运行了一次。
function ship() {
     var c;
     while (!c) {
         c = getValidCoords();
     }
     x = c[0];
     y = c[1];
     // then make the ship at x, y
 }
 function getValidCoords() {
     y = parseInt(prompt("Horizontal Coordinate position for the first unit of a ship"));
     x = parseInt(prompt("Vertical Coordinate position for the first unit of a ship"));
     // conduct various tests on x and y
     if (testsFail) {
         return false;
     }
     return [x, y];
  }