And Another Tiny BASIC Game: Mugwump
Tuesday, 13th August 2019
As I said when I implemented Hunt the Hurkle, it would be trivial to convert it to play the game of Mugwump too. The only difference between the games is what gets reported when you guess the position of the creature wrongly. In Hurkle you get the direction it lies in relative to your guess; in Mugwump you get the distance.
It took about fifteen minutes to change the Hurkle code to Mugwump, and that includes the time it took to adapt my original code for earlier interpreters by inserting missing line numbers and removing comments. Here's the code in its readable form:
REM REM Mugwump REM A Demonstration Program for Tiny BASIC REM REM --- Variables REM C: column or row difference between player guess and mugwump position REM D: total distance between player guess and mugwump position REM G: mugwump column REM H: mugwump row REM M: moves taken REM S: random number seed REM X: player guess column REM Y: player guess row REM --- Initialise the random number generator PRINT "Think of a number." INPUT S REM --- Initialise the game GOSUB 200 LET G=R-(R/10*10) GOSUB 200 LET H=R-(R/10*10) LET M=0 REM --- Input player guess 10 PRINT "Where is the mugwump? Enter column then row." INPUT X,Y IF X>=0 THEN IF X<=9 THEN IF Y>=0 THEN IF Y<=9 THEN GOTO 20 PRINT "That location is off the grid!" GOTO 10 REM --- Process player guess 20 LET M=M+1 PRINT "The mugwump is..." LET D=0 LET C=G-X GOSUB 60 LET C=H-Y GOSUB 60 IF D=0 THEN GOTO 40 PRINT "...",D," cells away." IF M>10 THEN GOTO 50 PRINT "You have taken ",M," turns so far." GOTO 10 REM --- Player has won 40 PRINT "...RIGHT HERE!" PRINT "You took ",M," turns to find it." END REM --- Player has lost 50 PRINT "You have taken too long over this. You lose!" END REM --- Helper subroutine to calculate distance from player to mugwump REM Inputs: C - difference in rows or columns REM D - running total distance REM Output: D - running total distance, updated 60 IF C<0 THEN LET C=-C LET D=D+C RETURN REM --- Random number generator REM Input: S - current seed REM Outputs: S - updated seed REM R - generated random number 200 LET S=(42*S+127)-((42*S+127)/126*126) LET R=S RETURN
One thing I have noticed is that the renumbering process can become tedious if I have to make significant revisions to the program afterwards. This looks like an excellent job for an awk script. Once I get fed up enough with manual renumbering to write that script, I'll post it here.