home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l3p110.seq < prev    next >
Text File  |  1990-04-01  |  3KB  |  83 lines

  1. \ Simple numeric input, version 2
  2.  
  3. \ Check to see if n is an ASCII key code corresponding
  4. \ to a numeric digit 0 thru 9.
  5. : DIGIT?  ( n -- flag )
  6.          48 ( ASCII 0 ) 57 ( ASCII 9 ) [IN] ;
  7.  
  8. \ Erase character behind the cursor and backup one
  9. \ character position.
  10. : RUBOUT  ( -- )
  11.         8 ( backspace ) EMIT SPACE  8 EMIT ;
  12.  
  13. \ Remove the most recently accumulated digit.  For example
  14. \ 123/10 = 12   The digit 3 is removed.
  15. : -DIGIT  ( n -- n/10 )
  16.         10 / ;
  17.  
  18. \ Take the ASCII code  c for a digit 0 thru 9 and add it
  19. \ into the number being formed.  For example if n = 12
  20. \ and c=51, the ASCII code for digit 3 then number left on
  21. \ the stack will be 123.  Try it yourself.
  22. : +DIGIT  ( n c -- 10n+c-48)
  23.         48 - SWAP 10 * + ;
  24.  
  25. \ Wait for user to input a number.  Make sure that only numeric
  26. \ keycodes are taken for input.  This word uses an infinite loop
  27. \ and uses EXIT to escape when the enter key is pressed.
  28. : #IN   ( -- num )
  29.         0  BEGIN  KEY               \ Fetch a key press.
  30.            DUP 13 ( enter ) =
  31.            IF DROP EXIT THEN        \ Exit if done.
  32.            DUP  8 ( backspace ) =
  33.            IF   DROP RUBOUT -DIGIT  \ Erase and correct.
  34.            ELSE DUP  DIGIT?         \ Was digit pressed?
  35.                 IF   DUP EMIT       \ Echo digit
  36.                      +DIGIT         \ Convert digit.
  37.                 ELSE DROP           \ Invalid key.
  38.                      7 ( bell) EMIT \ Sound bell
  39.                 THEN
  40.            THEN
  41.            AGAIN ;
  42.  
  43. COMMENT:
  44. Problem 3.25
  45. Use the new #IN to program this simple number guessing game.
  46. The computer picks a secret number between 1 and 100.  You try
  47. to guess the number.  With each guess the computer responds
  48.  "WARMER"  if the guess  is closer than the old guess,
  49.  "COLDER"  if the guess  is it is not closer,
  50.  "HOT!"    if the guess  is within 2 of the actual number.
  51.  "YOU GOT IT" if the guess is correct.
  52.  
  53. Hints:
  54. 1) Keep game info on the stack    ( secret old#  new# )
  55. 2) Use #IN
  56. 3) Use the random number generator below.  Don't try to figure out
  57.    how it works.  It uses one of those VARIABLES that we have been
  58.    avoiding.
  59. COMMENT;
  60.  
  61. VARIABLE SEED   12345 SEED !
  62. : (RND) SEED @ 259 * 3 + 32767 AND DUP SEED ! ;
  63. \ r is a random number   0 <= r < n
  64. : RND  ( n   r )
  65.         (RND) 32767 */ ;
  66.  
  67. \ Solution to problem 3.5 without any comments.  If you are going to
  68. \ borrow any thing from what you see below be sure to add the comments.
  69. : WINNER? 2 PICK OVER =               ;
  70. : HOT?    2 PICK OVER - ABS 3 <       ;
  71. : WARMER? 2 PICK OVER - ABS
  72.           3 PICK 3 PICK - ABS <       ;
  73. : GAME
  74.         100 RND 1+ 0
  75.         BEGIN CR ." GUESS "  #IN SPACE
  76.         WINNER?  IF ." GOT IT" DROP 2DROP EXIT THEN
  77.         HOT?     IF ." HOT "   ELSE
  78.         WARMER?  IF ." WARMER" ELSE ." COLDER" THEN
  79.                  THEN  NIP
  80.         AGAIN ;
  81.  
  82.  
  83.