1

所以我编写了在战舰游戏中创建 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,0,0,0,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]];

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

2 回答 2

1

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

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

}

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

于 2013-08-09T23:55:07.443 回答
1

首先,了解在其内部调用函数并不会停止运行原始函数。试试这个(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];
  }
于 2013-08-10T00:49:04.810 回答