6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 24, 2024 6:52 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sun Oct 26, 2014 12:09 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
The weather has turned horrible again in Berlin, and this means I can finally spend more time inside with other projects. Yay for cold, wet drizzles!

When I've had a bit of time, I've been continuing my Forth education with small programs. One of those finally gave me the push to understand two things I had read in Thinking Forth, but not fully grasped until this. It's a ridiculously primitve adventure game with nine rooms you can only walk through:

Code:
\ The Very Very Small Forth Mini-Adventure
\ Scot W. Stevenson <scot.stevenson@gmail.com>
\ This version: 26. Oct 2014
\ Uses NOS for X and TOS for Y

3 constant SIZE  \ 3x3 playing field
SIZE dup *  1-  constant TARGET#   \ exit Room 8 for nine rooms
SIZE 1- constant WALL  \ for zero-based indexing

create Descriptions
   s" The word START is written on the floor" , ,  \ Room 0
   s" Frogs sit on the stones of this little room" , ,
   s" This corner is very dirty and smells like cat pee" , ,
   s" Somebody wrote 'Elbereth' in the dust here" , ,  \ Room 3
   s" A single shaft of light streams from the ceiling" , ,
   s" There is a big bolder in your way" , ,
   s" This corner is amazingly clean" , ,  \ Room 6
   s" There is a poster of Buffy the Vampire Slayer on the wall here" , ,
   s" There is a big door marked EXIT here" , ,  \ Room 8 (the exit)

create Roomtypes  \ 1 blocked, 0 free
   0 , 0 , 0 ,
   0 , 0 , 1 ,
   0 , 0 , 0 ,

: room# ( x y -- u )  SIZE *  + ;  \ index of given room for arrays

: examine ( x y -- x y )  \ does not consume arguments; system uses LOOK
   2dup room# 2* cells  Descriptions +
   dup @ swap  1 cells +  @  swap  \ get ( addr u ) for string
   cr type space ;

: won? ( x y -- f )  room# TARGET# = ;
: blocked? ( x y -- f ) room# cells  Roomtypes +  @ 1 = ;

: step ( x y x y -- x y ) 2swap 2drop ; \ system uses MOVE

: guide ( x y x y -- x y )
   2dup won? if  cr ." You've won!" step examine  else
   2dup blocked? if  cr ." Can't go there: " examine 2drop  else
   step  then then ;

: west ( x y -- x y )  over 1-  0 max  over  guide ;
: east ( x y -- x y )  over 1+  WALL min  over  guide ;
: north ( x y -- x y )  over >r  dup 1+  WALL min  r> swap  guide ;
: south ( x y -- x y )  over >r  dup 1-  0 max  r> swap  guide ;

: adventure ( -- x y )
   0 dup  \ start in Room 0
   cr
   ." Welcome to the Very Very Small Forth Mini-Adventure" cr
   ." Find your way to the far corner with commands 'examine'" cr
   ." 'west', 'east', 'north', and 'south'."
   space examine ;

adventure


(For gforth 0.7.0: gforth and then include adventure.fs) So my insights were, duh:

1. Use the stack, not variables. First versions used x and y as variables, because, well, you need variables, right? Been using them since BASIC days. Except everything had x and y in them, and that seemed stupid. Now TOS is y and NOS is x. Amazing how that cleans up the code.

2. Use the Forth interpreter itself. I spent a whole day wondering how to write a good CLI, something like the one that is available in the Python library. Except that Forth comes with a perfectly good interpreter, of course. So now Forth itself includes an adventure game, which I'm sure EMACS has built in somehow, but takes a bit of getting used to. As an added bonus, debugging is suddenly amazingly simple, because you have all the tools right there. Wondering if you left stuff on the stack? Just type .S in the middle of the game.

This isn't quite done -- I think I need a 2DROP somewhere after you've reached the exit to clean up. And I'm not sure if I should just declare a VOCABULARY named adventure and use "look" instead of examine, which is system command in gforth. Any suggestions on the best practices for that?


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 26, 2014 2:17 pm 
Offline
User avatar

Joined: Fri Dec 11, 2009 3:50 pm
Posts: 3367
Location: Ontario, Canada
Good stuff, Scot -- I see the cold, wet drizzles have been productive for you! :)

Quote:
use "look" instead of examine, which is system command in gforth
'Fraid I don't have gforth and can't comment on look. But I did want to comment on your word examine and its use in guide. Apparently examine spits out the text string describing the room specified by x:y on NOS:TOS. It seems an unusual choice that, in guide, the description is issued for the winning room and the blocked room (the only one you can't move into) but not for the other seven rooms. Maybe I'm missing something.


A couple of tiny suggestions. These aren't functional changes; they just shrink the code slightly.

In the final lines of adventure
Code:
   ." 'west', 'east', 'north', and 'south'."
   space examine
... can be replace with...
Code:
   ." 'west', 'east', 'north', and 'south'. "
         examine
Instead of using space I simply included a space at the end of the printed string.

Also, in north
Code:
: north ( x y -- x y )  over >r  dup 1+  WALL min  r> swap  guide ;
... the over >r r> gymnastics can be simplified somewhat by using pick (if gforth features that word -- if not, write it yourself; this is Forth after all!). The same change can be applied to south.

cheers,
Jeff

_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 26, 2014 10:53 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1951
Location: Sacramento, CA, USA
I'm a rank amateur, but it seems to me that those OVERs should be SWAPs in west, east, north, and south.

Or is it just incorrect stack comments for the same?
That's probably it, otherwise your code wouldn't make it very far, right?

Or am I completely missing something due to my n00bness?

Mike

[Edit: Ah, guide balances things out! ... at least part of the time, anyway ... it looks like guide might benefit from a closer look.]

[Edit #2: Would this version of north and south be equivalent to yours?:
: north ( x y -- x y ) 2dup 1+ WALL min guide ;
: south ( x y -- x y ) 2dup 1- 0 max guide ;

I still haven't figured out exactly how guide works.]


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 27, 2014 9:22 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
Jeff -- Yes, thank you, that SPACE can go, I had it there because I was fooling around with the text and kept forgetting it. Then I, uh, forgot it. As for the second part, I'm afraid Mike wins the race with his second edit. Duh, yes, of course, far simpler. Thanks. Interesting mistake, though, I had done WEST and EAST first and obviously had OVER on the brain ...

I'm going to take a look at VOCABULARY next for a while to figure out if I can use MOVE and LOOK after all.


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 01, 2014 3:40 pm 
Offline

Joined: Mon Jan 07, 2013 2:42 pm
Posts: 576
Location: Just outside Berlin, Germany
So, it turns out that vocabulary management in Forth is something of a mess, because of the usual historical reasons. Gforth seems to recognize this and provides a bunch of additional, more modern stuff. In our case, to temporarily add a word list so we can redefine MOVE and LOOK, the simplest way seems to be

WORDLIST >ORDER DEFINITIONS

This creates a new word list, pushes it to the top of the stack of wordlists, and makes it the current one for definitions. Then, we redefine MOVE and LOOK, do all our stuff, and once we reach the exit, we include

PREVIOUS DEFINITIONS

to restore the old order. Note the word list never gets a name, but then it doesn't need one.

What I still don't like about the program is that the end is not at the end. Have to think about that for a while.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: