Classic Computer Magazine Archive COMPUTE! ISSUE 76 / SEPTEMBER 1986 / PAGE 109

Programming the TI

C. Regena

Game Programming

Many computer games are translations of games that already exist in some other form. The challenge in making such a conversion is to offer features that make you want to play the game on a computer instead of the usual way (with cards, dice, a board, or whatever). In the next two columns, we'll construct a game that has been popular under various names, but is usually called "Solitaire."

The original Solitaire game consists of several pegs arranged in a pattern of holes on a board. The center hole is left without a peg. Your goal is to get rid of pegs by jumping: One peg jumps over another into an adjacent hole, then the jumped peg is removed. You keep jumping and removing pegs until you can no longer jump. The optimum solution is to end up with one peg in the center hole. Actually, if you end up with one peg anywhere, you are an excellent player, and even two, three, or four remaining pegs would be a good score.

Why create this game on a computer? The main reason is that you'll often start to play the game, but find that some pegs are missing. You can't even set up the board without the right number of pegs. The computer will always set up the game without losing pegs, and can also check for impossible moves and thus prevent cheating. In a computerized version, we can also include a feature which would allow backing up and changing a move, or even replaying several moves. As a final enhancement, the program can keep track of every move in the game and print them out so you could prove to a friend that you really solved the puzzle.

I usually start game programming by designing the graphics. This playing board consists of yellow circles for the pegs and black circles for the holes. Lines 190-240 define graphic characters and colors, and lines 250-280 define strings for printing the board. The subroutine in lines 620-770 prints the starting board on the screen.

The next step is to move the pegs. CALL KEY is used for keyboard input. Use the arrow keys to move to the peg you want to move, then press ENTER. Now press an arrow key to show which direction to jump. The computer then needs to check to see whether you made a valid move.

Since the complete program is too long to include in a single column, I've split it into two separate portions. This month's listing includes enough of the program to draw the graphics and move the pegs, so you can play a complete game. However, not all of the features are included. Next month's column will explain more of the programming techniques and add the sections that let you back up to change a move, replay the game, or make a game printout.

If you to prefer to save typing time, you may obtain a copy of the complete program by sending a check for $3 together with a stamped, self-addressed mailer and a blank cassette or disk to:

C. Regena
P. 0. Box 1502
Cedar City, Utah 84720

Be sure to specify the title, "Solitaire" for the TI-99/4A.

100 REM  SOLITAIRE
110 DIM G(12,12),M$(43)
120 CALL CLEAR
130 PRINT TAB(5);"** SOLITAIRE **"
140 PRINT ::"MOVE A PEG BY JUMPING OVER"
150 PRINT :"ANOTHER PEG TO AN EMPTY HOLE"
160 PRINT :"THEN REMOVE THE JUMPED PEG."
170 PRINT :"TRY TO END WITH ONLY ONE"
180 PRINT :"PEG IN THE CENTER HOLE."
190 CALL CHAR(96,"0")
200 CALL CHAR(97,"0000183C3C18")
210 CALL CHAR(98,"00183C7E7E3C18")
220 CALL COLOR(9,11,7)
230 CALL CHAR(105,"00183C7E7E3C18")
240 CALL COLOR(10,2,7)
250 A$="'''''''"
260 B$="'a'a'a'"
270 C$="''''''"&A$&"''''''"
280 D$="'a'a'a"&B$&"a'a'a'"
290 FOR J=0 TO 12
300 FOR K=0 TO 12
310 READ G(J,K)
320 NEXT K
330 NEXT J
340 DATA 2,2,2,2,2,2,2,2,2,2,2,2,2
350 DATA 2,2,2,2,2,2,2,2,2,2,2,2,2
360 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
370 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
380 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
390 DATA 2,2,1,1,1,1,1,1,1,1,1,2,2
400 DATA 2,2,1,1,1,1,0,1,1,1,1,2,2
410 DATA 2,2,1,1,1,1,1,1,1,1,1,2,2
420 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
430 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
440 DATA 2,2,2,2,2,1,1,1,2,2,2,2,2
4S0 DATA 2,2,2,2,2,2,2,2,2,2,2,2,2
460 DATA 2,2,2,2,2,2,2,2,2,2,2,2,2
470 PRINT ::: "PRESS <ENTER>."
480 CALL KEY(0,K,S)
490 IF K<>13 THEN 480
500 CALL CLEAR
S10 PRINT "USE THE ARROW KEYS THEN"
520 PRINT "<ENTER> TO SELECT THE PEG,"
530 PRINT "THEN PRESS AN ARROW KEY TO MOVE."
540 PRINT :: "PRESS FCTN-8 TO REDO A PLAY."
550 PRINT :: "PRESS FCTN-5 TO SHOW ALL"
560 PRINT "MOVES FROM THE START. "
570 PRINT :: "PRESS FCTN-P TO PRINT THE"
580 PRINT "SEQUENCE OF MOVES."
590 PRINT ::: "PRESS <ENTER> TO START NOW."
600 CALL KEY(0,K,S)
610 IF K=13 THEN 780 ELSE 600
620 CALL CLEAR
630 FOR T=1 TO 3
640 PRINT TAB(11);A$
650 PRINT TAB(11);B$
660 NEXT T
670 FOR T=1 TO 3
680 PRINT TAB(5);C$
690 PRINT TAB(5);D$
700 NEXT T
710 PRINT TAB(5);C$
720 FOR T=1 TO 3
730 PRINT TAB(11);B$
740 PRINT TAB(11);A$
750 NEXT T
760 CALL HCHAR(14,16,105)
770 RETURN
780 GOSUB 620
790 PRINT ::
800 R=6
810 C=4
820 ROW=R*2
830 COL=C*2+4
840 CALL GCHAR(ROW,COL,GG)
850 CALL KEY(0,K,S)
860 CALL HCHAR(ROW,COL,96)
870 CALL HCHAR(ROW,COL,GG)
880 IF S<1 THEN 850
890 IF K=13 THEN 1100
900 IF K<>69 THEN 950
910 IF R-1<2 THEN 850
920 IF G(R-1,C)=2 THEN 850
930 R=R-1
940 GOTO 820
950 IF K<>83 THEN 1000
960 IF C-1<2 THEN 850
970 IF G(R,C-1)=2 THEN 850
980 C=C-1
990 GOTO 830
1000 IF K<>68 THEN 1050
1010 IF C+1>10 THEN 850
1020 IF G(R,C+1)=2 THEN 850
1030 C=C+1
1040 GOTO 830
1050 IF K<>88 THEN 850
1060 IF R+1>10 THEN 850
1070 IF G(R+1,C)=2 THEN 850
1080 R=R+1
1090 GOTO 820
1100 CALL SOUND(50,1400,2)
1110 IF GG=105 THEN 850
1120 CALL KEY(0,K,S)
1130 CALL HCHAR(ROW,COL,98)
1140 CALL HCHAR(ROW,COL,97)
1150 IF S<1 THEN 1120
1160 IF K<>69 THEN 1240
1170 IF (G(R-2,C)<>0)+(G(R-1,C)<>1) THEN 1530
1180 G(R-1,C)=0
1190 CALL HCHAR(ROW-2,COL,105)
1200 CALL HCHAR(ROW,COL,105)
1210 G(R,C)=0
1220 R=R-2
1230 GOTO 1470
1240 IF K<>83 THEN 1320
1250 IF (G(R,C-2)<>0)+(G(R,C-1)<>1) THEN 1530
1260 G(R,C-1)=0
1270 CALL HCHAR(ROW,COL-2,105)
1280 CALL HCHAR(ROW,COL,105)
1390 G(R,C)=0
1300 C=C-2
1310 GOTO 1470
1320 IF K<>68 THEN 1400
1330 IF (G(R,C+2)<>0)+(G(R,C+1)<>1) THEN 1530
1340 G(R,C+1)=0
1350 CALL HCHAR(ROW,COL+2,105)
1360 CALL HCHAR(ROW,COL,105)
1370 G(R,C)=0
1380 C=C+2
1390 GOTO 1470
1400 IF K<>88 THEN 1100
1410 IF (G(R+2,C)<>0)+(G(R+1,C)<>1)THEN 1530
1420 G(R+1,C)=0
1430 CALL HCHAR(ROW+2,COL,105)
1440 CALL HCHAR(ROW,COL,105)
1450 G(R,C)=0
1460 R=R+2
1470 G(R,C)=1
1480 ROW=R*2
1490 COL=C*2+4
1500 CALL HCHAR(ROW,COL,97)
1510 CALL SOUND(50,1400,2)
1520 GOTO 840
1530 CALL SOUND(100,135,2)
1540 GOTO 850