将串联的数字存储为二维数组中的字符串。结果仍然 "undefined"

Storing concatenated numbers as string in a 2D array. Result remains "undefined"

我是一名新手程序员。我正在创建一个 2D 战舰游戏。我已经成功地 运行 控制了上述船只的重生点,并且我知道下一步该怎么做。 我正在使用二维船舶数组来存储船舶坐标,每行中的最后一个地址将是其状态:0=浮动,1=单次命中,2=两次命中,依此类推。但是我 运行 遇到了问题,需要帮助。我似乎无法将任何内容存储到所述数组中。正如您在我的代码中看到的那样,board[][] 有效,但 ship[][] 无效。

我在这部分有错误:

var z = 1; //set to 1 for debugging purposes. z is supposed to be the length of each battleship.
ship[c][z] = 1; // for debug only. line to be removed during final iteration
console.log("c z = " + c + " " + z);
console.log("ship c z = " + ship[c][z]);
if(c == 0)
{
    for(z = 0; z < 4; z++)// this for loop is for battlehsip. more for loops to be added one for each ship type.
        {
            console.log("a b = " + a + " " + b);
            ship[c][z] = ("" + a + b);
            console.log("sketchy array " + ship[c][z]);
            a++;
            console.log("Z = " + z);
        }
}

这是控制台输出(修剪):

Loop start
i = 0
rn = 7
rn = 0
x y =7 0
board x y = 1
Board 7 0
C = 0
rng = 0
VH = 0
c z = 0 1
ship c z = undefined
a b = 7 0
sketchy array undefined
Z = 0
a b = 8 0
sketchy array undefined
Z = 1
a b = 9 0
sketchy array undefined
Z = 2
a b = 10 0
sketchy array undefined
Z = 3

这是我的完整代码。也许这会清除我想要实现的目标。随时纠正我现有的逻辑。

var vhposition = 0;
var x = 0;
var y = 0;
var guess;
var guesses
var fsunk;
var userchoices = [];
var board = [];
var ship = []; //ship array. converted to 2D array to hold ship status and X 
Y coordinates.

function createboard()
{
for (var i = 0; i < 10; i++)
{
    board[i] = [];
}
return board;
}
function fleet()
{
for(var i = 0; i < 10; i ++)
    ship[i] = [];
}
function rng() //Generates Random Numbers
{
var rn = Math.floor(Math.random() * 10);
console.log("rn = " + rn);
return rn;
}
function rngesus()
{
var rng = Math.floor(Math.random() * 2);
console.log("rng = " + rng);
return rng;
}
function play() // onclick function
{
console.log("game start");
bhit = 0; //battleship hit counter set to zero
c1hit = 0; //cruiser hit counter set to zero
//console.log("sunk array = " + sunk[0] + " " + sunk[1]);
fsunk = 0; //fleet status
createboard();
fleet();
var i = 0;
while(i < 10) // generates random points for ship spawn
{
    ship[i] = 0; //overkill to ensure no residual data
    console.log("Loop start"); //makes reading console easier
    console.log("i = " + i); 
    spawn(i); //i acts as the ship id that is being worked on
    i++;
}
//game();

}
function spawn(j) // ship positon generated, i think
{
x = rng();
y = rng();
console.log("x y =" + x +" "+ y);
board[x][y] = 1;
console.log(" board x y = " + board[x][y]);
position(x, y, j);
}
function position(a,b,c)
{
console.log("Board " + a + " " + b);
console.log("C = " + c);
vhposition = rngesus(); //returns 0 or 1 for ship orienetation. maybe later will add 4 way
console.log("VH = " + vhposition);
var z = 1; //set to 1 for debugging purposes. z is supposed to be the length of each battleship.
ship[c][z] = 1; // for debug only. line to be removed during final iteration
console.log("c z = " + c + " " + z);
console.log("ship c z = " + ship[c][z]);
if(c == 0)
{
    for(z = 0; z < 4; z++)// this for loop is for battleship. more for loops to be added one for each ship type.
        {
            console.log("a b = " + a + " " + b);
            ship[c][z] = ("" + a + b);
            console.log("sketchy array " + ship[c][z]);
            a++;
            console.log("Z = " + z);
        }
}
}
//function game()
{
//to be continued...
}
function userinput()// this works fine
{
guess = prompt("Enter the grid coordinates. Do not use space. X-coordinates 0-6, Y-coordinates 0-6.");
console.log("users input = " + guess);

while(guess < 0 || guess > 99 || userchoices.includes(guess)) //checks user input for repeated strikes or out of range. Suggest better way if possible. this is just bad code
{
    alert("You have entered an invalid coordinate.");
    guess = prompt("Try Again!");
}
guesses++; //increments no of guessess
userchoices.push(guess); //add users guess to array
return guess;
}

抱歉这么久 question/post。

谢谢。

函数 position 是从 spawn 调用的,spawn 是从 play 调用的,而在 play 中,您将 0 分配给 ship[i] ?所以 ship 不再是二维数组。