Boggle solver
Type all the words in the boggle board into the input. Solves for English words up to 7 letters long. based off github https://github.com/sds/boggle-solver
HTML
<div id="loading-message">
Loading
</div>
<input id="letters" value="abcd" type="text"/><br/>
(optional) target length: <input id="length" value=""/><br/>
<input id="go" value="go" type="button" />
<div id="result">
</div>
CSS
.word {
float: left;
width: 100px;
}
JavaScript
function solve(dict) {
let letters = $('#letters').val()
let length = $('#length').val()
let words = []
let visited = []
let charStack = []
function findWords(pos) {
if(visited[pos] || pos < 0 || pos >= letters.length) return
charStack.push(letters.charAt(pos))
// keep a list of which positions have been visited
// so we don't get duplicate letters
visited[pos] = true
for (var dx = -letters.length; dx <= letters.length; dx++) {
var c = pos + dx;
if (c < 0 || c >= letters.length || dx == 0) continue;
findWords(c);
}
if(length == '' || (length && length != '' && charStack.length == length)) {
let s = ""
for (let i = 0; i < charStack.length; i++) {
s = s + charStack[i].toLowerCase()
}
// check the dictionary for the word
if(dict[s]) {
words.push(s)
}
}
visited[pos] = false
charStack.pop()
}
for (var i = 0; i < letters.length; i++) {
findWords(i)
}
return words
}
$(document).ready(() => {
$.get('https://raw.githubusercontent.com/sds/boggle-solver/master/data/length-up-to-7.txt')
.success(function (data) {
// set up dictionary
let dict = []
var wds = data.split('\n');
for (var i = wds.length - 1; i >= 0; i--) {
// add words to dictionary array based on value
// so we can access them without looping
dict[wds[i].toLowerCase()] = true;
}
$('#loading-message').hide();
$('#go').click(() => {
let words = solve(dict)
words.sort(function(a, b){
// sort by length
return b.length - a.length
})
let content = ''
$.each(words, function(index, val) {
content += '<div class="word">' + val + '</div>'
});
$('#result').html(content)
})
})
.error(function () {
$('#loading-message').text("There was a problem loading the dictionary");
})
})