Implement a back function

This commit is contained in:
Kelvin Ly 2023-05-11 14:09:51 -04:00
parent ad3f7f0323
commit 37ade8158d
1 changed files with 30 additions and 1 deletions

View File

@ -57,13 +57,13 @@ var valid_words = null;
})()
var inited = false
const cur_status = [0, 0, 0, 0, 0]
var cur_guess = null
var num_guess = 0
var chosen_words = [];
(() => {
inited = false
window.addEventListener('load', () => {
document.getElementById('init').addEventListener('click', e => {
if (!inited) {
@ -94,6 +94,10 @@ var chosen_words = [];
})
document.getElementById('find').addEventListener('click', e => {
if (!inited) {
status("please initialize the solver first")
return
}
if (words_len(valid_words) == 1) {
status("the word is " + words[valid_words[0]])
log("the word is " + words[valid_words[0]])
@ -165,6 +169,10 @@ var chosen_words = [];
})
document.getElementById('confirm').addEventListener('click', e => {
if (!inited) {
status("please initialize the solver first")
return
}
if (cur_guess != null) {
var nodes = []
const cur_word = document.getElementById('cur-word')
@ -194,6 +202,26 @@ var chosen_words = [];
cur_guess = null
}
})
document.getElementById('back').addEventListener('click', e => {
if (chosen_words.length == 0) return
if (!inited) {
status("please initialize the solver first")
return
}
chosen_words.pop()
const history = document.getElementById('history')
history.removeChild(history.lastChild)
// replay the game up to this point
valid_words = gen_all_words_list()
for (var i = 0; i < chosen_words.length; i++) {
invalidate_words(valid_words, chosen_words[i][0], undump_match(chosen_words[i][1]))
}
const num_valid = words_len(valid_words)
log("removed last added word, " + num_valid + " words left")
status("removed last added word, " + num_valid + " words left")
})
})
})()
</script>
@ -217,6 +245,7 @@ var chosen_words = [];
</form>
<div id=status>loading words...</div>
log:
<!-- TODO show a sorted list of all the valid words or all the best words to guess with -->
<div id=log class=log></div>
</body>
</html>