Compare commits
2 commits
c668b1e266
...
947ed37974
Author | SHA1 | Date | |
---|---|---|---|
|
947ed37974 | ||
|
af1c5ace72 |
|
@ -5,24 +5,31 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
gonanoid "github.com/matoous/go-nanoid"
|
||||||
"log"
|
"log"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var validWordRegex = regexp.MustCompile(`^[a-z]+$`)
|
||||||
|
|
||||||
type wordList struct {
|
type wordList struct {
|
||||||
Words map[int][]string `json:"words"`
|
Words map[int][]string `json:"words"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type shufflePattern struct {
|
type shufflePattern struct {
|
||||||
|
ID string `json:"id"`
|
||||||
Index map[int][]int `json:"index"`
|
Index map[int][]int `json:"index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dictFile := flag.String("dict", "./dict/en_GB.dic", "dictionary of word to prep")
|
dictFile := flag.String("dict", "./dict", "directory of dictionary of word to prep")
|
||||||
outDir := flag.String("out", "./site/assets/data", "output directory")
|
outDir := flag.String("out", "./site/assets/data", "output directory")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -32,14 +39,24 @@ func main() {
|
||||||
Words: make(map[int][]string),
|
Words: make(map[int][]string),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wordSet := make(map[string]bool)
|
||||||
if err := scanSuitableWords(*dictFile, func(word string) {
|
if err := scanSuitableWords(*dictFile, func(word string) {
|
||||||
if len(word) >= 4 && len(word) <= 6 {
|
w := strings.TrimSpace(word)
|
||||||
words.Words[len(word)] = append(words.Words[len(word)], word)
|
if !validWordRegex.MatchString(w) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(w) >= 4 && len(w) <= 6 {
|
||||||
|
wordSet[word] = true
|
||||||
}
|
}
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for w := range wordSet {
|
||||||
|
words.Words[len(w)] = append(words.Words[len(w)], w)
|
||||||
|
}
|
||||||
|
|
||||||
for k, word := range words.Words {
|
for k, word := range words.Words {
|
||||||
log.Printf("Found %d words of length %v", len(word), k)
|
log.Printf("Found %d words of length %v", len(word), k)
|
||||||
sort.Strings(word)
|
sort.Strings(word)
|
||||||
|
@ -54,7 +71,10 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate a shuffle pattern
|
// Generate a shuffle pattern
|
||||||
shp := shufflePattern{Index: make(map[int][]int)}
|
shp := shufflePattern{
|
||||||
|
ID: gonanoid.MustID(12),
|
||||||
|
Index: make(map[int][]int),
|
||||||
|
}
|
||||||
for k := range words.Words {
|
for k := range words.Words {
|
||||||
pattern := make([]int, len(words.Words[k]))
|
pattern := make([]int, len(words.Words[k]))
|
||||||
for i := range words.Words[k] {
|
for i := range words.Words[k] {
|
||||||
|
@ -80,7 +100,56 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func scanSuitableWords(dictFile string, withWord func(word string)) error {
|
func scanSuitableWords(dictDir string, withWord func(word string)) error {
|
||||||
|
if err := scanSuitableWordsFromWordListHTML(filepath.Join(dictDir, "oxford-word-list.htm"), withWord); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanSuitableWordsFromOxford3000(filepath.Join(dictDir, "The_Oxford_3000.txt"), withWord); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanSuitableWordsFromEnGB(filepath.Join(dictDir, "en_GB.dic"), withWord); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanSuitableWordsFromWordListHTML(dictFile string, withWord func(word string)) error {
|
||||||
|
f, err := os.Open(dictFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
dom, err := goquery.NewDocumentFromReader(f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dom.Find("table.t tbody tr td.t:nth-child(2)").Each(func(i int, s *goquery.Selection) {
|
||||||
|
withWord(s.Text())
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanSuitableWordsFromOxford3000(dictFile string, withWord func(word string)) error {
|
||||||
|
f, err := os.Open(dictFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(f)
|
||||||
|
for scanner.Scan() {
|
||||||
|
withWord(scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
return scanner.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func scanSuitableWordsFromEnGB(dictFile string, withWord func(word string)) error {
|
||||||
f, err := os.Open(dictFile)
|
f, err := os.Open(dictFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1,2 +1,9 @@
|
||||||
|
oxford-word-list.htm
|
||||||
|
Taken from https://www.oxfordwordlist.com/pages/report.asp
|
||||||
|
|
||||||
|
en_GB.dic
|
||||||
This is a Hunspell dictionary taken from this location:
|
This is a Hunspell dictionary taken from this location:
|
||||||
https://archive.netbsd.org/pub/pkgsrc-archive/distfiles/2019Q4/hunspell-dictionaries/en_GB-20061130/en_GB.zip
|
https://archive.netbsd.org/pub/pkgsrc-archive/distfiles/2019Q4/hunspell-dictionaries/en_GB-20061130/en_GB.zip
|
||||||
|
|
||||||
|
The_Oxford_3000.txt
|
||||||
|
https://github.com/sapbmw/The-Oxford-3000/blob/master/The_Oxford_3000.txt
|
3848
dict/The_Oxford_3000.txt
Normal file
3848
dict/The_Oxford_3000.txt
Normal file
File diff suppressed because it is too large
Load diff
9151
dict/oxford-word-list.htm
Normal file
9151
dict/oxford-word-list.htm
Normal file
File diff suppressed because it is too large
Load diff
7
go.mod
7
go.mod
|
@ -1,3 +1,10 @@
|
||||||
module lmika.dev/lmika/wordle-clone
|
module lmika.dev/lmika/wordle-clone
|
||||||
|
|
||||||
go 1.23.3
|
go 1.23.3
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/PuerkitoBio/goquery v1.10.2 // indirect
|
||||||
|
github.com/andybalholm/cascadia v1.3.3 // indirect
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 // indirect
|
||||||
|
golang.org/x/net v0.35.0 // indirect
|
||||||
|
)
|
||||||
|
|
73
go.sum
Normal file
73
go.sum
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
github.com/PuerkitoBio/goquery v1.10.2 h1:7fh2BdHcG6VFZsK7toXBT/Bh1z5Wmy8Q9MV9HqT2AM8=
|
||||||
|
github.com/PuerkitoBio/goquery v1.10.2/go.mod h1:0guWGjcLu9AYC7C1GHnpysHy056u9aEkUHwhdnePMCU=
|
||||||
|
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
||||||
|
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1 h1:aCjdvTyO9LLnTIi0fgdXhOPPvOHjpXN6Ik9DaNjIct4=
|
||||||
|
github.com/matoous/go-nanoid v1.5.1/go.mod h1:zyD2a71IubI24efhpvkJz+ZwfwagzgSO6UNiFsZKN7U=
|
||||||
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
|
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||||
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
|
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||||
|
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||||
|
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||||
|
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||||
|
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||||
|
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||||
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
|
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||||
|
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||||
|
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||||
|
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||||
|
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||||
|
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||||
|
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||||
|
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||||
|
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||||
|
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||||
|
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||||
|
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||||
|
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -7,7 +7,7 @@ import { WordSource } from "../models/words.js";
|
||||||
export default class extends Controller {
|
export default class extends Controller {
|
||||||
static targets = ["row", "playfield", "topMessage", "nextPuzzleButtons"];
|
static targets = ["row", "playfield", "topMessage", "nextPuzzleButtons"];
|
||||||
static outlets = ["overlay"];
|
static outlets = ["overlay"];
|
||||||
|
|
||||||
async connect() {
|
async connect() {
|
||||||
this._wordSource = new WordSource();
|
this._wordSource = new WordSource();
|
||||||
this._gameController = new GameController(this._wordSource);
|
this._gameController = new GameController(this._wordSource);
|
||||||
|
@ -16,32 +16,33 @@ export default class extends Controller {
|
||||||
|
|
||||||
this._buildPlayfield();
|
this._buildPlayfield();
|
||||||
}
|
}
|
||||||
|
|
||||||
tappedKey(key) {
|
tappedKey(key) {
|
||||||
console.log(`Key ${key} was tapped via outliet`);
|
console.log(`Key ${key} was tapped via outliet`);
|
||||||
|
|
||||||
this._addLetter(key);
|
this._addLetter(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addLetter(letter) {
|
_addLetter(letter) {
|
||||||
if (this._activeRowIndex < 0) {
|
if (this._activeRowIndex < 0) {
|
||||||
return;
|
return;
|
||||||
} else if (this._activeLetter >= this._gameController.wordLength()) {
|
} else if (this._activeLetter >= this._gameController.wordLength()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let rowElem = this.rowTargets[this._activeRowIndex];
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
||||||
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
||||||
|
|
||||||
colElem.innerText = letter.toUpperCase();
|
colElem.innerText = letter.toUpperCase();
|
||||||
|
colElem.classList.remove("hint");
|
||||||
|
|
||||||
this._activeLetter += 1;
|
this._activeLetter += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
enterGuess() {
|
enterGuess() {
|
||||||
if (this._activeLetter >= this._gameController.wordLength()) {
|
if (this._activeLetter >= this._gameController.wordLength()) {
|
||||||
let rowElem = this.rowTargets[this._activeRowIndex];
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
||||||
this._verifyGuess(rowElem);
|
this._verifyGuess(rowElem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ export default class extends Controller {
|
||||||
let word = this._gameController.currentWord();
|
let word = this._gameController.currentWord();
|
||||||
window.open(`https://www.ecosia.org/search?q=define+${word}`, "_blank");
|
window.open(`https://www.ecosia.org/search?q=define+${word}`, "_blank");
|
||||||
}
|
}
|
||||||
|
|
||||||
tappedBackspace() {
|
tappedBackspace() {
|
||||||
if (this._activeLetter == 0) {
|
if (this._activeLetter == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -61,20 +62,28 @@ export default class extends Controller {
|
||||||
let rowElem = this.rowTargets[this._activeRowIndex];
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
||||||
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
||||||
|
|
||||||
colElem.innerText = "";
|
let colHint = colElem.dataset["hint"];
|
||||||
|
if (colHint) {
|
||||||
|
colElem.classList.add("hint");
|
||||||
|
colElem.innerText = colHint;
|
||||||
|
} else {
|
||||||
|
colElem.innerText = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_verifyGuess(rowElem) {
|
_verifyGuess(rowElem) {
|
||||||
let guessedWord = Array.from(rowElem.querySelectorAll("span")).map((x) => x.innerText).join("");
|
let guessedWord = Array.from(rowElem.querySelectorAll("span")).map((x) => x.innerText).join("");
|
||||||
console.log("The guessed word is: " + guessedWord);
|
console.log("The guessed word is: " + guessedWord);
|
||||||
|
|
||||||
let results = this._gameController.checkGuess(guessedWord);
|
let results = this._gameController.submitGuess(guessedWord);
|
||||||
|
|
||||||
switch (results.guessResult) {
|
switch (results.guessResult) {
|
||||||
case GUESS_RESULT.FOUL:
|
case GUESS_RESULT.FOUL:
|
||||||
this.overlayOutlet.showMessage("Not a valid word");
|
this.overlayOutlet.showMessage("Not in dictionary");
|
||||||
|
|
||||||
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
|
let newRow = this._buildPlayfieldRow(this._gameController.wordLength());
|
||||||
|
this._showHints(newRow);
|
||||||
|
rowElem.replaceWith(newRow);
|
||||||
this._activeLetter = 0;
|
this._activeLetter = 0;
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent("guessResults", {
|
window.dispatchEvent(new CustomEvent("guessResults", {
|
||||||
|
@ -86,18 +95,16 @@ export default class extends Controller {
|
||||||
|
|
||||||
this._activeRowIndex += 1;
|
this._activeRowIndex += 1;
|
||||||
if (this._activeRowIndex >= this._gameController.guesses()) {
|
if (this._activeRowIndex >= this._gameController.guesses()) {
|
||||||
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
|
this._revealAnswer();
|
||||||
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
|
||||||
} else {
|
} else {
|
||||||
this._activeLetter = 0;
|
this._activeLetter = 0;
|
||||||
|
this._showHints(this.rowTargets[this._activeRowIndex], results.markers);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case GUESS_RESULT.WIN:
|
case GUESS_RESULT.WIN:
|
||||||
this._colorizeRow(rowElem, results);
|
this._colorizeRow(rowElem, results);
|
||||||
|
this._showWin();
|
||||||
this.topMessageTarget.innerText = "Hooray! You did it.";
|
|
||||||
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,42 +117,83 @@ export default class extends Controller {
|
||||||
this.overlayOutlet.showMessage("No more words available.");
|
this.overlayOutlet.showMessage("No more words available.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_showWin() {
|
||||||
|
this.topMessageTarget.innerText = "Hooray! You did it.";
|
||||||
|
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
||||||
|
}
|
||||||
|
|
||||||
|
_revealAnswer() {
|
||||||
|
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
|
||||||
|
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
||||||
|
}
|
||||||
|
|
||||||
_buildPlayfield() {
|
_buildPlayfield() {
|
||||||
let rows = this._gameController.guesses();
|
let rows = this._gameController.guesses();
|
||||||
let wordLength = this._gameController.wordLength();
|
let wordLength = this._gameController.wordLength();
|
||||||
|
let {currentGuesses, wasWin} = this._gameController.currentState();
|
||||||
this._activeRowIndex = 0;
|
|
||||||
this._activeLetter = 0;
|
this._activeRowIndex = currentGuesses.length;
|
||||||
|
this._activeLetter = 0;
|
||||||
let newRows = [];
|
|
||||||
for (let r = 0; r < rows; r++) {
|
|
||||||
newRows.push(this._buildPlayfieldRow(wordLength));
|
|
||||||
}
|
|
||||||
|
|
||||||
this.playfieldTarget.replaceChildren.apply(this.playfieldTarget, newRows);
|
|
||||||
|
|
||||||
this.topMessageTarget.innerHTML = " "
|
|
||||||
this.nextPuzzleButtonsTarget.classList.add("hide");
|
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent("resetKeyColors"));
|
window.dispatchEvent(new CustomEvent("resetKeyColors"));
|
||||||
|
|
||||||
|
let newRows = [];
|
||||||
|
for (let r = 0; r < rows; r++) {
|
||||||
|
let currentGuess = null;
|
||||||
|
let currentGuessResults = null;
|
||||||
|
|
||||||
|
if (r < currentGuesses.length) {
|
||||||
|
currentGuess = currentGuesses[r];
|
||||||
|
currentGuessResults = this._gameController.checkGuess(currentGuess);
|
||||||
|
}
|
||||||
|
|
||||||
|
newRows.push(this._buildPlayfieldRow(wordLength, currentGuess, currentGuessResults));
|
||||||
|
if (currentGuessResults) {
|
||||||
|
window.dispatchEvent(new CustomEvent("guessResults", {
|
||||||
|
detail: currentGuessResults
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wasWin) {
|
||||||
|
this._showWin();
|
||||||
|
} else if (currentGuesses.length >= rows) {
|
||||||
|
// User has already used up all their guesses so just show the results;
|
||||||
|
this._revealAnswer();
|
||||||
|
} else {
|
||||||
|
this._showHints(newRows[currentGuesses.length]);
|
||||||
|
|
||||||
|
this.topMessageTarget.innerHTML = " "
|
||||||
|
this.nextPuzzleButtonsTarget.classList.add("hide");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.playfieldTarget.replaceChildren.apply(this.playfieldTarget, newRows);
|
||||||
}
|
}
|
||||||
|
|
||||||
_buildPlayfieldRow(wordLength) {
|
_buildPlayfieldRow(wordLength, currentGuess, currentGuessResults) {
|
||||||
let divElem = document.createElement("div");
|
let divElem = document.createElement("div");
|
||||||
divElem.classList.add("row");
|
divElem.classList.add("row");
|
||||||
divElem.setAttribute("data-playfield-target", "row");
|
divElem.setAttribute("data-playfield-target", "row");
|
||||||
|
|
||||||
for (let c = 0; c < wordLength; c++) {
|
for (let c = 0; c < wordLength; c++) {
|
||||||
let letterSpan = document.createElement("span");
|
let letterSpan = document.createElement("span");
|
||||||
|
if (currentGuess) {
|
||||||
|
letterSpan.innerText = currentGuess[c].toUpperCase();
|
||||||
|
}
|
||||||
divElem.appendChild(letterSpan);
|
divElem.appendChild(letterSpan);
|
||||||
}
|
}
|
||||||
return divElem;
|
|
||||||
|
if (currentGuess) {
|
||||||
|
this._colorizeRow(divElem, currentGuessResults);
|
||||||
|
}
|
||||||
|
|
||||||
|
return divElem;
|
||||||
}
|
}
|
||||||
|
|
||||||
_colorizeRow(row, results) {
|
_colorizeRow(row, results) {
|
||||||
let markers = results.markers;
|
let markers = results.markers;
|
||||||
|
|
||||||
for (let i = 0; i < this._gameController.wordLength(); i++) {
|
for (let i = 0; i < this._gameController.wordLength(); i++) {
|
||||||
switch (markers[i]) {
|
switch (markers[i]) {
|
||||||
case MARKERS.RIGHT_POS:
|
case MARKERS.RIGHT_POS:
|
||||||
|
@ -153,15 +201,28 @@ export default class extends Controller {
|
||||||
break;
|
break;
|
||||||
case MARKERS.RIGHT_CHAR:
|
case MARKERS.RIGHT_CHAR:
|
||||||
row.children[i].classList.add("right-char");
|
row.children[i].classList.add("right-char");
|
||||||
break;
|
break;
|
||||||
case MARKERS.MISS:
|
case MARKERS.MISS:
|
||||||
row.children[i].classList.add("miss");
|
row.children[i].classList.add("miss");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.dispatchEvent(new CustomEvent("guessResults", {
|
window.dispatchEvent(new CustomEvent("guessResults", {
|
||||||
detail: results
|
detail: results
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_showHints(row) {
|
||||||
|
let hint = this._gameController.showHint();
|
||||||
|
|
||||||
|
for (let i = 0; i < this._gameController.wordLength(); i++) {
|
||||||
|
if (hint[i]) {
|
||||||
|
let colElem = row.children[i];
|
||||||
|
colElem.classList.add("hint");
|
||||||
|
colElem.innerText = hint[i].toUpperCase();
|
||||||
|
colElem.dataset["hint"] = hint[i].toUpperCase();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -18,7 +18,17 @@ class ProgressionState {
|
||||||
return JSON.parse(prog);
|
return JSON.parse(prog);
|
||||||
}
|
}
|
||||||
|
|
||||||
prog = {wordLength: 4, wordIndex: {"4": 0, "5": 0, "6": 0}};
|
this.clearProgression("");
|
||||||
|
return prog;
|
||||||
|
}
|
||||||
|
|
||||||
|
clearProgression(shuffleId) {
|
||||||
|
let prog = {
|
||||||
|
shuffleId: shuffleId,
|
||||||
|
wordLength: 4,
|
||||||
|
wordIndex: {"4": 0, "5": 0, "6": 0},
|
||||||
|
currentGuesses: []
|
||||||
|
};
|
||||||
localStorage.setItem('progression', JSON.stringify(prog));
|
localStorage.setItem('progression', JSON.stringify(prog));
|
||||||
return prog;
|
return prog;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +47,11 @@ export class GameController {
|
||||||
async start() {
|
async start() {
|
||||||
let prog = this._progressionState.getProgression();
|
let prog = this._progressionState.getProgression();
|
||||||
|
|
||||||
|
if (await this._wordSource.needToResetProgression(prog)) {
|
||||||
|
console.log("Clearing patten shuffle id")
|
||||||
|
prog = this._progressionState.clearProgression(await this._wordSource.getPattenShuffleID());
|
||||||
|
}
|
||||||
|
|
||||||
this._currentWord = await this._wordSource.getCurrentWord(prog);
|
this._currentWord = await this._wordSource.getCurrentWord(prog);
|
||||||
this._guesses = Math.max(this._currentWord.length, 5) + 1;
|
this._guesses = Math.max(this._currentWord.length, 5) + 1;
|
||||||
|
|
||||||
|
@ -53,6 +68,17 @@ export class GameController {
|
||||||
return this._guesses;
|
return this._guesses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentState() {
|
||||||
|
let prog = this._progressionState.getProgression();
|
||||||
|
if (!prog || !prog.currentGuesses) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
wasWin: prog.wasWin,
|
||||||
|
currentGuesses: prog.currentGuesses
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
currentWord() {
|
currentWord() {
|
||||||
this._checkHasStarted();
|
this._checkHasStarted();
|
||||||
return this._currentWord;
|
return this._currentWord;
|
||||||
|
@ -63,14 +89,47 @@ export class GameController {
|
||||||
let prog = this._progressionState.getProgression();
|
let prog = this._progressionState.getProgression();
|
||||||
prog.wordIndex[prog.wordLength + ""] += 1;
|
prog.wordIndex[prog.wordLength + ""] += 1;
|
||||||
prog.wordLength = (((Math.random() * 23) | 0) / 10 | 0) + 4;
|
prog.wordLength = (((Math.random() * 23) | 0) / 10 | 0) + 4;
|
||||||
|
prog.currentGuesses = [];
|
||||||
|
prog.wasWin = false;
|
||||||
this._progressionState.saveProgression(prog);
|
this._progressionState.saveProgression(prog);
|
||||||
|
|
||||||
this._currentWord = await this._wordSource.getCurrentWord(prog);
|
this._currentWord = await this._wordSource.getCurrentWord(prog);
|
||||||
this._guesses = Math.max(this._currentWord.length, 5) + 1;
|
this._guesses = Math.max(this._currentWord.length, 5) + 1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showHint() {
|
||||||
|
let hints = new Array(this._currentWord.length);
|
||||||
|
let priorGuesses = this._progressionState.getProgression().currentGuesses;
|
||||||
|
|
||||||
|
hints[0] = this._currentWord[0];
|
||||||
|
for (let i = 1; i < this._currentWord.length; i++) {
|
||||||
|
for (let guess of priorGuesses) {
|
||||||
|
if (guess[i] === this._currentWord[i]) {
|
||||||
|
hints[i] = this._currentWord[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hints;
|
||||||
|
}
|
||||||
|
|
||||||
|
submitGuess(guess) {
|
||||||
|
guess = guess.toLowerCase();
|
||||||
|
|
||||||
|
let results = this.checkGuess(guess);
|
||||||
|
|
||||||
|
// Add this guess to the progression state
|
||||||
|
if (results.guessResult !== GUESS_RESULT.FOUL) {
|
||||||
|
let prog = this._progressionState.getProgression();
|
||||||
|
prog.currentGuesses.push(guess);
|
||||||
|
prog.wasWin = results.guessResult === GUESS_RESULT.WIN;
|
||||||
|
this._progressionState.saveProgression(prog);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
checkGuess(guess) {
|
checkGuess(guess) {
|
||||||
this._checkHasStarted();
|
this._checkHasStarted();
|
||||||
|
|
||||||
|
@ -141,7 +200,7 @@ export class GameController {
|
||||||
} else {
|
} else {
|
||||||
guessResult = GUESS_RESULT.MISS;
|
guessResult = GUESS_RESULT.MISS;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
guessResult: guessResult,
|
guessResult: guessResult,
|
||||||
hits: hits,
|
hits: hits,
|
||||||
|
|
|
@ -33,6 +33,15 @@ export class WordSource {
|
||||||
|
|
||||||
return binSearch(list, word);
|
return binSearch(list, word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async needToResetProgression(prog) {
|
||||||
|
await this._fetchAllWordsIfNecessary();
|
||||||
|
return !prog || !prog.shuffleId || this._pattern.id !== prog.shuffleId;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getPattenShuffleID() {
|
||||||
|
return this._pattern.id;
|
||||||
|
}
|
||||||
|
|
||||||
async getCurrentWord(prog) {
|
async getCurrentWord(prog) {
|
||||||
let words = await this._fetchAllWordsIfNecessary();
|
let words = await this._fetchAllWordsIfNecessary();
|
||||||
|
@ -44,15 +53,6 @@ export class WordSource {
|
||||||
return words.words[wordLengthKey][idx];
|
return words.words[wordLengthKey][idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
// async nextWord() {
|
|
||||||
// if (this._currentWord >= this._words.length - 1) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// this._currentWord += 1;
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
async _fetchAllWordsIfNecessary() {
|
async _fetchAllWordsIfNecessary() {
|
||||||
if (this._wordData) {
|
if (this._wordData) {
|
||||||
return this._wordData;
|
return this._wordData;
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
:root {
|
:root {
|
||||||
|
--color-hint: #aaa;
|
||||||
|
|
||||||
--color-no-letter-bg: #666;
|
--color-no-letter-bg: #666;
|
||||||
--color-no-letter-fg: #fff;
|
--color-no-letter-fg: #fff;
|
||||||
|
|
||||||
|
@ -150,6 +152,10 @@ div.playfield div.row span {
|
||||||
margin: 5px;
|
margin: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.playfield div.row span.hint {
|
||||||
|
color: var(--color-hint);
|
||||||
|
}
|
||||||
|
|
||||||
div.playfield div.row span.right-pos {
|
div.playfield div.row span.right-pos {
|
||||||
background: var(--color-right-letter-bg);
|
background: var(--color-right-letter-bg);
|
||||||
color: var(--color-right-letter-fg);
|
color: var(--color-right-letter-fg);
|
||||||
|
|
Loading…
Reference in a new issue