Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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 (i.e. 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);
share|improve this question

4 Answers 4

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..

share|improve this answer
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.

share|improve this answer
    
Thanks @eddyjs, your code worked perfectly. Seeing it written out made sense, but I was unsure how to go about indexing a multidimensional array. Thanks again. – Joshua Swiss Aug 3 at 6:49
    
No problem :) I'm glad you got it working. – eddyjs Aug 3 at 7:09

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.

share|improve this answer
    
Thanks so much. Your explanation made a lot of sense. I'm trying the code you provided and will play around with it a bit. I'll update once I get it sorted. Thanks again! – Joshua Swiss Aug 3 at 6:34

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);
share|improve this answer
    
It would be useful for the OP that you explain what did you change and why? And maybe explain where was he mistaken.. – Joaquín O Sep 2 at 22:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.