24 game: Difference between revisions

m
Minor clarifications and edits.
m (Minor code correction that didn't get saved last time.)
m (Minor clarifications and edits.)
Line 3,469:
To get around this, this program utilizes BASIC's ability to parse expressions with the simple mathematical operators, and is in fact technically a self-modifying program. Line 2005 is a line padded with colons which simply allow BASIC to join multiple statements on a single line, otherwise perform no operation. This reserves sufficient space in memory for inserting the user's expression—by overwriting the first several bytes of colons—which can then be evaluated in the normal course of the program's execution. The subroutine at 1400 initializes a simple translation table for exchanging the operators into their proper BASIC tokens. Parenthesis, numerals, and variable names do not need to be translated.
 
After the user types in an expression, the program validates the input (same algorithms as the ZX Spectrum example), and then insertswrites the expression for variableas <code>R=''expression''</code> toas betokenized equalBASIC tointo itline 2005, and then executes the subroutine at 2000 to obtain the value. Upon return, the normal evaluation of whether 24 was obtain is checked.
 
If the statement at line 645 is uncommented, this will allow the program to "erase" line 2005 and thus "hide" the trick. As is, if you list the program after making an attempt, you will see the last expression given to the program.
 
Since Commodore BASIC v2 was the initial target for this program, all other versions of Commodore BASIC are compatible as long as the base memory address for BASIC programs is adjusted. (BASIC tokens maintain compatibility across all versions.) Simply use the appropriate values for <code>bh</code> and <code>bl</code> in lines 11-15.
 
<lang gwbasic>1 rem 24 game
113

edits