#! /usr/bin/env gforth \ A simple hjkl keyboard-driven proto-game in Forth 2012. See wander.c decimal 1 value seed \ this LCG random number generator assumes ≥32-bit Forth : minstd ( -- random ) seed 48271 m* 2147483647 sm/rem drop dup to seed ; 80 constant width 23 constant height create board width height * allot : .board ( -- ) board height 0 ?do dup width type cr width + loop drop ; : pos ( x y -- c-addr ) width * + board + ; : seed-board ( n -- ) 0 ?do [char] # minstd width height * mod board + c! loop ; time&date - - - - - to seed board width height * blank 512 seed-board 40 value dude-x 12 value dude-y : set-dude ( c -- ) dude-x dude-y pos c! ; : draw-dude [char] @ set-dude ; : erase-dude bl set-dude ; draw-dude : moved-dude ( Δx Δy -- x y ) dude-y + 0 max height 1- min swap dude-x + 0 max width 1- min swap ; : try-move ( Δx Δy -- ) moved-dude 2dup pos c@ bl = if erase-dude to dude-y to dude-x draw-dude else 2drop then ; : act ( c -- ) case \ Dispatch key command. [char] h of -1 0 try-move endof [char] l of 1 0 try-move endof [char] j of 0 1 try-move endof [char] k of 0 -1 try-move endof [char] q of drop 0 endof endcase ; : wander 1 begin dup while 0 0 at-xy .board ." [hjklq]" key act repeat ; page wander cr bye