简体   繁体   中英

Javascript pushing values into multidimensional array with for loop

I'm new to Javascript and trying to put together a checkerboard for a course. I have to set up the board with the black and red checkers (ie the first three rows for red, the last three for black, every other tile has a checker), and I'm a bit stuck. I tried the code below, which just results in each value in the array for var checkers being printed as null , followed by four 'B's . I imagine I need to somehow incorporate the multidimensionality of the array into my for loop, but I have no idea how to do that. Any guidance or help is much appreciated!

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

Is it what you were looking for?

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"
 }  
}

If you have already defined your checkerboard you shouldn't push items..

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

I assume you wanted this to look like a regular checkerboard using checker pieces. This is how you would do it.

Firstly, your 'for' conditions are wrong.

In the first loop, you test if i < checkerboard [3][7]. In other words, you are testing if i < null, which is always false (since null == 0 in JS) and thus, you never enter this loop.

In the second loop, you test if i > checkerboard [6][0]. In other words, you are testing if i > null (which is the same as i > 0). Also, you initialize it with i = (checkerboard.length - 1), which means the last line of your matrix (the length refers to your top-level array, which contains the lines). Considering you decrement i by 2 each iteration, you will iterate here 4 times.

Secondly, you are pushing values into an already filled array. This will add elements instead of overwriting on your desired positions. For overwriting, you must write directly to the desired position by indexing it (the row and the column, 2 dimensions).

So, the solution would be:

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

This is just the initial fix for your code. You will need to shift the starting position in alternate rows too to have the correct board.

The following code will properly set up both players and account for the offset of the middle row for each player.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM