From 37ade8158d993824240718d76e309b36b0d82540 Mon Sep 17 00:00:00 2001
From: Kelvin Ly <kelvin.ly1618@gmail.com>
Date: Thu, 11 May 2023 14:09:51 -0400
Subject: [PATCH] Implement a back function

---
 wordle.htm | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/wordle.htm b/wordle.htm
index 9c9ef96..f05c61a 100644
--- a/wordle.htm
+++ b/wordle.htm
@@ -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>