From 61e87095b4efc7d7b15fe0aabb6e41af61dd4465 Mon Sep 17 00:00:00 2001 From: Kelvin Ly Date: Thu, 11 May 2023 13:48:24 -0400 Subject: [PATCH] Implement the basic UI; everything's basically working except for reset --- wordle.htm | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++-- wordle.js | 17 ++++++--- 2 files changed, 114 insertions(+), 9 deletions(-) diff --git a/wordle.htm b/wordle.htm index be4342a..62a4f11 100644 --- a/wordle.htm +++ b/wordle.htm @@ -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 + } + }) }) })() @@ -78,6 +175,7 @@ var valid_words = null;
+
+

- +

- + diff --git a/wordle.js b/wordle.js index 52088ec..34b67ed 100644 --- a/wordle.js +++ b/wordle.js @@ -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++) {