SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
20

Chance

Introduction

A program that does the same thing every time is a tool. A program that does something you did not expect is a game. The difference is a random number.

CHOOSE

Screen 1 of UTILITY gives you CHOOSE. 6 CHOOSE leaves a number from 0 to 5 - six possibilities, starting at nought, in the same spirit as DO loops. For a dice you want 1 to 6:

: DICE 6 CHOOSE 1+ ;

The Same Every Time

Load CHOOSE on a freshly started SuperForth, type 10 CHOOSE . six times, and write the numbers down. Now leave, start again, and do exactly the same. You get exactly the same numbers.

CHOOSE is not random at all. Each number is worked out from the one before. The results look thoroughly shuffled, but the same start always gives the same sequence, and SuperForth always starts in the same place.

What is needed is something genuinely unpredictable to move the starting point, and the only unpredictable thing attached to the machine is you.

: SHUFFLE BEGIN RANDOM DROP
?TERMINAL UNTIL KEY DROP ;

RANDOM is the word CHOOSE is built on: it works out the next number in the sequence. SHUFFLE does that over and over, throwing the numbers away, until you press a key. The KEY DROP on the end then takes that keypress away, so that it does not turn up in front of the player's first guess. Nobody can press a key at the same thousandth of a second twice, so nobody knows how far down the sequence it got. "Press any key to start" has been doing this job in games for a very long time.

A Game

The machine thinks of a number from 1 to 100, and tells you whether each guess is too low or too high.

Look at how it is put together before you type it. One variable. Then the smallest questions - is this guess low? is it high? - and then a word for one turn, and at the top a word that reads almost like the rules.

The Code

NEWFILE UTILITY
1 LOAD
4 LOAD
0 VARIABLE SECRET
: THINK 100 CHOOSE 1+ SECRET ! ;
: LOW? SECRET @ <
IF ." TOO LOW" THEN ;
: HIGH? SECRET @ >
IF ." TOO HIGH" THEN ;
: JUDGE DUP LOW? DUP HIGH? ;
: TURN CR ." YOUR GUESS?"
CR #IN JUDGE ;
: GAME THINK BEGIN TURN
SECRET @ = UNTIL ." GOT IT!" ;
GAME

Starting from

A fresh boot, with SuperForth just started.

What you should see

If you started plain FORTH and typed exactly this, the secret is 69 for you too - which is the point of the section.

GAME

YOUR GUESS?
50
TOO LOW
YOUR GUESS?
80
TOO HIGH
YOUR GUESS?
69
GOT IT!ok

A game in three guesses

Follow one guess through the stack. #IN leaves it. JUDGE copies it for LOW?, which uses up the copy; copies it again for HIGH?; and leaves the original. GAME then compares that with the secret, and UNTIL uses the answer. Nothing is left over.

This is fifteen short lines and worth keeping. It belongs on a screen of your own file, where a line can be longer and a mistake can be fixed without retyping the lot.

Change One Thing

  • Play the game, then leave with DOS, start again and play again. Is the secret number the same?
  • Add SHUFFLE to the start of GAME, after printing PRESS A KEY. Now is it the same?
  • Make the game count your guesses and tell you the score at the end. You need one more variable, and two of the words from S11.
  • Change the range to 1 to 1000. Roughly how many guesses should a good player need?

Exercises

20.1 Define ROLLS ( n -- ) to throw the dice n times and print the throws.

20.2 Define COIN to print HEADS or TAILS at random.

20.3 Throw the dice 600 times and count the sixes. About how many would you expect? Is CHOOSE fair?

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
The same "random" numbers every sessionThat is how CHOOSE works. SHUFFLE first.
YOUR? when you type a guessThe prompt and the answer are on the same row. CR before #IN.
The game never ends though you guessed rightThe guess was used up before the comparison. Every test needs its own DUP.
A dice that throws 06 CHOOSE is 0 to 5. Add one.

Summary

n CHOOSE leaves 0 to n-1 from a fixed sequence that always starts in the same place. Running RANDOM until a key is pressed moves the starting point by an amount nobody can predict. A game is a variable, a few small questions, a turn, and a loop.

Next

S21, Tables Of Numbers - keeping a whole row of numbers under one name.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.