3125a52331/forthlets/hangman/dict.retro

User picture

Commiter: Charles Childers

Author: Charles Childers

Revision: 3125a52331


File Size: 1.88 KB

(March 10, 2010 19:28 UTC) About 2 years ago

fix from marc

 
Show/hide line numbers
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Hangman for Retro Console                                    )
(  * System dictionary access.                                 )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Copyright [c] 2010, Marc Simpson                             )
( License: ISC                                                 )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )

include library/data/random-mwc.retro
include library/files.retro

( --[ Variables ]--------------------------------------------- )

' files open

: sys-dict s" /usr/share/dict/words" :r ;

variable dict                                         0 dict !
here 80 allot                               constant dict-word
                                            variable dict-size

( --[ Access ]------------------------------------------------ )

: open-dict  ( -h ) sys-dict fopen dup 0 =if
                    cr ." Error: can't open dictionary." then ;
: initialise ( -  ) dict @ 0 =if open-dict 0; dup
                    dict ! fsize dict-size ! then ;

: rand-off   ( -n ) dict-size @ >random ;
: rand-pos   ( -n ) rand-off dict @ fseek drop ;

: >line      ( -  ) repeat dict @ fread 10 =if ;then again ;

: readline   ( -  ) dict-word repeat dict @ over fread! drop
                    dup @ 10 =if 0 swap ! ;; else 1+ then again ;

: rand-word  ( -  ) rand-pos >line readline ;
: close-dict ( -  ) dict @ fclose 0 =if
                    ." Error: Can't close dictionary." then ;

( --[ Retrieval ]--------------------------------------------- )

: >lower     ( c-c' ) dup char: A char: Z within if 32 + then ;

: lowercase  ( $-$' ) repeat dup @ 0 =if drop ;then
                      dup @ >lower over ! 1+ again ;

: get-word   (  -$  ) rand-word dict-word lowercase dict-word ;

( ============================================================ )
' files shut