1 ! Copyright (C) 2022 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs assocs.extras colors
4 combinators hashtables io io.styles kernel math random ranges
5 sequences sequences.extras sets sorting strings
9 TUPLE: wordlet-game secret-word chances guesses ;
11 : <wordlet-game> ( secret-word chances -- wordlet-game )
15 V{ } clone >>guesses ; inline
17 : guess>chars ( secret guess -- seq )
18 [ zip [ first2 = not ] filter keys [ 1string ] map ] 2keep
20 [ nip 1string ] [ = ] 2bi
24 [ [ swap remove-first ] [ COLOR: yellow ] bi ]
27 background associate 2array
30 : color>n ( color -- n )
33 { COLOR: yellow [ 2 ] }
34 { COLOR: green [ 3 ] }
37 : reamining-chars ( game -- chars )
38 [ secret-word>> ] [ guesses>> ] bi [
40 ] with map concat members
41 [ background of ] assoc-map
42 [ first ] collect-value-by
43 [ [ color>n ] zip-with sort-values reverse first first ] assoc-map
44 CHAR: a CHAR: z [a..b] [ 1string COLOR: white ] { } map>assoc [ or ] assoc-merge ;
46 : print-remaining-chars ( game -- )
47 reamining-chars [ background associate format ] assoc-each nl ;
49 : print-guesses ( game -- )
50 [ secret-word>> ] [ guesses>> ] bi [
51 guess>chars [ format ] assoc-each nl
54 : read-guess ( -- guess )
56 readln >lower dup length 5 =
57 [ " needs to have 5 letters" append print read-guess ] unless
59 [ " not in the word list" append print read-guess ] unless ;
61 : check-winner? ( game -- ? )
62 [ secret-word>> ] [ guesses>> ?last ] bi = ;
64 : print-secret ( game color -- )
65 [ secret-word>> ] [ background associate ] bi* format nl ;
67 : maybe-stop? ( game -- ? )
68 [ guesses>> length ] [ chances>> ] bi >= ;
70 : play-wordlet ( game -- )
72 COLOR: red print-secret
76 [ print-remaining-chars ]
77 [ [ read-guess ] dip guesses>> push ]
80 [ COLOR: green print-secret ]
86 : play-random-wordlet-game ( -- )
87 "wordlet Started" print
88 word-list random 6 <wordlet-game> play-wordlet ;
90 MAIN: play-random-wordlet-game