home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l3p110 < prev    next >
Text File  |  1990-07-15  |  4KB  |  110 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 110  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.                  ┌───────────────────────────┐
  6.                  │  A better version of #IN  │
  7.                  └───────────────────────────┘
  8.  
  9. We need a version of #IN that will not allow arbitrary text and garbage
  10. to be input.  Preferably one that could precheck for valid numeric
  11. input.  Here is our first attempt at a better numeric input word.
  12.  
  13. \ Simple numeric input, version 2
  14. \ Check to see if n is an ASCII key code corresponding
  15. \ to a numeric digit 0 thru 9.
  16. : DIGIT?  ( n -- flag )
  17.          48 ( ASCII 0 ) 57 ( ASCII 9 ) [IN] ;
  18.  
  19. \ Erase character behind the cursor and backup one
  20. \ character position.
  21. : RUBOUT  ( -- )
  22.         8 ( backspace ) EMIT SPACE  8 EMIT ;
  23.  
  24. \ Remove the most recently accumulated digit.  For example
  25. \ 123/10 = 12   The digit 3 is removed.
  26. : -DIGIT  ( n -- n/10 )
  27.         10 / ;
  28.  
  29. \ Take the ASCII code  c for a digit 0 thru 9 and add it
  30. \ into the number being formed.  For example if n = 12
  31. \ and c=51, the ASCII code for digit 3 then number left on
  32. \ the stack will be 123.  Try it yourself.
  33. : +DIGIT  ( n c -- 10n+c-48)
  34.         48 - SWAP 10 * + ;
  35.  
  36. \ Wait for user to input a number.  Make sure that only numeric
  37. \ key codes are taken for input.  This word uses an infinite loop
  38. \ and uses EXIT to escape when the enter key is pressed.
  39. : #IN   ( -- num )
  40.         0  BEGIN  KEY               \ Fetch a key press.
  41.            DUP 13 ( enter ) =
  42.            IF DROP EXIT THEN        \ Exit if done.
  43.            DUP  8 ( backspace ) =
  44.            IF   DROP RUBOUT -DIGIT  \ Erase and correct.
  45.            ELSE DUP  DIGIT?         \ Was digit pressed?
  46.                 IF   DUP EMIT       \ Echo digit
  47.                      +DIGIT         \ Convert digit.
  48.                 ELSE DROP           \ Invalid key.
  49.                      7 ( bell) EMIT \ Sound bell
  50.                 THEN
  51.            THEN
  52.            AGAIN ;
  53.  
  54. We have included quite a few comments in the above program and would
  55. like you to study it on your own.
  56.  
  57. ╓───────────────╖
  58. ║ Problem 3.24  ║
  59. ╙───────────────╜
  60. Use this version of #IN for the TANK program Lesson 3 Part 10.
  61. Even this version of #IN has some problems.  Can you identify them?
  62. What happens if you try to enter a very large number?  What happens if
  63. you try to enter a negative number?  What happens if you try to make a
  64. correction?  Now we don't want to alarm you unnecessarily!  This number
  65. input routine is pretty good but it is not good enough for a program you
  66. might like to sell for money.
  67.  
  68. ╓──────────────╖
  69. ║ Problem 3.25 ║
  70. ╙──────────────╜
  71. Use the new #IN to program this simple number guessing game.
  72. The computer picks a secret number between 1 and 100.  You try
  73. to guess the number.  With each guess the computer responds
  74.  "WARMER"  if the guess  is closer than the old guess,
  75.  "COLDER"  if the guess  is it is not closer,
  76.  "HOT!"    if the guess  is within 2 of the actual number.
  77.  "YOU GOT IT" if the guess is correct.
  78.  
  79. Hints:
  80. 1) Keep game info on the stack    ( secret old#  new# )
  81. 2) Use #IN
  82. 3) Use the random number generator below.  Don't try to figure out
  83.    how it works.  It uses one of those VARIABLES that we have been
  84.    avoiding.
  85.  
  86. VARIABLE SEED   12345 SEED !
  87. : (RND) SEED @ 259 * 3 + 32767 AND DUP SEED ! ;
  88. \ r is a random number   0 <= r < n
  89. : RND  ( n   r )
  90.         (RND) 32767 */ ;
  91.  
  92. \ Solution to problem 3.25 without any comments.  If you are going to
  93. \ borrow anything from what you see below be sure to add the comments.
  94. : WINNER? 2 PICK OVER =               ;
  95. : HOT?    2 PICK OVER - ABS 3 <       ;
  96. : WARMER? 2 PICK OVER - ABS
  97.           3 PICK 3 PICK - ABS <       ;
  98. : GAME
  99.         100 RND 1+ 0
  100.         BEGIN CR ." GUESS "  #IN SPACE
  101.         WINNER?  IF ." GOT IT" DROP 2DROP EXIT THEN
  102.         HOT?     IF ." HOT "   ELSE
  103.         WARMER?  IF ." WARMER" ELSE ." COLDER" THEN
  104.                  THEN  NIP
  105.         AGAIN ;
  106.  
  107. ┌────────────────────────────────────┐
  108. │  Please move to Lesson 3 Part 120  │
  109. └────────────────────────────────────┘
  110.