\ playspace.fs - Stars game play space
\ +JMJ 2013 David Meyer <[email protected]>

5 constant N \ Play space linear extent (NxNxN cube)
N dup * constant N^2 \ value NxN
N^2 N * constant N^3 \ value NxNxN

: in-playspace? ( n -- f )
   \ Is position index n within the defined play space?
   dup 0>=
   swap N^3 <
   and
;

: x ( n1 -- n2 )
   \ Return x coordinate n2 of position index n1.
   N mod
;

: y ( n1 -- n2 )
   \ Return y coordinate n2 of position index n1.
   N^2 mod N /
;

: z ( n1 -- n2 )
   \ Return z coordinate n2 of position index n1.
   N^2 /
;

: valid-move? ( n1 n2 -- f )
   \ Is a move from position index n1 to n2 valid according to game rules?
   2dup in-playspace?
   swap in-playspace?
   and                       \ n1 and n2 are in playspace
   rot rot 2dup x swap x - abs
   rot rot 2dup y swap y - abs
   rot rot      z swap z - abs
   + + 1 =                   \ move is single space in 1 direction
   and
;

: test ( -- )
   \ Test driver

   N^3 0 u+do
       cr i dup . [char] : emit space
       N^3 0 u+do
           dup i valid-move? if
               i .
           then
       loop
   loop
   cr
;