Skip to content

Instantly share code, notes, and snippets.

@thenickreynolds
Created August 17, 2015 02:20
Show Gist options
  • Save thenickreynolds/0b7b7825a15edb80a897 to your computer and use it in GitHub Desktop.
Save thenickreynolds/0b7b7825a15edb80a897 to your computer and use it in GitHub Desktop.
var trie = createTrie(dictionary);
function findWords(board) {
var count = 0;
var visited = [];
for (var x = 0; x < board.width; x++) {
visited[x] = []
for (var y = 0; y < board.height; y++) {
visited[x][y] = false;
}
}
for (var x = 0; x < board.width; x++) {
for (var y = 0; y < board.height; y++) {
count += findWordsAt(board, x, y, trie, visited);
}
}
return count;
}
function findWordsAt(board, x, y, node, visited) {
if (x < 0
|| x >= board.width
|| y < 0
|| y >= board.width
|| visited[x][y]
|| !node.hasChild(board.at(x, y))) {
return 0;
}
visited[x][y] = true;
var character = board.at(x, y);
var currentNode = node.getChild(character);
var wordCount = node.isLeaf ? 1 : 0;
for (var offX = -1; offX < 2; offX++) {
for (var offY = -1; offY < 2; offY++) {
if (offX == 0 && offY == 0) continue;
wordCount += findWordsAt(
board,
(x + offX + board.width) % board.width,
(y + offY + board.height) % board.height,
currentNode,
visited);
}
}
visited[x][y] = false;
return wordCount;
}
var boggleBoard = new BoggleBoard(
'happybirthdaylizhappybirthdaylizyouroneyearolderandstillverycoolnotnearlyasoldasnickandyourenotcatchingupbecausetimedoesnotworkthatwayhavefunsolvingthispuzzleihopeitspossible',
29,
6);
var count = findWords(boggleBoard);
alert(count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment