As I presented in BASIC and in Assembly, here I created a Guess My Number game. This game generates a random number between 1 and 100 and asks the user to guess it. There isn’t a limit to the number of guesses.
It looks and behaves just like the other two versions I created – with one minor exception. The only difference is in the display of the guess number. In this Action! version, the guess number does not have the 0 padded five digit number. I need to re-learn the string manipulation routines Action! has to offer.
Source
MODULE PROC Main() ; Declare variables byte gsnum=[1], iguess=[0], mynum byte lmarg=82 ; Prep screen lmarg=0 graphics(0) ; Generate random between 1 and 100 mynum=rand(100)+1 ; Display header print("-[Number Guess, Action!, V1]------------") printe("My number is between 1 and 100.") printe("What is it?") ; Loop until number is guessed do ; Display guess number print("Guess #") printb(gsnum) print("? ") ; Get users input in byte form iguess=inputb() ; If guess is too low if iguessmynum then printe("Sorry, too high!") ; Otherwise they got it else printe("You guessed it!") exit fi ; Increment guess number gsnum == +1 until iguess=mynum od RETURN
Breakdown
I commented the source family well, but I’ll break it down by section since Action! does not use line numbers.
; Declare variables
This declares the following:
- gsnum as type BYTE and initializes it to 1. This is to keep track of the guess number.
- iguess as type BYTE and initializes it to 0. This is where the users input is stored.
- mynum as type BYTE. This is the secret number the user is trying to guess.
- lmarg as BYTE initialized to point at memory location 82 (decimal) which is the left margin.
; Prep Screen
This sets up the screen by:
- lmarg=0 puts a 0 in the memory location pointed to by lmarg, which is 82, effectively setting the left margin to 0 instead 0f default 2.
- graphics(0) resets the text display, effectively clearing the screen. There is probably a better way to do this, and I will look into it further. I couldn’t get character 125 to clear the screen using any print combination.
; Generate random between 1 and 100
mynum is assigned to the result of the rand() function passed with 100 which will return a number between 0 and 99, so 1 is added to the result to get a number between 1 and 100.
; Display header
This just prints the header for the game. The lines using “print” do not add an EOL to the end of the string thus leaving the cursor at its location. The lines using “printe” do add an EOL to the end of the string thus moving the cursor to the next line.
; Loop until number is guessed
This creates the program guess loop. Everything between “do” and “od” are repeated until an exit condition is satisfied. The exit condition is specified by the “until” statement which says that the users guess must equal the secret number (“iguess=mynum”).
; Display guess number
This prints the guess number and input prompt by:
Printing “Guess #” without EOL (print).
Printing the guess number as a byte (printb), again without EOL.
Printing the ? prompt without EOL.
; Get users input in byte form
Use the inputb function to get a byte value from the user.
; If guess is too low
Simple enough. If the users guess is less than the secret number then print “Sorry, too low!” with EOL.
; If guess is too high
Again, simple. If the users guess is higher than the secret number then print “Sorry, too high!” with EOL.
; Otherwise they got it
Again, simple. Since neither condition prior matched, this else catches the only other condition – equal. Print “You guessed it!” with EOL, then force an exit of the DO loop. I’m not sure the exit is required since the values will be the same and satisfy the loop exit condition.
; Increment guess number
Does what it says. Adds 1 to gsnum. Normally you would write this “gsnum = gsnum + 1”, but Action! has a shortcut of “gsnum = +1”
Then a RETURN to signal the procedure Main end.
Compilation & Run
Before the program can be run, it has to be compiled. Enter the Action! Monitor. Then enter “C” to compile. If there were no errors, enter “R” to run it. Action! programs can be compiled with a runtime so the cartridge is not needed. I’ll explore that in a later post.
Result
When the game is first run:
Once the user has guess the number. In this shot the Action! Monitor is showing on the top line since the program has ended:
In the next post I will have the 0 padded guess number.