【问题标题】:Stopping a function, Arrays, and Integer Check停止函数、数组和整数检查
【发布时间】:2013-08-09 23:49:36
【问题描述】:

所以我编写了在战舰游戏中创建 5 艘船的代码。我成功地编写了一些代码来布置所有玩家飞船。它没有错误。我让玩家写下他想要放置他的船的地方的绳索。然后它将船位置写入二维数组中的相应部分,即游戏地图。当然,绳索必须是整数,否则它会崩溃和燃烧。

所以我在做其他事情之前做了一些检查坐标是否是整数。如果不是,它将重新启动函数制作,以便函数的其余部分不会运行。如果你把数字写对了,就没有问题。问题是,如果你不写数字,函数确实会重新启动,但函数或它的某些部分必须仍在运行,因为飞船会无缘无故地写入数组。连绳子都没有指定,所以我不知道这怎么可能。

这是我为检查它是否为整数并重新启动函数而编写的代码。

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

// 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);

【问题讨论】:

    标签: javascript arrays function if-statement


    【解决方案1】:

    首先,请了解在其内部调用函数并不会停止运行原始函数。试试this (jsfiddle) 看看它是如何工作的:

    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();
    

    函数的新递归完成后,原来的递归只是从中断的地方继续;它不会“重新启动”。因此,这不是响应无效用户输入的好方法,尤其是因为您正在使用全局变量。函数的每次递归都可以在将控制权返回给其“父级”(调用它的函数的递归)之前以难以预测的方式改变这些变量。

    您可以做的是使用返回值来检查是否给出了正确的输入:

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

    【讨论】:

    • 谢谢,我明白了我的错误,我认为如果我再次调用该函数,它将重新开始。它实际上所做的只是在 if 语句中调用它,然后继续导致它运行两次,而我认为它只运行一次。
    【解决方案2】:

    当它出错时你不能递归。试试这样的:

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

    你会想要一些他们可以说他们也想退出的方式。

    【讨论】:

    • 我能够递归所有其他条件。那和这有什么区别?
    • @Zachooz 在开始其他条件之前,您要设置userY = userYTest-1; userX = userXTest-1;。因此,当他们出错时,后果只是将船两次设置在同一个地方。对于第一个条件,当它出错时,它会将船设置在之前定义过 userXuserY 的位置,例如 (0, 0)。
    猜你喜欢
    • 1970-01-01
    • 2017-09-17
    • 2014-06-11
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    相关资源
    最近更新 更多