Javascript用for循环将值推入多维数组



我是Javascript的新手,并试图为课程组合一个棋盘。我必须设置带有黑色和红色方格的棋盘(即前三行为红色,后三行为黑色,每隔一层都有一个方格),我有点卡住了。我尝试了下面的代码,这只是导致var checkers数组中的每个值被打印为null,然后是四个'B's。我想我需要把数组的多维度合并到我的for循环中,但我不知道怎么做。任何指导或帮助是非常感激!

var checkerboard = [[null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null]];

function setUpRed(square) {
    return (square = 'R');
}
for (var i = 0; i < checkerboard [3][7]; i += 2 ) {
    checkerboard.push(setUpRed(checkerboard[i]));
}
function setUpBlack(square) {
    return (square = 'B');
}
for (var i = (checkerboard.length - 1); i > checkerboard [6][0]; i -= 2) {
    checkerboard.push(setUpBlack(checkerboard[i]));
}
console.log(checkerboard);

这是你要找的吗?

for(var i = 0; i < 3; i++){
 for(var j = 0; j < row.length; j++){
   checkerboard[i][j] = "RED"
 }  
}
for(var i = allrows - 3; i < 3; i++){
 for(var j = 0; j < row.length; j++){
   checkerboard[i][j] = "BLACK"
 }  
}

如果你已经定义了你的棋盘,你不应该推项目…

var checkerboard = [[null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null]];

for(var i = 0; i < 3; i++){
  for(var j = 0; j < 8; j+=2){
    if (i % 2) checkerboard[i][j] = "R";
    else checkerboard[i][j+1] = "R";
  }  
}
for(var i = checkerboard.length - 3; i < checkerboard.length ; i++){
  for(var j = 0; j < 8; j+=2){
    if (i%2) checkerboard[i][j] = "B";
    else checkerboard[i][j+1] = "B";
  }  
}
console.log(checkerboard);

我假设你想让它看起来像一个使用棋子的常规棋盘。这就是你要做的。

首先,你的for条件是错误的。

在第一个循环中,测试i <棋盘[3][7]。换句话说,你是在测试它是否>

在第二个循环中,测试i> checkerboard[6][0]。换句话说,您正在测试i是否> null(这与i> 0相同)。此外,您使用i = (checkerboard)初始化它。长度- 1),这意味着矩阵的最后(长度指的是包含行的顶级数组)。考虑到每次迭代i减2,您将在这里迭代4次。

其次,您正在将值压入已经填充的数组。这将添加元素,而不是覆盖您想要的位置。对于覆盖,您必须通过索引(行和列,2维)直接写入所需的位置。

所以,解决方案是:

for(var i = 0; i < 3; i++){
 for(var j = 0; j < checkerboard[i].length; j+=2){
   checkerboard[i][j] = "R";
 }  
}
for(var i = checkerboard.length - 1; i > checkerboard.length - 3; i--){
 for(var j = 0; j < checkerboard[i].length; j+=2){
   checkerboard[i][j] = "B";
 }  
}

这只是您的代码的初始修复。您还需要在交替行中移动起始位置,以获得正确的电路板。

下面的代码将正确地设置两个播放器,并说明每个播放器的中间行偏移。

http://repl.it/agR/3344

var checkerboard = [[null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null],
                    [null,null,null,null,null,null,null,null]];
function setSquare(player, row, col) {
    checkerboard[row][col] = player;
}
function getPieceAt(row, col) {
    console.log(checkerboard[row][col] || 'null');
}
function clearBoard() {
    for (i = 0; i < checkerboard.length; i++) {
        checkerboard[i][i] = null;
    }
}
function setUpRed() {
    for (row = 0; row < 3; row++) {
        if ((row === 0) || (row === 2)) {
            for (col = 0; col < checkerboard[row].length; col += 2) {
            checkerboard[row][col] = "R";   
            }
        }
        else {
            for (col = 1; col < checkerboard[row].length; col += 2) {
            checkerboard[row][col] = "R";
            }
        }
    }
}
function setUpBlack() {
    for (row = 5; row < 8; row++) {
        if ((row === 5) || (row === 7)) {
            for (col = 1; col < checkerboard[row].length; col += 2) {
            checkerboard[row][col] = "B";   
            }
        }
        else {
            for (col = 0; col < checkerboard[row].length; col += 2) {
            checkerboard[row][col] = "B";
            }
        }
    }
}
setUpRed();
setUpBlack();
console.log(checkerboard);

相关内容

  • 没有找到相关文章

最新更新