1 ! Copyright (C) 2022 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs assocs.extras base91 colors
4 combinators hashtables io io.encodings.binary
5 io.encodings.string io.encodings.utf8 io.files io.styles kernel
6 literals math random ranges sequences sequences.extras sets
7 sorting splitting strings ;
12 CONSTANT: word-list $[
13 "vocab:wordlet/word-list.txt" binary file-contents
14 base91> utf8 decode "\n" split fast-set
19 TUPLE: wordlet-game secret-word chances guesses ;
21 : <wordlet-game> ( secret-word chances -- wordlet-game )
25 V{ } clone >>guesses ; inline
27 : guess>chars ( secret guess -- seq )
28 [ zip [ first2 = not ] filter keys [ 1string ] map ] 2keep
30 [ nip 1string ] [ = ] 2bi
34 [ [ swap remove-first ] [ COLOR: yellow ] bi ]
37 background associate 2array
40 : color>n ( color -- n )
43 { COLOR: yellow [ 2 ] }
44 { COLOR: green [ 3 ] }
47 : reamining-chars ( game -- chars )
48 [ secret-word>> ] [ guesses>> ] bi [
50 ] with map concat members
51 [ background of ] assoc-map
52 [ drop ] collect-value-by
53 [ [ color>n ] zip-with sort-values reverse first first ] assoc-map
54 CHAR: a CHAR: z [a..b] [ 1string COLOR: white ] { } map>assoc [ or ] assoc-merge ;
56 : print-remaining-chars ( game -- )
57 reamining-chars [ background associate format ] assoc-each nl ;
59 : print-guesses ( game -- )
60 [ secret-word>> ] [ guesses>> ] bi [
61 guess>chars [ format ] assoc-each nl
64 : read-guess ( -- guess )
66 readln >lower dup length 5 =
67 [ " needs to have 5 letters" append print read-guess ] unless
69 [ " not in the word list" append print read-guess ] unless ;
71 : check-winner? ( game -- ? )
72 [ secret-word>> ] [ guesses>> ?last ] bi = ;
74 : print-secret ( game color -- )
75 [ secret-word>> ] [ background associate ] bi* format nl ;
77 : maybe-stop? ( game -- ? )
78 [ guesses>> length ] [ chances>> ] bi >= ;
80 : play-wordlet ( game -- )
82 COLOR: red print-secret
86 [ print-remaining-chars ]
87 [ [ read-guess ] dip guesses>> push ]
90 [ COLOR: green print-secret ]
96 : play-random-wordlet-game ( -- )
97 "wordlet Started" print
98 word-list random 6 <wordlet-game> play-wordlet ;
100 MAIN: play-random-wordlet-game