DEV Community

loading...

Discussion on: Daily Challenge #92 - Boggle Board

Collapse
erezwanderman profile image
erezwanderman

TypeScript:

const isValid = (board: string[][], guess: string) => {
  const checkGuess = (biggerBoard: string[][], guess: string, coords: [number, number]) => {
    if (guess === '') return true;
    if (biggerBoard[coords[0]][coords[1]] !== guess[0]) return false;

    const temp = biggerBoard[coords[0]][coords[1]];
    biggerBoard[coords[0]][coords[1]] = '*';
    const answer = [
        [coords[0] - 1, coords[1] - 1],
        [coords[0] - 1, coords[1]    ],
        [coords[0] - 1, coords[1] + 1],
        [coords[0]    , coords[1] - 1],

        [coords[0]    , coords[1] + 1],
        [coords[0] + 1, coords[1] - 1],
        [coords[0] + 1, coords[1]    ],
        [coords[0] + 1, coords[1] + 1]
    ].some(([r, c]) => checkGuess(biggerBoard, guess.substring(1), [r, c]));
    biggerBoard[coords[0]][coords[1]] = temp;
    return answer;
  };
  // Create a padded array
  const biggerBoard = Array(board.length + 2);
  biggerBoard[0] = biggerBoard[board.length + 1] = Array(board[0].length + 2).fill(undefined);
  board.forEach((row, i) => biggerBoard[i + 1] = [undefined, ...row, undefined]);

  const coords = [].concat(...biggerBoard.map((row, r) => row.map((val, c) => [r, c])));

  return coords.some(([r, c]) => checkGuess(biggerBoard, guess, [r, c]));
}

const testBoard = [
  ["I","L","A","W"],
  ["B","N","G","E"],
  ["I","U","A","O"],
  ["A","S","R","L"]
];

const testWords = ['BINGO', 'LINGO', 'ILNBIA', 'BUNGIE', 'BINS', 'SINUS'];
for (const word of testWords) {
  console.log(word, isValid(testBoard, word));
}
Collapse
erezwanderman profile image
erezwanderman

Shorter ver:

const isValid = (board: string[][], guess: string, prevCoords?: [number, number]) => {
  if (guess === '') return true;
  if (!prevCoords) {
    const coords = [].concat(...board.map((row, r) => row.map((val, c) => [r, c])));
    return coords.some(c => isValid(board, guess, c));
  } else {
    if (board[prevCoords[0]][prevCoords[1]] !== guess[0]) return false;

    const temp = board[prevCoords[0]][prevCoords[1]];
    board[prevCoords[0]][prevCoords[1]] = '*';
    const answer = [
        [prevCoords[0] - 1, prevCoords[1] - 1],
        [prevCoords[0] - 1, prevCoords[1]    ],
        [prevCoords[0] - 1, prevCoords[1] + 1],
        [prevCoords[0]    , prevCoords[1] - 1],

        [prevCoords[0]    , prevCoords[1] + 1],
        [prevCoords[0] + 1, prevCoords[1] - 1],
        [prevCoords[0] + 1, prevCoords[1]    ],
        [prevCoords[0] + 1, prevCoords[1] + 1]
    ]
    .filter(([r, c]) => r >= 0 && c >= 0 && r < board.length && c < board[0].length)
    .some(([r, c]) => isValid(board, guess.substring(1), [r, c]));
    board[prevCoords[0]][prevCoords[1]] = temp;
    return answer;
  }
}

const testBoard = [
  ["I","L","A","W"],
  ["B","N","G","E"],
  ["I","U","A","O"],
  ["A","S","R","L"]
];

const testWords = ['BINGO', 'LINGO', 'ILNBIA', 'BUNGIE', 'BINS', 'SINUS'];
for (const word of testWords) {
  console.log(word, isValid(testBoard, word));
}