function checkDirectionForWinner(grid, cell, turn, direction, discCountsByDirection) {
  // get the coordinates for the cell we're checking
  const column = cell.column + direction.column;
  const row = cell.row + direction.row;

  // make sure we're inside the grid
  if (column < 0 || column >= GRID_COLUMNS || row < 0 || row >= GRID_ROWS) {
    return false;
  }

  // cell is empty?
  const adjacentCellTurn = grid[column][row].usedAtTurn;
  if (adjacentCellTurn === -1) {
    return false;
  }

  // player is the same?
  if (turn % 2 === adjacentCellTurn % 2) {
    // increment the counts for the current direction
    discCountsByDirection[direction.direction]++;

    // we have a winner yet?
    if (discCountsByDirection[direction.direction] === DISCS_NEEDED_FOR_WIN) {
      return true
    }

    // recurse into this function with the adjacent cell as the new center going in the same direction
    return checkDirectionForWinner(grid, {column, row}, turn, direction, discCountsByDirection);
  }

  return false;
}
/**
 * Check the specified direction for a disc matching the current disc.
 *
 * @param grid
 * @param cell
 * @param turn
 * @param direction
 * @param discCountsByDirection
 * @returns {*}
 */
Check the specified direction for a disc matching the current disc.
Check the specified direction for a disc matching the current disc .
function checkDirectionForWinner(grid, cell, turn, direction, discCountsByDirection) {
  const column = cell.column + direction.column;
  const row = cell.row + direction.row;
  if (column < 0 || column >= GRID_COLUMNS || row < 0 || row >= GRID_ROWS) {
    return false;
  }
  const adjacentCellTurn = grid[column][row].usedAtTurn;
  if (adjacentCellTurn === -1) {
    return false;
  }
  if (turn % 2 === adjacentCellTurn % 2) {
    discCountsByDirection[direction.direction]++;
    if (discCountsByDirection[direction.direction] === DISCS_NEEDED_FOR_WIN) {
      return true
    }
    return checkDirectionForWinner(grid, {column, row}, turn, direction, discCountsByDirection);
  }
  return false;
}
function checkDirectionForWinner ( grid , cell , turn , direction , discCountsByDirection ) { const column = cell . column + direction . column ; const row = cell . row + direction . row ; if ( column < 0 || column >= GRID_COLUMNS || row < 0 || row >= GRID_ROWS ) { return false ; } const adjacentCellTurn = grid [ column ] [ row ] . usedAtTurn ; if ( adjacentCellTurn === - 1 ) { return false ; } if ( turn % 2 === adjacentCellTurn % 2 ) { discCountsByDirection [ direction . direction ] ++ ; if ( discCountsByDirection [ direction . direction ] === DISCS_NEEDED_FOR_WIN ) { return true } return checkDirectionForWinner ( grid , { column , row } , turn , direction , discCountsByDirection ) ; } return false ; }
Check the specified direction for a disc matching the current disc.
Check the specified direction for a disc matching the current disc .
// get the coordinates for the cell we're checking // make sure we're inside the grid // cell is empty? // player is the same? // increment the counts for the current direction // we have a winner yet? // recurse into this function with the adjacent cell as the new center going in the same direction