www.jupiter-ace.co.uk

Previous Page > Listings Index > Guessing Game.


  Guessing Game listing from Practical Computing listing May 1983

Game

This listing was updated in March 2011 after a few errors were found.

THE LISTING by S D Collier of Llangollen, Clwyd is for a simple game in which the player has nine chances to guess where the computer is "hiding". The computer responds with clues to guide the player to one of 100 rooms.
The program will run easily within the Ace's 3K of memory and is fully documented to help newcomers understand Forth.

Ace game.

Words used: GAME INST CHOOSE AGAIN EQ TL TH INPUT RND SEEDON

Variables used:	SEED RPOS

0 VARIABLE SEED

0 VARIABLE RPOS

: SEEDON ( Part of Random No. Generator. ) SEED @ 75 U* 75 0 D+
  OVER OVER U< - - 1- DUP SEED ! ;

: RND ( Random Generator.) SEEDON U* SWAP DROP ;

: INPUT ( As basic. ) QUERY LINE ;

: TH ( Guess too high? ) DUP PROS @ > IF ." Too high !" CR THEN ;

: TL ( Guess too Low? ) DUP PPOS @ < IF ." Too low !!" CR THEN ;

: AGAIN CR CR ." To play again type GAME" DROP CR QUIT ;

: EQ ( Guess correct? ) DUP RPOS @ = IF ."Correct !!" CR AGAIN THEN ;

: CHOOSE ( Select RND No. for variable RPOS ) 100 RND 1+ RPOS ! ;

: INST ( Instructions ) CLS ." I will hide in a building of 100"
  CR ." floors. I will give you 9 goes  "
  CR ." to Guess where I am." CR CR ;

: GAME ( Put it all together ! ) INVIS INST CHOOSE 0 10 1 DO 
  DROP ." Guess " I . INPUT DUP . TH TL EQ LOOP
  ." It was room " DUP . CR AGAIN ;


To play Just type GAME.