examples:word_scrambler
Der Wordscrambler lief in gforth. Das Beispiel am Ende der Codesequenz zeigt den Gebrauch.
\ word scrambler based on random shuffle vocabulary scrambler scrambler definitions variable seed base @ hex 10450405 constant generator base ! : rnd ( -- n ) seed @ generator UM* DROP 1+ DUP seed ! ; : random ( n -- 0..n-1 ) rnd UM* NIP ; : init ( -- ) TIME&DATE 12 * + 31 * + 24 * + 60 * + 60 * + seed ! ; init : c>< ( c-addr1 c-addr2 -- ) \ character exchange 2dup c@ ( c-addr1 c-addr2 c-addr-1 c2 ) swap c@ ( c-addr1 c-addr2 c2 c1 ) rot c! ( c-addr1 c2 ) swap c! ; : cshuffle ( c-addr n -- ) \ shuffle Durstenfeld/Knuth BEGIN ?dup WHILE ( c-addr i ) 2dup 1- chars + >r 2dup random chars + r> c>< 1- REPEAT drop ; : scramble-word2 ( c-addr len -- ) \ some case handling included. dup 4 < IF 2drop exit THEN dup 4 = IF over char+ dup char+ c>< 2drop exit THEN 2 - swap char+ swap cshuffle ; : scramble ( <word> -- ) cr BEGIN bl word count dup WHILE ( c-addr len ) 2dup scramble-word2 type space REPEAT 2drop ; words scramble sah ein knab ein roeslein stehen cr cr \ study random behavior, execute serveral times. \ : xxx ( -- ) 0 10 do cr i . i random . -1 +loop ; \ scramble (.) (.O) (.:O) (.:=O) (.:=#O) (.:=#*O)
examples/word_scrambler.txt · Zuletzt geändert: 2013-06-06 21:26 von 127.0.0.1