Skip to content Skip to sidebar Skip to footer

Javascript - Connect Four Game Validation

I have the following test multidimensional array that mirrors a standard Connect Four gameboard: var board = [['-','-','-','-','-','-','-'], ['-','-','-','-','-','-','

Solution 1:

The for loops, like:

for (row=col=0; row<3 && col<7; row++, col++) {

will iterate over (0,0),(1,1),(2,2). What you want is (0,0),(0,1),(0,2),...,(1,0),(1,1),(1,2),.... To achieve this you must use nested loops:

for (row = 0; row<3; row++) {
    for (col = 0; col<7; col++) {
        // do that check
    }
}

Solution 2:

Use two nested loops and check each of the four directions if you have to.

var board = [['-','-','-','-','-','-','-'],
             ['-','-','-','-','-','-','-'],
             ['-','-','-','R','R','R','R'],
             ['-','-','-','Y','Y','R','Y'],
             ['-','-','-','Y','R','Y','Y'],
             ['-','-','Y','Y','R','R','R']];

functionfourTogether(a,b,c,d) {
   return (a != '-') && (a == b) && (a == c) && (a == d);
}

functionconnectFour(board) {
  var bl = board.length, bw = board[0].length;

  // loop through the whole board once not a bunch of timesfor (var row = 0; row < bl; row++) {
    for (var col = 0; col < bw; col++) {
      var sq = board[row][col];
      
      // check right if we have toif (col < bw - 3 &&
          fourTogether(sq, board[row][col+1], board[row][col+2], board[row][col+3])) {
        return sq;
      }
      // check down if we have toif (row < bl - 3 &&
          fourTogether(sq, board[row+1][col], board[row+2][col], board[row+3][col])) {
        return sq;
      }
      // down rightif (row < bl - 3 && col < bw - 3 &&
          fourTogether(sq, board[row+1][col+1], board[row+2][col+2], board[row+3][col+3])) {
        return sq;
      }
      // down leftif (row < bl - 3 && col > 2 && 
          fourTogether(sq, board[row+1][col-1], board[row+2][col-2], board[row+3][col-3])) {
        return sq;
      }
    }      
  } 
  //board.indexOf('-') > -1 ? return 'in progress' : return 'draw'; //?????return"no winner";
}
alert(connectFour(board));

Solution 3:

I would solve this a bit differently to you..

First, set up functions to easily get the lines to test

functionrow(board, i) {
    return board[i].join('');
}
functioncol(board, j) {
    return board.map(e => e[j]).join('');
}
functiondiagDown(board, i) {
    return board.map((e, j) => e[i - board.length + j] || '').join('');
}
functiondiagUp(board, i) {
    return board.slice(0).reverse().map((e, j) => e[i - board.length + j] || '').join('');
}

(If you want to see what is going on with the diagonal ones try playing with a few values in the console and you'll see how it's doing the mapping)

Now iterate over valid lines

function whoWon(board) {
    var i, s, r = 'RRRR', y = 'YYYY';
    // rowsfor (i = 0; i < board.length; ++i) {
        s = row(board, i);
        if (s.indexOf(r)) return'R';
        if (s.indexOf(y)) return'Y';
    }
    // colsfor (i = 0; i < board[0].length; ++i) {
        s = col(board, i);
        if (s.indexOf(r)) return'R';
        if (s.indexOf(y)) return'Y';
    }
    // diagonalsfor (i = 4; i <= board.length + board[0].length - 4; ++i) {
        s = diagDown(board, i);
        if (s.indexOf(r)) return'R';
        if (s.indexOf(y)) return'Y';
        s = diagUp(board, i);
        if (s.indexOf(r)) return'R';
        if (s.indexOf(y)) return'Y';
    }
    return'-';
}

Now have

whoWon(board); // "R"

Also note

functionisDraw(board) {
    return board[0].every(e => e !== '-');
}

Post a Comment for "Javascript - Connect Four Game Validation"