Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #24592

Request for comments on a first forth program

From Christian Kellermann <ckeen@pestilenz.org>
Newsgroups comp.lang.forth
Subject Request for comments on a first forth program
Date 2013-07-19 11:39 +0200
Organization A noiseless patient Spider
Message-ID <87wqomga80.fsf@pestilenz.org> (permalink)

Show all headers | View raw


Dear group readers,

I have been re-reading my books on Forth lately and decided to get into
learning it finally. One of the first results have been attached to this
article and I would like to hear your criticism about it. (If this is a
faux pas on this list please ignore this request).

It's a small and silly implementation of the BSD game robots. You ("@")
have to escape some robots ("R") and win by luring them into holes
("*").

I have used GNU Forth to get started, mostly because I don't know any
forth system well enough (besides a little dabbling with amforth) to
judge any of the existing systems.

Things that did surprise me:

* Working with the stack is not hard at all
* Debugging Forth words has been easier than anticipated. I did not use
  the gforth debugging facilities though.
* A bad choice of data structures is punished more heavily in forth due
  to unnecessary stack juggling involved than in other programming
  languages.

What I don't like in my program:

* The word names are probably way too verbose for forthers. Comming from
  scheme I am quite accustomed to lengthy names and could not help it so
  far.
* Redundant code due to my ignorance of the forth metaprogramming
  facilities. I need to read up on these.
* Unclear when to use cells or bytes as an access unit for accessing
  buffers.
* What's the preferred coding style for longer words? Or is this a sign
  that my word is too convoluted and should be refactored?
* The access words for the values seem too verbose, are these even
  necessary?

Also I have the general feeling that it could be rewritten to take up
less words. I probably choose a bad data representation.

What are your thoughts?

Looking forward to hearing them,

Christian

--8<---------------cut here---------------start------------->8---
require random.fs

\ statistics
0 value #moves
: #moves+ #moves 1+ to #moves ;
: #moves- #moves 1- to #moves ;
3 value #teleports
: #teleports+ #teleports 1+ to #teleports ;
: #teleports- #teleports 1- to #teleports ;

\ the board
25 constant board-rows
80 constant board-cols

: random-xy board-cols random board-rows random ;

board-rows board-cols * constant board-dimension
create board board-dimension allot
: xy->board ( n1 n2 -- i ) board-cols * + ;
: board@ ( n1 n2 -- c ) xy->board board + c@ ;
: board! ( c n1 n2 -- )  xy->board board + c! ;

\ drawing words
char @ constant player-sym
char R constant robot-sym
char o constant hole-sym
32 constant floor-sym
char | constant wall-sym

: border ( -- ) [char] +  emit board-cols 0 do [char] - emit loop [char] + emit ;
: wall ( -- ) wall-sym emit ;
: board-reset ( -- ) board-dimension 0 do floor-sym board i + ! loop ;
: board-print ( -- )
  page border cr wall
  board-dimension 0 do
   i i board-cols mod 0= 0< and
   if wall cr wall then
   board i + @ emit
  loop
  wall cr border cr ;

\ player
create (player) 2 cells allot
: player@ ( -- x y ) (player) 2@ ;
: player! ( x y -- ) (player) 2! ;
: new-position ( x1 y1 dx dy -- x2 y2 ) rot + >r + r> ;
: valid-position? ( x y - b )
   dup 0>= swap board-rows < and swap
   dup 0>= swap board-cols < and and ;
 \ xy->board dup board-dimension < swap 0 >= and ;

: up ( -- dx dy ) 0 -1 ;
: down ( -- dx dy ) 0 1 ;
: left ( -- dx dy ) -1 0 ;
: right ( -- dx dy ) 1 0 ;
: update-player ( x y -- ) floor-sym player@ board! player-sym rot rot 2dup player! board! ;
: move ( dx dy -- ) player@ 2swap new-position 2dup valid-position? if update-player else 2drop then ;

\ hole words
10 constant #holes
create holes #holes 2* cells allot
: hole@ ( i -- x y ) 2* cells holes + 2@ ;
: hole! ( x y i -- ) 2* cells holes + 2! ;


\ robot words
2 constant #robots
\ robots occupy 3 cells: x y alive?
create robots #robots 3 * cells allot
: robot@ ( i -- x y ) 3 * cells robots + 2@ ;
: robot! ( x y i -- ) 3 * cells robots + 2! ;
: robot-alive ( b i -- )  3 * cells 2 cells + robots + ! ;
: robot-alive? ( i -- b ) 3 * cells 2 cells + robots + @ ;
: #robots-alive ( -- n ) 0 #robots 0 ?do i robot-alive? if 1+ then loop ;
: update-robot ( x y i -- )  >r 2dup i robot@ floor-sym rot rot board! 2dup robot-sym rot rot board! r> robot! ;
: distance ( x1 y1 x2 y2 -- x2-x1 y2-y1) >r rot - r> rot - swap ;
: sign ( n1 -- n2 ) dup 0= if drop 0 else dup abs / then ;
: direction ( d1 d2 -- dx dy ) sign swap sign ;
: towards-player ( x y -- x' y' ) 2dup player@ distance direction new-position ;
: collision? ( x1 y1 x2 y2 -- b )  xy->board rot rot xy->board = ;
: is-in-hole? ( x y -- b )
  xy->board false #holes 0 do over i hole@ xy->board = or loop swap drop ;
: move-robot ( i -- )
   dup >r robot@ towards-player 2dup is-in-hole?
    if false i robot-alive floor-sym r> robot@ board!
    else r> update-robot then ;
: move-robots ( -- ) #robots 0 ?do i robot-alive? if i move-robot then loop ;
: any-collision? ( -- b ) false #robots 0 ?do i robot-alive? if i robot@ player@ collision? or then loop ;

\ game routines init, loop
: init-robots #robots 0 ?do robot-sym random-xy 2dup i robot! board! true i robot-alive loop ;
: init-holes #holes 0 do hole-sym random-xy 2dup i hole! board! loop ;
: init-player random-xy 2dup player! update-player 0 to #moves 3 to #teleports ;
: reset-game board-reset init-player init-robots init-holes ;
: status-line ." moves: "  #moves . ." teleports: " #teleports . ." robots: " #robots-alive . ;
: help ." h: left, j: down, k: up, l: right, t: teleport, q: quit game"  cr ;
: user-input key
  case
    [char] h of left move endof
    [char] j of down move endof
    [char] k of up move endof
    [char] l of right move endof
    [char] q of ." Thanks for playing! " quit endof
    [char] t of #teleports 0> if #teleports- random-xy 2dup update-player player! then endof
    #moves-
  endcase
  #moves+ ;

: run reset-game
   begin
    move-robots board-print status-line cr help
    #robots-alive 0= if ." You win!" cr bye then
    any-collision? if
     ." You died! " cr bye
     else user-input then
   again ;

run
--8<---------------cut here---------------end--------------->8---

Back to comp.lang.forth | Previous | NextNext in thread | Find similar


Thread

Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-19 11:39 +0200
  Re: Request for comments on a first forth program Ron Aaron <rambamist@gmail.com> - 2013-07-19 13:04 +0300
    Re: Request for comments on a first forth program rickman <gnuarm@gmail.com> - 2013-07-19 12:43 -0400
  Re: Request for comments on a first forth program Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-07-19 05:28 -0500
  Re: Request for comments on a first forth program m.a.m.hendrix@tue.nl - 2013-07-19 03:56 -0700
    Re: Request for comments on a first forth program m.a.m.hendrix@tue.nl - 2013-07-19 04:00 -0700
    Re: Request for comments on a first forth program rickman <gnuarm@gmail.com> - 2013-07-19 12:51 -0400
      Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:28 +0200
        Re: Request for comments on a first forth program anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-07-22 15:43 +0000
          Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-24 20:00 -0700
            Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-25 10:37 +0200
              Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-26 00:00 -0700
                Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-26 09:34 +0200
                Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-25 21:40 -1000
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-27 20:16 -0700
                Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-29 10:09 +0200
                Re: Request for comments on a first forth program m.a.m.hendrix@tue.nl - 2013-07-29 04:14 -0700
                Re: Request for comments on a first forth program Andrew Haley <andrew29@littlepinkcloud.invalid> - 2013-07-29 07:49 -0500
                Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-29 15:32 -1000
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-31 17:50 -0700
                Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-31 13:23 +0200
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-31 18:05 -0700
                Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-08-01 12:53 +0200
                Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-08-01 09:58 -0500
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-08-01 20:42 -0700
                Re: Request for comments on a first forth program Brad Eckert <hwfwguy@gmail.com> - 2013-08-04 18:53 -0700
                Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-08-04 22:05 -0500
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-08-04 20:54 -0700
                Re: Request for comments on a first forth program Alex McDonald <blog@rivadpm.com> - 2013-08-05 05:50 -0700
                Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-08-05 22:14 +0200
                Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-08-06 21:11 -0700
                Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-08-01 01:04 -0500
    Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-19 18:06 +0000
      Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:31 +0200
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:24 +0200
      Re: Request for comments on a first forth program anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-07-22 15:32 +0000
  Re: Request for comments on a first forth program Alex McDonald <blog@rivadpm.com> - 2013-07-19 06:49 -0700
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:34 +0200
  Re: Request for comments on a first forth program Mark Wills <markrobertwills@yahoo.co.uk> - 2013-07-19 07:19 -0700
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:41 +0200
  Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-19 07:55 -1000
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:47 +0200
      Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-20 11:15 -1000
    Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-20 20:09 -0700
  Re: Request for comments on a first forth program "Paul E. Bennett" <Paul_E.Bennett@topmail.co.uk> - 2013-07-19 19:36 +0100
    Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-20 12:36 +0000
      Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:53 +0200
      Re: Request for comments on a first forth program Coos Haak <chforth@hccnet.nl> - 2013-07-21 02:02 +0200
        Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-20 17:18 -1000
      Re: Request for comments on a first forth program Ian Osgood <iano@quirkster.com> - 2013-07-23 16:07 -0700
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:48 +0200
  Re: Request for comments on a first forth program stephenXXX@mpeforth.com (Stephen Pelc) - 2013-07-20 12:18 +0000
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@necronomicon.my.domain> - 2013-07-20 21:57 +0200
      Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-20 11:24 -1000
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-22 15:53 +0200
      Re: Request for comments on a first forth program m.a.m.hendrix@tue.nl - 2013-07-22 08:06 -0700
        Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-22 19:35 +0000
          Re: Request for comments on a first forth program m.a.m.hendrix@tue.nl - 2013-07-23 00:41 -0700
          Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-23 12:00 +0200
            Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-23 12:10 +0000
              Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-23 14:19 +0200
              Re: Request for comments on a first forth program stephenXXX@mpeforth.com (Stephen Pelc) - 2013-07-24 09:27 +0000
            Re: Request for comments on a first forth program anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-07-23 13:44 +0000
  Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-20 20:02 -0700
    Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-21 03:36 +0000
      Re: Request for comments on a first forth program hughaguilar96@yahoo.com - 2013-07-20 22:28 -0700
        Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-21 10:31 +0000
    Re: Request for comments on a first forth program rickman <gnuarm@gmail.com> - 2013-07-20 23:41 -0400
  Re: Request for comments on a first forth program anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2013-07-22 15:36 +0000
  Re: Request for comments on a first forth program Lars Brinkhoff <lars.spam@nocrew.org> - 2013-07-23 12:22 +0200
    Re: Request for comments on a first forth program Christian Kellermann <ckeen@pestilenz.org> - 2013-07-23 13:44 +0200
      Re: Request for comments on a first forth program albert@spenarnc.xs4all.nl (Albert van der Horst) - 2013-07-23 12:26 +0000
    Re: Request for comments on a first forth program "Elizabeth D. Rather" <erather@forth.com> - 2013-07-23 07:10 -1000
    Re: Request for comments on a first forth program stephenXXX@mpeforth.com (Stephen Pelc) - 2013-07-24 09:30 +0000

csiph-web