The following programs provide you with an entertaining puzzle game that pops up as a small window on the Workbench screen. But more importantly, they demonstrate some interesting and powerful programming techniques in Amiga BASIC.
A popular game that used to keep kids occupied for hours in the back of the station wagon was known as the "slide puzzle." This was simply a plastic frame with 15 numbered or lettered tiles arranged in a 4 X 4 pattern with one square left vacant. The object was to slide one tile at a time into the vacant slot in an attempt to restore the puzzle to its proper numeric or alphabetic order. "Amiga Puzzle" is the Amiga BASIC equivalent of the slide puzzle.
Amiga Puzzle works on any Amiga with Amiga Microsoft BASIC (not ABasiC-Amiga BASIC is available as a free upgrade if your computer was shipped with ABasiC). To get started, run Amiga BASIC, enter Program 1 below, and save a copy on disk. Be sure to set Preferences for a 60-column screen before running Amiga Puzzle. When you run the program, it pops open as a small window on the Workbench screen and scrambles the tiles. When the program announces "Ready" using the Amiga's built-in speech capability, you can begin.
To move a tile into the vacant slot, point to the tile with the mouse and press the left mouse button. If you try to cheat by moving a tile diagonally, the program will scold you. The gadget in the lower-right corner of the window, normally reserved for window resizing, has been replaced with a plus sign in Amiga Puzzle. Clicking on this gadget rescrambles the puzzle and starts a new game. The other gadgets (front/back, the move bar, and dose window) are all active as usual.
As a final touch, the Puzzle window displays the elapsed time and number of moves since the start of the game.
This photo shows two copies of "Amiga
Puzzle" running simultaneously, an ex-
ample of the Amiga's multitasking
How It Works
Amiga Puzzle was adapted from a similar puzzle game available on the Macintosh. Those of you familiar with Microsoft BASIC 2.0 on the Macintosh will immediately see many similarities in Amiga BASIC. In fact, many Macintosh BASIC programs can be converted to Amiga BASIC with little effort. Both languages support windows, pull-down menus, labels, many types of graphics commands, and other features.
Amiga Puzzle is divided into seven subroutines named Done, Init, DrawScreen, Mix, Play, CheckCheat, and More. There are also two small subprograms, Talk and Position. Here are descriptions of what these subroutines do:
Init: initializes variables, userdefined functions, and loads the puzzle's characters A-O into the two-dimensional array c0.
DrawScreen: displays the puzzle's squares and characters.
Mix: mixes only adjacent squares and keeps track of the blank square's position in variables blankX and blankY.
Play: the main subroutine where the current mouse position is compared to coordinates of each square stored in the three-dimensional array rat.
CheckCheat: makes sure the attempted move is a legal one (adjacent squares only, no diagonal moves).
More: checks if the mouse is clicked on the plus-sign gadget in Amiga Puzzle's lower-right corner. If so, the program starts a new game by jumping back to Start.
Done: ends the program and returns control to BASIC.
Most of the program is standard Microsoft BASIC. The only lines that merit special attention are the user-defined functions in the Init subroutine. One function operates the puzzle's timer, and the other determines if the puzzle has been solved.
A quick way to time any event is to define a function that subtracts the current time from the initial starting time. This is done with the function FNlaps. The current time is obtained from TIMER and the initial time stored in the variable starttime.
The other defined function, FNwin, determines if the puzzle has been solved by comparing the letter in each tile-stored in the array c0-with the characters A-O. Each letter in the correct position returns a value of -1 (true). So the puzzle has been solved when all 15 letters are sorted, returning a value of -15.
A very powerful feature of Amiga BASIC is its subprogram capability (see "Requester Windows in Amiga BASIC," COMPUTE!, March 1986). In effect, subprograms let you add new commands to the language. The word TALK is not a command in Amiga BASIC, but has been added to Amiga Puzzle as a subprogram. It lets you execute both a SAY command and the text-to-speech TRANSLATE$ function by simply typing TALK followed by the desired string. Similarly, POSITION is a subprogram that expects parameters of the X and Y coordinates as the location to PRINT the desired string.
Program 2, "MakeIcon," lets you create a special icon for Amiga Puzzle. This is optional, of course, since Amiga BASIC automatically assigns a standard icon when you save the program. MakeIcon recreates the special icon I designed with the Amiga's Icon Editor (a new feature that's included with the free upgrade to version 1.1 of the operating system). The icon data is converted from the hexadecimal values in the DATA statements into singlebyte values and stored in the file Puzzle.info (all Amiga icon files end with.info). In addition to the icon's shape and color, the special data also contains information that makes Amiga BASIC the program's default tool. That means when the icon is clicked on the Workbench screen, Amiga BASIC is automatically loaded and run prior to loading and running the game.
Multitasking With BASIC
Since multitasking is built into the Amiga's operating system as a standard feature, no special programming techniques are required to write a BASIC program that's capable of running simultaneously with other tasks. Feel free to move Amiga Puzzle over or under other windows running different programs without causing interference. If your computer has at least 512K RAM, you can even click on the Amiga Puzzle icon a second time and run two of the games at once.
Program 1: Amiga Puzzle
BEEP:WINDOW CLOSE 2:WINDOW 1←
Talk " " ←
tries= 0:RANDOMIZE TIMER ←
FOR y=0 TO 3←
FOR x=0 TO 3←
c(x,y)=x+y4+ASC("A") 'Load oharac
cCblankX,blankY)=ASC(" ") ←
+(((TIMER-starttimel) MoD 60)/100)←
c(2,1)=71) +(c(3,1)= 72)←
)'Won if = -15←
FOR y=0 TO 3←
FOR x=0 TO 3 ←
)) ' Print characters←
LINE (xl-l,yl-l)-(xl +30+ l,yl + 18+ 1)
Position 14,11,"+" ' New game gadget ←
NEXT y ←
Position 3,10,"TIME 0.00" ←
Position 3,11,"TRIES 0" ←
FOR mixing=333 TO 1 STEP -1 ←
IF (mixing AND 1)=0 THEN←
x=INT(RND'4):y=blankY ' Even←
y=INT(RND'4):x=blankX ' Odd←
NEXT mixing ←
Talk "Ready." ←
LOCATE 10,8:PRINT USING "##.##";FN1
FOR y=0 TO 3←
FOR x=0 TO 3 ←
IF (mouseX>rat(x,y,0) AND mouseX<r
at(x,y,0)+30) AND (mouseY>rat(x,y,l
) AND mouseY<rat(x,y,l)+18) THE
N GOSUB CheckCheat:RETURN←
NEXT y ←
GOSUB More ←
IF (ABS(x-blankX)> 1 OR ABS(y-blankY)
>1) OR ((x<>blankX AND y<>blankY)
IF mixing=0 THEN Talk "Cheater." 'Ch
ELSE 'Not cheating←
SWAP x,blankX:SWAP y,blankY←
IF mixing=0 THEN←
tries =tries +1 ←
LOCATE 11,8:PRINT tries; ←
IF FNwin=.15 THEN Talk "We have a wi
WHILE MOUSE(0)<>0 OR FNwin=.15'A
IF MOUSE(0)=0 AND (mouseX>more
X AND mouseX<moreX+ 10) AND (mou
seY>moreY AND mouseY<moreY+10
) THEN GOTO Start←
IF WINDOW(7)=0 THEN Done ←
SUB Talk(a$) STATIC ←
SUB Position(x,y,a$) STATIC ←
LOCATE y,x:PRINT a$;←
Program 2: Makelcon
PRINT:PRINT SPC(20);"Creating Puzzl
OPEN "Puzzle.info" FOR OUTPUT AS 2←
FOR ii =1 TO 98←
FOR i=1 TO 8 STEP 2←
KILL "Puzzle.info.info"' Delete icon's is
IF checksum<>12063 THEN BEEP:PRI
NT:PRINT "Error in data statements."←
SUB HX(a$) STATIC ` STR$ hex to dec (m
FOR i=1 TO LEN(a$)←
IF c>ASC("9") THEN n=(VAL(CHR$(
NEXT i ←
' Puzzle Icon data←