3-D Tic Tac Toe

Type
Game
Category
Three-dimensional tic-tac-toe against a human or the computer
Author
Pierre Desloover
Published in
Antic, April 1986 (Vol. 4, No. 12), Game of the Month
Year
1986
Language
Atari BASIC
Requirements
32K RAM, Atari BASIC
Three green 3 by 3 grids drawn in perspective one above the other on a black screen, with SELECT MODE(A/B)? in the text window

About the program

3-D Tic Tac Toe plays the classic game on three nine-square boards stacked one above another. A row of three counts horizontally, vertically or diagonally, on one board or through all three. Antic made it the Game of the Month for April 1986. Mode A is "ho-hum human vs. human". Mode B pits you against the Atari, which the article called "crafty enough to beat you, yet smart enough not to win all the time".

Pierre Desloover of Seattle was a programmer for Micro-Phonics Technology, which made speech-recognition systems for IBM PCs.

Typing it in

The only special characters are the inverse lower-case title letters in line 1520 ({inv}…{/inv}: press the Atari key before and after). The magazine printed TYPO II codes for checking. Line 1580 and the DATA lines after it hold the screen positions of the 27 squares, and the long table of winning lines follows, so type those numbers carefully. The program uses MOVX as a variable name, which Atari BASIC accepts but some replacement BASICs don't.

How to play

Choose mode A or B, then a level from 1 to 3 (level 1 is easiest). Some squares then fill in solid green at random. They are a handicap and can't be part of a row. You are blue and the computer is red. Enter a square number from 1 to 27 and press RETURN. The squares aren't labelled. They count like reading a page, from top left to bottom right: 1–9 on the top board, 10–18 on the middle one and 19–27 on the bottom. Win and you're offered a harder level. The program keeps score for as many matches as you like. Enter 0 to end a game.

How it works

The article describes the computer's strategy as "nothing more than a set of rules that the computer blindly follows", modelled on a human player. Starting at line 1280, it asks three questions in turn:

  1. Can I win with one move? (lines 290–460)
  2. Must I block the opponent's win? (470–620)
  3. Which square best builds towards a win? (630–990)

Every winning line is stored, in every possible order, in the two-dimensional array TR, 455 pairs of screen X,Y coordinates in all. The index Z1 limits each search to the part of the table that matters, which saves time. Instead of keeping a separate board array, the program uses LOCATE to read the colour of a pixel inside each square on the GRAPHICS 7 screen. Squares are filled with the OS fill command (XIO 18, lines 200–220). The main routines are called through variables (GOSUB CONV, GOSUB SSI, GOSUB MV1) set up in line 1540, which makes the program easier to read.

Changes from the published listing

One change, so the program also runs under Altirra BASIC, the free replacement BASIC built into many emulators. Altirra BASIC adds a command called MOVE, so it rejected the published lines that use that word as the start of a variable name. The variable is renamed everywhere in the program. The new name is the same length, so the memory layout is unchanged and the program behaves exactly as published on genuine Atari BASIC. If you type it in from the magazine, the original names work fine on a real Atari.

Published Now Lines
MOVE MOVX 34 lines (120–1470)

Listing

10 REM 3D TIC-TAC-TOE
20 REM BY PIERRE DESLOOVER
30 REM (C) 1985, ANTIC PUBLISHING
40 GOSUB 1520
49 REM PLAY
50 GRAPHICS 7:K=2:COLOR K:SQ=27:X1=67:WIN=0:TIE=0:LN=SAV5:LN2=SAV4:TX$=" WON!":FOR Z1=0 TO 27:CH(Z1)=0:NEXT Z1
60 FOR Y1=10 TO 46 STEP 18:PLOT X1,Y1:DRAWTO X1+40,Y1:DRAWTO X1+25,Y1+15:DRAWTO X1-15,Y1+15:DRAWTO X1,Y1
70 PLOT X1+13,Y1:DRAWTO X1-2,Y1+15:PLOT X1+27,Y1:DRAWTO X1+12,Y1+15:PLOT X1-5,Y1+5:DRAWTO X1+35,Y1+5
80 PLOT X1-10,Y1+10:DRAWTO X1+30,Y1+10:NEXT Y1:IF CN=50 THEN GOSUB 1480:GOTO 1140
90 GOTO 110
100 CN=0
110 FOR T=0 TO 1
120 MOVX=INT(RND(0)*9)+1:IF CH(MOVX)<>0 THEN 120
130 CH(MOVX)=3:X1=TMB(MOVX,0):Y1=TMB(MOVX,1):GOSUB 200:NEXT T
140 FOR T=0 TO 2
150 MOVX=INT(RND(0)*9)+10:IF CH(MOVX)<>0 THEN 150
160 CH(MOVX)=3:X1=TMB(MOVX,0):Y1=TMB(MOVX,1):GOSUB 200:NEXT T
170 FOR T=0 TO 1
180 MOVX=INT(RND(0)*9)+19:IF CH(MOVX)<>0 THEN 180
190 CH(MOVX)=3:X1=TMB(MOVX,0):Y1=TMB(MOVX,1):GOSUB 200:NEXT T:GOTO 1220
199 REM FILL
200 X1=TMB(MOVX,0)-1:Y1=TMB(MOVX,1)-1:SQ=SQ-1:COLOR K:POKE 765,K:PLOT X1+12,Y1:DRAWTO X1+7,Y1+5:PLOT X1-5,Y1+5
210 POSITION X1,Y1:XIO 18,#6,0,0,"S:":SOUND 0,X1,10,6:SOUND 1,Y1*2,10,6:FOR T1=1 TO 35:NEXT T1:SOUND 0,0,0,0
220 SOUND 1,0,0,0:CLOSE #6:OPEN #6,44,7,"S:":X1=X1+1:Y1=Y1+1:RETURN
229 REM SSI
230 IF S1<=9 THEN Z1=1:MAX=147:RETURN
240 IF S1>=19 THEN Z1=148:MAX=294:RETURN
250 Z1=295:MAX=453:RETURN
259 REM CONV
260 S1=1
270 IF TMB(S1,0)=TR(Z1,0) AND TMB(S1,1)=TR(Z1,1) THEN MOVX=S1:RETURN
280 S1=S1+1:GOTO 270
289 REM INSP
290 Z1=1
300 IF Z1>MAX THEN RETURN
310 LOCATE TR(Z1,0),TR(Z1,1),R
320 IF R=1 THEN Z1=Z1+1:GOTO 350
330 IF R=3 THEN Z1=Z1+1:GOTO 410
340 Z1=Z1+3:GOTO 300
350 LOCATE TR(Z1,0),TR(Z1,1),R
360 IF R=1 THEN Z1=Z1+1:GOTO 380
370 Z1=Z1+2:GOTO 300
380 LOCATE TR(Z1,0),TR(Z1,1),R
390 IF R=1 THEN WIN=1:RETURN
400 Z1=Z1+1:GOTO 300
410 LOCATE TR(Z1,0),TR(Z1,1),R
420 IF R=3 THEN Z1=Z1+1:GOTO 440
430 Z1=Z1+2:GOTO 300
440 LOCATE TR(Z1,0),TR(Z1,1),R
450 IF R=3 THEN WIN=2:RETURN
460 Z1=Z1+1:GOTO 300
469 REM BLK
470 S1=MOVX:GOSUB 230
480 IF Z1>MAX THEN RETURN
490 IF TR(Z1,0)=X1 AND TR(Z1,1)=Y1 THEN 510
500 Z1=Z1+3:GOTO 480
510 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
520 IF R=0 THEN 550
530 IF R=3 THEN 590
540 Z1=Z1+2:GOTO 480
550 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
560 IF R=3 THEN 580
570 Z1=Z1+1:GOTO 480
580 Z1=Z1-1:GOSUB CONV:CH(MOVX)=1:K=1:POP :GOSUB 200:GOTO 1220
590 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
600 IF R=0 THEN 620
610 Z1=Z1+1:GOTO 480
620 GOSUB 260:CH(MOVX)=1:K=1:POP :GOSUB 200:GOTO 1220
629 REM MV1
630 Z1=1:S1=1
640 IF S1>MIN THEN RETURN
650 IF CH(S1)=1 THEN SAV1=TMB(S1,0):SAV2=TMB(S1,1):GOTO 670
660 S1=S1+1:GOTO 640
670 GOSUB SSI
680 IF Z1>MAX THEN S1=S1+1:GOTO 640
690 IF TR(Z1,0)=SAV1 AND TR(Z1,1)=SAV2 THEN 710
700 Z1=Z1+3:GOTO 680
710 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
720 IF R=0 THEN 780
730 IF R=1 THEN 750
740 Z1=Z1+2:GOTO 680
750 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
760 IF R=0 THEN WIN=1:GOSUB 260:CH(MOVX)=1:K=1:POP :GOSUB 200:GOSUB 1000
770 Z1=Z1+1:GOTO 680
780 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
790 IF R=1 THEN 810
800 Z1=Z1+1:GOTO 680
810 Z1=Z1-1:WIN=1:GOSUB 260:CH(MOVX)=1:K=1:POP :GOSUB 200:GOSUB 1000
819 REM MV2
820 Z1=1:S1=1:GOTO LN
830 GOTO 840+((CH(14)<>0)*10)+((CH(5)<>0)*10)+((CH(23)<>0)*10)
840 IF CH(5)=0 THEN LN=870:CH(5)=1:MOVX=5:K=1:POP :GOSUB 200:GOTO 1220
850 IF CH(14)=0 THEN LN=870:CH(14)=1:MOVX=14:K=1:POP :GOSUB 200:GOTO 1220
860 IF CH(23)=0 AND CH(5)=0 THEN LN=870:CH(23)=1:MOVX=23:K=1:POP :GOSUB 200:GOTO 1220
870 IF S1>MIN THEN RETURN
880 IF CH(S1)=1 THEN SAV1=TMB(S1,0):SAV2=TMB(S1,1):GOTO 900
890 S1=S1+1:GOTO 870
900 GOSUB 230
910 IF Z1>MAX THEN S1=S1+1:GOTO 870
920 IF TR(Z1,0)=SAV1 AND TR(S1,1)=SAV2 THEN 940
930 Z1=Z1+3:GOTO 910
940 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
950 IF R=0 THEN 970
960 Z1=Z1+2:GOTO 910
970 Z1=Z1+1:LOCATE TR(Z1,0),TR(Z1,1),R
980 IF R=0 THEN Z1=Z1-1:GOSUB 260:CH(MOVX)=1:K=1:POP :GOSUB 200:GOTO 1220
990 Z1=Z1+1:GOTO 910
999 REM WINCK
1000 IF WIN=0 AND TIE=0 THEN RETURN
1010 IF WIN=1 AND MODE=1 THEN WIN$=PLY1$:PLY1=PLY1+1:GOTO 1060
1020 IF WIN=2 AND MODE=1 THEN WIN$=PLY2$:PLY2=PLY2+1:GOTO 1060
1030 IF WIN=1 AND MODE=2 THEN WIN$=ACOMP$:PLY1=PLY1+1:GOTO 1060
1040 IF WIN=2 AND MODE=2 THEN WIN$=PLY2$:PLY2=PLY2+1:GOTO 1060
1050 TIES=TIES+1:WIN$="TIE!":TX$="    "
1060 POP :CLOSE #6:OPEN #6,56,7,"S:":GMS=GMS+1:? :? WIN$;TX$:GOSUB 1490+(WIN*10)+(TIE*10)
1070 TRAP 1070:? :? "NEXT GAME(Y/N)";:INPUT R$
1080 IF R$="Y" THEN GOTO PLAY
1090 ? :? :? "MODE ";CHR$(MODE+64);"  LEVEL ";LEV;" STATS":? :? "GMS ";GMS;" ";PLY1$;" ";PLY1;" ";PLY2$;" ";
1100 ? PLY2;" ";"TIES ";TIES
1110 TRAP 1110:? "NEW MODE/LEVEL(Y/N)";:INPUT R$:IF R$="Y" THEN OMOD=MOD:OLEV=LEV:GOTO 1140
1120 TRAP 1120:? :? "EXIT PROGRAM(Y/N)";:INPUT R$:IF R$="N" THEN 1070
1130 ? :? :? "BYE!":GOSUB 1500:END
1140 TRAP 1140:? :? "SELECT MODE(A/B)";:INPUT R$:IF R$<>"A" AND R$<>"B" THEN 1140
1150 MODE=ASC(R$)-64:IF MODE=1 AND MODE=OMOD THEN GOTO PLAY
1160 IF MODE=1 THEN PLY1$=" RED-SQR":LN1=1230:LEV=0:TURN$=PLY2$:GOSUB 1550:GOTO PLAY+CN
1170 PLY1$=ACOMP$:TRAP 1170:? :? "SELECT LEVEL(1/2/3)";:INPUT LEV:IF LEV<1 OR LEV>3 THEN 1170
1180 LEV=INT(LEV):IF MODE=OMOD AND LEV=OLEV THEN GOTO PLAY+CN
1190 GOSUB 1550:LN1=1310:IF LEV=1 THEN LN=840:SAV5=LN:LN2=1400:SAV4=LN2:GOTO PLAY+CN
1200 LN=830:SAV5=LN:IF LEV=2 THEN LN2=1320:SAV4=LN2:GOTO PLAY+CN
1210 LN2=1340:SAV4=LN2:GOTO PLAY+CN
1219 REM LOOP
1220 POP :MAX=453:CLOSE #6:OPEN #6,56,7,"S:":GOTO LN1
1230 IF TURN$=PLY1$ THEN TURN$=PLY2$:K=3:GOTO 1250
1240 TURN$=PLY1$:K=1
1250 TRAP 1250:? INST$:? :? TURN$;"'S MOVE...":INPUT MOVX:IF MOVX<0 OR MOVX>27 THEN 1250
1255 IF MOVX=0 THEN TIES=TIES+1:GMS=GMS+1:GOTO 1070
1260 MOVX=INT(MOVX):IF CH(MOVX)<>0 THEN 1250
1270 CH(MOVX)=9:GOSUB 200:IF SQ>=16 THEN 1300
1280 GOSUB 290:GOSUB 1000
1290 IF SQ=0 THEN TIE=1:GOTO 1000
1300 GOTO 1220
1310 GOTO LN2
1320 MOVX=INT(RND(0)*27)+1:IF CH(MOVX)<>0 THEN 1320
1330 LN2=1400:CH(MOVX)=1:K=1:GOSUB 200:GOTO LN2
1340 MOVX=INT(RND(0)*27)+1:IF CH(MOVX)<>0 THEN 1340
1350 IF MOVX=5 OR MOVX=14 OR MOVX=23 THEN 1390
1360 IF CH(5)<>0 AND CH(14)<>0 AND CH(23)<>0 THEN 1380
1370 GOTO 1340
1380 MOVX=INT(RND(0)*27)+1:IF CH(MOVX)<>0 THEN 1380
1390 LN2=1400:CH(MOVX)=1:K=1:GOSUB 200
1400 CLOSE #6:OPEN #6,56,7,"S:":TRAP 1400:? INST$:? :? PLY2$;"'S MOVE...":INPUT MOVX
1405 IF MOVX<0 OR MOVX>27 THEN 1400
1406 IF MOVX=0 THEN TIES=TIES+1:GMS=GMS+1:GOTO 1070
1410 MOVX=INT(MOVX):IF CH(MOVX)<>0 THEN 1400
1420 CH(MOVX)=2:K=3:GOSUB 200:IF SQ>=16 THEN 1440
1430 GOSUB 290:GOSUB 1000
1440 GOSUB MV1:GOSUB 470:GOSUB MV2
1450 IF SQ=0 THEN TIE=1:GOTO 1000
1460 MOVX=INT(RND(0)*27)+1:IF CH(MOVX)<>0 THEN 1460
1470 CH(MOVX)=1:K=1:GOSUB 200:GOSUB 290:GOSUB 1000:GOTO 1220
1479 REM FLASH COLORS
1480 SAV3=PEEK(REG):FOR HUE=0 TO 12:FOR LUM=0 TO 16 STEP 2:POKE REG,HUE*16+LUM:NEXT LUM:NEXT HUE
1490 POKE REG,SAV3:RETURN
1499 REM SLIDE WHISTLE
1500 SOUND 1,50,6,5:FOR T=0 TO 76:SOUND 2,T,10,4:NEXT T:SOUND 1,0,0,0:SOUND 2,0,0,0:GOSUB 1480:RETURN
1510 SOUND 2,50,6,4:FOR T=76 TO 1 STEP -1:SOUND 1,T,10,5:NEXT T:SOUND 1,0,0,0:SOUND 2,0,0,0:GOSUB 1480:RETURN
1519 REM GAME INITIALIZATIONS
1520 GRAPHICS 18:POSITION 9,3:? #6;"{inv}3d{/inv}":POSITION 5,5:? #6;"{inv}tIC-tAC-tOE{/inv}"
1530 REG=709:GOSUB 1560:MIN=27:CN=50:POKE 702,64
1540 PLAY=50:FILL=200:SSI=230:CONV=260:INSP=290:BLK=470:MV1=630:MV2=820:WINCK=1000:LOOP=1220
1550 PLY1=0:PLY2=0:TIES=0:GMS=0:RETURN
1560 DIM TR(454,1),TMB(27,1),CH(27),R$(1),WIN$(8),PLY1$(8),PLY2$(8),ACOMP$(8),TURN$(8),TX$(5),INST$(37)
1570 PLY2$="BLUE-SQR":ACOMP$="COMPUTER":INST$="Enter Sq.[1-27] or [0] to term. game."
1580 FOR Z1=1 TO 27:READ X1,Y1:TMB(Z1,0)=X1:TMB(Z1,1)=Y1:NEXT Z1
1590 DATA 68,11,82,11,96,11,63,16,77,16,91,16,58,21,72,21,86,21,68,29,82,29,96,29,63,34,77,34,91,34,58,39,72,39
1600 DATA 86,39,68,47,82,47,96,47,63,52,77,52,91,52,58,57,72,57,86,57
1610 FOR Z1=1 TO 454:READ X1,Y1:TR(Z1,0)=X1:TR(Z1,1)=Y1:NEXT Z1:RETURN
1620 DATA 68,11,82,11,96,11,68,11,63,16,58,21,68,11,77,16,86,21,68,11,68,29,68,47,68,11,63,34,58,57,68,11,77,34
1630 DATA 86,57,68,11,82,29,96,47,82,11,68,11,96,11,82,11,77,16,72,21,82,11,82,29,82,47,82,11,77,34,72,57,96,11
1640 DATA 82,11,68,11,96,11,91,16,86,21,96,11,77,16,58,21,96,11,96,29,96,47,96,11,91,34,86,57,96,11,77,34,58,57
1650 DATA 96,11,82,29,68,47,63,16,68,11,58,21,63,16,77,16,91,16,63,16,63,34,63,52,63,16,77,34,91,52,77,16,68,11
1660 DATA 86,21,77,16,96,11,58,21,77,16,63,16,91,16,77,16,82,11,72,21,77,16,77,34,77,52,91,16,96,11,86,21,91,16
1670 DATA 77,16,63,16,91,16,91,34,91,52,91,16,77,34,63,52,58,21,63,16,68,11,58,21,72,21,86,21,58,21,77,16,96,11
1680 DATA 58,21,58,39,58,57,58,21,63,34,68,47,58,21,72,39,86,57,58,21,77,34,96,47,72,21,58,21,86,21,72,21,77,16
1690 DATA 82,11,72,21,72,39,72,57,72,21,77,34,82,47,86,21,91,16,96,11,86,21,72,21,58,21,86,21,77,16,68,11,86,21
1700 DATA 86,39,86,57,86,21,91,34,96,47,86,21,72,39,58,57,86,21,77,34,68,47,68,47,63,52,58,57,68,47,82,47,96,47
1710 DATA 68,47,77,52,86,57,68,47,68,29,68,11,68,47,63,34,58,21,68,47,82,29,96,11,68,47,77,34,86,21,82,47,68,47
1720 DATA 96,47,82,47,77,52,72,57,82,47,82,29,82,11,82,47,77,34,72,21,96,47,91,52,86,57,96,47,82,47,68,47,96,47
1730 DATA 77,52,58,57,96,47,96,29,96,11,96,47,91,34,86,21,96,47,82,29,68,11,96,47,77,34,58,21,63,52,58,57,68,47
1740 DATA 63,52,77,52,91,52,63,52,63,34,63,16,63,52,77,34,91,16,77,52,82,47,72,57,77,52,63,52,91,52,77,52,96,47
1750 DATA 58,57,77,52,68,47,86,57,77,52,77,34,77,16,91,52,96,47,86,57,91,52,77,52,63,52,91,52,91,34,91,16,91,52
1760 DATA 77,34,63,16,58,57,63,52,68,47,58,57,72,57,86,57,58,57,77,52,96,47,58,57,58,39,58,21,58,57,63,34,68,11
1770 DATA 58,57,72,39,86,21,58,57,77,34,96,11,72,57,77,52,82,47,72,57,58,57,86,57,72,57,72,39,72,21,72,57,77,34
1780 DATA 82,11,86,57,91,52,96,47,86,57,72,57,58,57,86,57,77,52,68,47,86,57,86,39,86,21,86,57,91,34,96,11,86,57
1790 DATA 72,39,58,21,86,57,77,34,68,11,68,29,63,34,58,39,68,29,82,29,96,29,68,29,77,34,86,39,68,29,68,11,68,47
1800 DATA 82,29,77,34,72,39,82,29,68,29,96,29,82,29,82,47,82,11,82,29,96,47,68,11,82,29,68,47,96,11,96,29,82,29
1810 DATA 68,29,96,29,91,34,86,39,96,29,77,34,58,39,96,29,96,47,96,11,63,34,68,29,58,39,63,34,77,34,91,34,63,34
1820 DATA 63,16,63,52,63,34,68,47,58,21,63,34,58,57,68,11,77,34,77,52,77,16,77,34,68,29,86,39,77,34,82,29,72,39
1830 DATA 77,34,96,29,58,39,77,34,91,34,63,34,77,34,86,39,68,29,77,34,72,39,82,29,77,34,58,39,96,29,77,34,63,34
1840 DATA 91,34,77,34,96,47,58,21,77,34,68,11,86,57,77,34,63,16,91,52,77,34,82,11,72,57,77,34,96,11,58,57,77,34
1850 DATA 91,16,63,52,77,34,86,21,68,47,77,34,72,21,82,47,91,34,96,29,86,39,91,34,77,34,63,34,91,34,91,52,91,16
1860 DATA 91,34,96,47,86,21,91,34,86,57,96,11,58,39,63,34,68,29,58,39,72,39,86,39,58,39,77,34,96,29,58,39,58,57
1870 DATA 58,21,72,39,77,34,82,29,72,39,86,39,58,39,72,39,72,57,72,21,72,39,86,57,58,21,72,39,58,57,86,21,86,39
1880 DATA 91,34,96,29,86,39,72,39,58,39,86,39,77,34,68,29,86,39,86,57,86,21,0,0

Download

The original program from the Antic disk, to LOAD or ENTER instead of typing it: TTT.BAS.

Credits