How to resolve the algorithm Guess the number step by step in the Forth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Guess the number step by step in the Forth programming language
Table of Contents
Problem Statement
Write a program where the program chooses a number between 1 and 10. A player is then prompted to enter a guess. If the player guesses wrong, then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a "Well guessed!" message, and the program exits. A conditional loop may be used to repeat the guessing until the user is correct.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Guess the number step by step in the Forth programming language
Source code in the forth programming language
\ tested with GForth 0.7.0
: RND ( -- n) TIME&DATE 2DROP 2DROP DROP 10 MOD ; \ crude random number
: ASK ( -- ) CR ." Guess a number between 1 and 10? " ;
: GUESS ( -- n) PAD DUP 4 ACCEPT EVALUATE ;
: REPLY ( n n' -- n) 2DUP <> IF CR ." No, it's not " DUP . THEN ;
: GAME ( -- )
RND
BEGIN ASK GUESS REPLY OVER = UNTIL
CR ." Yes it was " .
CR ." Good guess!" ;
You may also check:How to resolve the algorithm Stable marriage problem step by step in the J programming language
You may also check:How to resolve the algorithm Ruth-Aaron numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the CLU programming language
You may also check:How to resolve the algorithm Order disjoint list items step by step in the REXX programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the Erlang programming language