Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.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_Appcelerator - Fatal编程技术网

Javascript 在二维数组中以字符串形式存储连接的数字。结果仍然是";“未定义”;

Javascript 在二维数组中以字符串形式存储连接的数字。结果仍然是";“未定义”;,javascript,arrays,appcelerator,Javascript,Arrays,Appcelerator,我是一个新手程序员。我正在制作一个2D战舰游戏。我已经成功地随机分配了上述船只的产卵点,我对下一步如何进行有了想法。 我使用一个2D ship数组来存储ship坐标,每行的最后一个地址是它的状态:0=浮动,1=单次命中,2=两次命中,依此类推。但我遇到了一个问题,需要帮助。我似乎无法将任何内容存储到所述阵列。正如您在我的代码板中所看到的,[][]可以工作,但ship[][]不能 我在这一部分有一个错误: var z = 1; //set to 1 for debugging purposes.

我是一个新手程序员。我正在制作一个2D战舰游戏。我已经成功地随机分配了上述船只的产卵点,我对下一步如何进行有了想法。 我使用一个2D ship数组来存储ship坐标,每行的最后一个地址是它的状态:0=浮动,1=单次命中,2=两次命中,依此类推。但我遇到了一个问题,需要帮助。我似乎无法将任何内容存储到所述阵列。正如您在我的代码板中所看到的,[][]可以工作,但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);
        }
}
这是我的全部代码。也许这会让我明白我想要实现的目标。请随意更正我现有的逻辑

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;
}
var vhposition=0;
var x=0;
var y=0;
var猜测;
var猜测
var fsunk;
var userchoices=[];
var board=[];
var ship=[]//飞船阵列。已转换为二维阵列以保存装运状态和X
Y坐标。
函数createboard()
{
对于(变量i=0;i<10;i++)
{
董事会[i]=[];
}
返回板;
}
功能车队()
{
对于(变量i=0;i<10;i++)
船舶[i]=[];
}
函数rng()//生成随机数
{
var rn=Math.floor(Math.random()*10);
console.log(“rn=”+rn);
返回rn;
}
函数rngesus()
{
var rng=Math.floor(Math.random()*2);
console.log(“rng=“+rng”);
返回rng;
}
函数play()//onclick函数
{
控制台日志(“游戏开始”);
bhit=0;//战舰命中计数器设置为零
c1hit=0;//巡洋舰命中计数器设置为零
//log(“沉没数组=“+sunked[0]+”“+sunked[1]);
fsunk=0;//车队状态
createboard();
舰队();
var i=0;
while(i<10)//为船舶繁殖生成随机点
{
ship[i]=0;//过度杀戮以确保没有剩余数据
log(“循环开始”);//使读取控制台更容易
console.log(“i=“+i”);
spawn(i);//我充当正在处理的船舶id
i++;
}
//游戏();
}
函数spawn(j)//我认为生成了ship position
{
x=rng();
y=rng();
console.log(“x y=“+x+”+y);
董事会[x][y]=1;
console.log(“board x y=“+board[x][y]);
位置(x,y,j);
}
功能位置(a、b、c)
{
控制台日志(“板“+a+”+b);
console.log(“C=“+C”);
vhposition=rngesus();//为船舶定向返回0或1。以后可能会添加4个方向
控制台日志(“VH=“+vhposition”);
var z=1;//为了调试的目的设置为1。z应该是每艘战舰的长度。
ship[c][z]=1;//仅用于调试。在最终迭代期间删除行
console.log(“cz=“+c+”+z);
console.log(“ship c z=“+ship[c][z]);
如果(c==0)
{
for(z=0;z<4;z++)//此for循环用于战列舰。更多for循环将为每种船型添加一个。
{
console.log(“ab=“+a+”+b);
船舶[c][z]=(“”+a+b);
log(“粗略数组”+ship[c][z]);
a++;
console.log(“Z=”+Z);
}
}
}
//函数游戏()
{
//待续。。。
}
函数userinput()//这工作正常
{
猜测=提示(“输入栅格坐标。不要使用空格。X坐标0-6,Y坐标0-6”);
console.log(“用户输入=”+guess);
while(guess<0 | | guess>99 | | | | userchoices.includes(guess))//检查用户输入是否重复敲击或超出范围。如果可能,建议更好的方法。这只是错误代码
{
警报(“您输入的坐标无效”);
猜测=提示(“再试一次!”);
}
猜测+++;//增加猜测次数
userchoices.push(guess);//将用户猜测添加到数组中
返回猜测;
}
很抱歉问了这么长的问题/帖子


谢谢。

函数
位置
是从
产卵
调用的,
产卵
是从
播放
调用的,在
播放
中,您将0分配给
配送[i]
?因此
飞船
不再是2D数组。

函数
位置
产卵
调用,
产卵
播放
调用,在
播放
中,将0分配给
飞船[i]
?因此,
飞船
不再是2D阵列。

请您详细说明您的问题和收到的错误?例如,哪个结果是未定义的?我似乎无法将任何数据存储到ship 2d数组中。正如你所看到的,我有board[x][y]=1,它可以工作,但是ship[c][z]=1不能工作,即使bot c和z有整数值。请你能更具体地说明你的问题和你收到的错误吗?例如,哪个结果是未定义的?我似乎无法将任何数据存储到ship 2d数组中。正如你所看到的,我有board[x][y]=1,它可以工作,但是ship[c][z]=1不能工作,即使bot c和z有整数值,所以我删除了那一行?所以我删除了那一行?
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;
}