A small game in forth
Forth is a strange programming language, in that it does things a bit
differently than most other languages this program is fairly short
about 10 lines
High Low is a very simple program, so this will be my introductionary
post on forth. I am by no means an expert, so the code will likely by
very naive.
Anyway let us write the only thing that might be seen as magical in
this program, it really is not very magical, but it is the only thing
that has some complexity to it.
The random number generator, yes we make one ourselves, that is the
philosophy of forth to make it yourself.
,----
| variable rnd HERE rnd !
| : random rnd @ 82398327 * 638812 + dup rnd ! ;
| : choose random um* nip ;
|
`----
Let us examine this block of code variable introduces a new variable
in forth, which is actually a memory address then you need to put
stuff into memory and get stuff from memory but that is done with read
(@) and put (!). Another thing that is necessary to know about forth
is the way it reads input anything you type into forth is put on a
stack, and then it takes the items off the stack and evaluates them
one by one. A small example of this is +, this word expects 2 numbers
on the stack, so 3 4 + will return a 7, but it will not print the 7,
that is also put on the stack.
To get something from the stack you need pop (.) this takes the top
item on the stack and prints it. numbers goes on the stack and
anything that is not a number is evaluated, meaning that if forth does
not know the word at the time it runs into it that will throw an
error.
dup is short for duplicate um* does a mod operation, nip takes the
second element out of the stack.
Anything between : and ; are a word definition, the firth thing after
: is what you call to evaluate the word.
I think that is enough to explain what that code does.
1.2 Secret Number
So with the almost complicated part done let us look at what we need
to write the rest of the code to have a functional high low game.
First we need a random number, ideally a random number we "hide" in
memory, so it is not available by a simple .s (showing the stack)
Fortunately that is fairly easy, we just need another variable.
,----
| variable secret
| : hide-number 101 choose secret ! ;
|
`----
That is all we need to hide the number away, but we need to have this
and the definition from the other block in the same file otherwise
forth does not know what choose means.
To compare the number we stored in memory to the guess the user made,
we need to retrieve it again.
I will not explain to much about the code from here on out.
,----
| : get-number secret @ ;
|
`----
This retrieves our hidden number, making it a little easier to compare
this number with the guess the user inputs.
,----
| : high dup get-number > if ." You went over the secret " then ;
| : low dup get-number < if ." You guessed too low " then ;
| : right get-number = if ." You guessed the secret " then ;
|
| : new-game hide-number ;
|
| : guess high low right ;
`----
So this is the finishing touches for this small game.
Forth is a strange programming language, in that it does things a bit
differently than most other languages this program is fairly short
about 10 lines
High Low is a very simple program, so this will be my introductionary
post on forth. I am by no means an expert, so the code will likely by
very naive.
Anyway let us write the only thing that might be seen as magical in
this program, it really is not very magical, but it is the only thing
that has some complexity to it.
The random number generator, yes we make one ourselves, that is the
philosophy of forth to make it yourself.
,----
| variable rnd HERE rnd !
| : random rnd @ 82398327 * 638812 + dup rnd ! ;
| : choose random um* nip ;
|
`----
Let us examine this block of code variable introduces a new variable
in forth, which is actually a memory address then you need to put
stuff into memory and get stuff from memory but that is done with read
(@) and put (!). Another thing that is necessary to know about forth
is the way it reads input anything you type into forth is put on a
stack, and then it takes the items off the stack and evaluates them
one by one. A small example of this is +, this word expects 2 numbers
on the stack, so 3 4 + will return a 7, but it will not print the 7,
that is also put on the stack.
To get something from the stack you need pop (.) this takes the top
item on the stack and prints it. numbers goes on the stack and
anything that is not a number is evaluated, meaning that if forth does
not know the word at the time it runs into it that will throw an
error.
dup is short for duplicate um* does a mod operation, nip takes the
second element out of the stack.
Anything between : and ; are a word definition, the firth thing after
: is what you call to evaluate the word.
I think that is enough to explain what that code does.
1.2 Secret Number
-------------------------
So with the almost complicated part done let us look at what we need
to write the rest of the code to have a functional high low game.
First we need a random number, ideally a random number we "hide" in
memory, so it is not available by a simple .s (showing the stack)
Fortunately that is fairly easy, we just need another variable.
,----
| variable secret
| : hide-number 101 choose secret ! ;
|
`----
That is all we need to hide the number away, but we need to have this
and the definition from the other block in the same file otherwise
forth does not know what choose means.
To compare the number we stored in memory to the guess the user made,
we need to retrieve it again.
I will not explain to much about the code from here on out.
,----
| : get-number secret @ ;
|
`----
This retrieves our hidden number, making it a little easier to compare
this number with the guess the user inputs.
,----
| : high dup get-number > if ." You went over the secret " then ;
| : low dup get-number < if ." You guessed too low " then ;
| : right get-number = if ." You guessed the secret " then ;
|
| : new-game hide-number ;
|
| : guess high low right ;
`----
So this is the finishing touches for this small game.