Search code examples
javascriptarraysmatrixtypeerror

Search a positive gradient in a dynamic-sized 2d array


I need to search a positive & negative gradient in a 2d array for values, for example.

Positive gradient: bottom left > middle > top right

Negative gradient: top left > middle > bottom right

[
    ['x',  'x',  'o'],
    [null, null, 'o'],
    ['x', 'x',   null] 
]

I have 2 functions. First the value is found in a for loop, when found both of these functions run to search the positive & negative gradient of the maze and return true if a winning combination is found.

scanForNegativeGradientCombinationsworks, scanForPositiveGradientCombinations does not and ends in a typeError. I think the problem may be when reducing the rowIndex, I am pushing the game out of bounds, but im not sure.

const isDiagonalWinner = (rowCellValues, winCountCondition, player) => {

for(let rowIndex = 0; rowIndex < rowCellValues.length; rowIndex++){
    for(let columnIndex = 0; columnIndex < rowCellValues[rowIndex].length; columnIndex++){
        const cellValue = rowCellValues[rowIndex][columnIndex];
        if(cellValue === player.symbol) {
            console.log('initiating player scan for ', player.symbol, 'at', [rowIndex, columnIndex]);
            if(scanForNegativeGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
                return true
            } 

            if(scanForPositiveGradientCombinations(columnIndex, rowIndex, rowCellValues, player.symbol, winCountCondition)) {
                return true
            }
        }
    }
}

return null;
};

Above is the function that will call the following 2 functions.

const scanForNegativeGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while(rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length){
    if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
        counter++;
    }
    if(counter >= winCountCondition){
        return true;
    }
    rowIndex++;
    columnIndex++;
}
return false;
}


const scanForPositiveGradientCombinations= (columnIndex, rowIndex, rowCellValues, playerSymbol, winCountCondition) => {
let counter = 0;
while(rowIndex < rowCellValues.length && columnIndex < rowCellValues[rowIndex].length){
    if(rowCellValues[rowIndex][columnIndex] === playerSymbol){
        counter++;
    }
    if(counter >= winCountCondition){
        return true;
    }
    rowIndex--;
    columnIndex++;
}
return false;
}

Solution

  • function sequence(n) { return Array(n).fill().map((_,i)=>i) }
    
    function diagonalWin(board) {
      let d = board.length
      let seq = sequence(d)
      return board[0][0]!==null && seq.every(i=>board[i][i]===board[0][0]) ||
      board[0][d-1]!==null && seq.every(i=>board[i][d-i-1]===board[0][d-1])
    }
    
    console.log(diagonalWin([
      ['x',  'x',  'o'],
      [null, null, 'o'],
      ['x', 'x',   null]
    ]))
    
    console.log(diagonalWin([
      ['x',  'x',  'o'],
      [null, 'x', 'o'],
      ['x', 'x',  'x']
    ]))
    
    console.log(diagonalWin([
      ['x',  'x',  'o'],
      [null, 'o', 'o'],
      ['o', 'x',  'x']
    ]))