2

友達と小さな戦艦ゲームを作っています。私たちが学んだように、それは良い習慣になると思いました。すべてゼロで埋められた 10X10 の 2 次元配列があります。船が行く場所で、これらのゼロを 1 に変更します。1X5の船を降ろす機能を作りました。この関数は、ユーザーに 2 つの座標の入力を求め、それらを関数の外部にある変数に割り当てます。開始点の座標を取得し、プレイヤーが船の残りの部分を移動したい方向を尋ねます。障害物がない場合は、船を配置する必要があります。いくつかある場合、関数は再起動する必要があります。問題は、このエラーが発生することです。# は、垂直座標位置を求めるプロンプトの数字に置き換えられます。

これがエラーです。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
4

1 に答える 1