Implement the basic UI; everything's basically working except for reset

This commit is contained in:
Kelvin Ly 2023-05-11 13:48:24 -04:00
parent 4c8c872c99
commit 61e87095b4
2 changed files with 114 additions and 9 deletions

View File

@ -57,9 +57,13 @@ var valid_words = null;
})();
const cur_status = [0, 0, 0, 0, 0];
var cur_guess = null;
var num_guess = 0;
(() => {
inited = false
window.addEventListener('load', () => {
window.addEventListener('load', () => {
document.getElementById('init').addEventListener('click', e => {
if (!inited) {
precompute_all()
@ -67,10 +71,103 @@ var valid_words = null;
})
document.getElementById('find').addEventListener('click', e => {
if (words_len(valid_words) == 1) {
status("the word is " + words[valid_words[0]])
}
const [idx, entropy] = calc_best_word(valid_words)
status("best word is " + words[idx] + " , " + entropy)
log("best word " + words[idx] + " , " + entropy)
})
document.getElementById('submit').addEventListener('click', e => {
const input_elem = document.getElementById('guess')
const input = input_elem.value.trim()
var idx = -1
for (var i = 0; i < words.length; i++) {
if (words[i] == input) {
idx = i
break
}
}
if (idx == -1) {
status(input + " is not a valid word")
return
}
cur_guess = input
num_guess++
const my_num_guess = num_guess
for (var i = 0; i < 5; i++) {
cur_status[i] = 0
}
const cur_word = document.getElementById('cur-word')
while (cur_word.firstChild) {
cur_word.removeChild(cur_word.lastChild)
}
for (var i = 0; i < 5; i++) {
const elem = document.createElement('span')
elem.textContent = input[i].toUpperCase()
elem.classList.add('letter')
elem.classList.add('notincluded')
const char_idx = i
elem.addEventListener('click', e => {
if (num_guess != my_num_guess) return
cur_status[char_idx]++
if (cur_status[char_idx] >= 3) {
cur_status[char_idx] = 0
}
elem.classList.remove('match')
elem.classList.remove('exists')
elem.classList.remove('notincluded')
switch (cur_status[char_idx]) {
case 0:
elem.classList.add('notincluded')
break
case 1:
elem.classList.add('match')
break
case 2:
elem.classList.add('exists')
}
})
cur_word.appendChild(elem)
}
})
document.getElementById('confirm').addEventListener('click', e => {
if (cur_guess != null) {
var nodes = []
const cur_word = document.getElementById('cur-word')
while (cur_word.firstChild) {
nodes.push(cur_word.firstChild)
cur_word.removeChild(cur_word.firstChild)
}
const div_wrapper = document.createElement('div')
for (var i = 0; i < nodes.length; i++) {
div_wrapper.appendChild(nodes[i])
}
document.getElementById('history').appendChild(div_wrapper)
var guess_idx = -1
for (var i = 0; i < words.length; i++) {
if (words[i] == cur_guess) {
guess_idx = i
break
}
}
if (guess_idx == -1) return
invalidate_words(valid_words, guess_idx, undump_match(cur_status))
log("guessed " + cur_guess + " " + words_len(valid_words) + " words left")
status("guessed " + cur_guess + " " + words_len(valid_words) + " words left")
cur_guess = null
}
})
})
})()
</script>
@ -78,6 +175,7 @@ var valid_words = null;
</head>
<body>
<div class="history" id="history">
<!--
<div>
<span class="letter match">W</span>
<span class="letter exists">E</span>
@ -85,14 +183,16 @@ var valid_words = null;
<span>R</span>
<span>Y</span>
</div>
-->
</div>
<div id="cur-word"></div>
<form>
<p>
<input minlength=5 maxlength=5 size=5></input>
<input id="guess" minlength=5 maxlength=5 size=5></input>
</p>
<p>
<input type=button id="init" value="Init search"></input>
<input type=button id="clear" value="Clear history"></input>
<input type=button id="clear" value="Reset game"></input>
<input type=button id="submit" value="Submit word"></input>
<input type=button id="confirm" value="Confirm word"></input>
<input type=button id="find" value="Find best"></input>

View File

@ -64,14 +64,16 @@ var all_matches_buffer = null
var all_matches = null
const precompute_all = (() => {
var idx = 0
var init_start = null
return () => {
const l = words.length
const start = Date.now()
if (idx == 0) {
all_matches_buffer = new ArrayBuffer(l*l)
all_matches = new Uint8Array(all_matches_buffer)
init_start = start
}
const start = Date.now()
for (; (Date.now() - start) < 50 && idx < l; idx++) {
for (; (Date.now() - start) < 40 && idx < l; idx++) {
for (var j = 0; j < l; j++) {
all_matches[idx*l + j] = compute_match(words[idx], words[j])
}
@ -81,10 +83,13 @@ const precompute_all = (() => {
}
if (idx >= l) {
const end = Date.now()
inited = true
console.log("precompute done, mem = " + all_matches.length/1024/1024 + " MB")
log("precompute done, mem = " + all_matches.length/1024/1024 + " MB")
status("precompute done, mem = " + all_matches.length/1024/1024 + " MB")
console.log("precompute done, mem = " + all_matches.length + " bytes")
log("precompute done, mem = " + all_matches.length + " bytes")
status("precompute done, mem = " + all_matches.length + " bytes")
log("took " + (end - init_start) + " ms")
console.log("took " + (end - init_start) + " ms")
} else {
setTimeout(precompute_all, 0)
}
@ -124,7 +129,7 @@ function calc_best_word(valid_words) {
var cur_best = -1
var best_entropy = -1
var cache = new Uint16Array(5*5*5*5*5)
const cache = new Uint16Array(5*5*5*5*5)
/*
for (var i = 0; i < valid_words.length; i++) {