1 ! Copyright (C) 2022 Doug Coleman.
2 ! See https://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 math.order random ranges sequences
7 sequences.extras sets sorting.specification 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 [ [ = ] [ drop 1string ] { } 2reject-map-as ] 2keep
30 [ nip 1string ] [ = ] 2bi
34 [ [ remove-first-of ] [ COLOR: yellow ] bi ]
37 background associate 2array
40 : color>n ( color -- n )
43 { COLOR: yellow [ 2 ] }
44 { COLOR: green [ 3 ] }
47 : remaining-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-with-spec 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 remaining-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/f )
68 [ " needs to have 5 letters" append print read-guess ] unless
70 [ " not in the word list" append print read-guess ] unless
73 : check-winner? ( game -- ? )
74 [ secret-word>> ] [ guesses>> ?last ] bi = ;
76 : print-secret ( game color -- )
77 [ secret-word>> ] [ background associate ] bi* format nl ;
79 : maybe-stop? ( game -- ? )
80 [ guesses>> length ] [ chances>> ] bi >= ;
82 : play-wordlet ( game -- )
84 COLOR: red print-secret
88 [ print-remaining-chars ]
89 [ [ read-guess ] dip guesses>> push ]
93 [ COLOR: green print-secret ]
96 "you gave up, the word was " write COLOR: red print-secret
102 : play-random-wordlet-game ( -- )
103 "Wordlet Started" print
104 word-list random 6 <wordlet-game> play-wordlet ;
106 MAIN: play-random-wordlet-game