Classic Computer Magazine Archive ANTIC VOL. 4, NO. 12 / APRIL 1986

game of the month


Can you beat your Atari?


3-D Tic Tac Toe is a three-dimensional computerized version of the classic strategy game. This BASIC program can play a rather pedestrian human vs. human game, or a highly challenging match between you and your computer. It will work on any 8-bit Atari computer with 32K memory

3-D Tic Tac Toe is crafty enough to beat you, yet smart enough not to win all the time! (After all, if your Atari starts getting too smug about its tic tac toe prowess, you're likely to call a halt by pulling the plug.)
   To get started, type in Listing 1, TTT.BAS, check it with TYPO II and SAVE a copy before you RUN it.
   After the title screen appears, three green grids will appear on the screen. Select [A] for ho-hum human vs. human tic-tac-toe. Or be daring and select [B] to battle a formidable opponent-your Atari computer.

Pick a level of difficulty. Level one is easier than level three. After you have chosen, some of the squares in the grids will randomly fill up in solid green. The filled squares are your difficulty handicap and cannot be used to make a match.
   From here on, it's classic tic tac toe with the added challenge of three dimensions. Match up three squares in a row horizontally, diagonally or vertically-among any (or all) of the three nine-square boards. Meanwhile, try to keep one step ahead of your Atari by blocking any rows it tries to fill.
   You're blue. The computer is red. Enter a number 1-27 to move onto a space, then press [RETURN]. If you win by placing three squares in a row in any direction, you'll be prompted to move to a more difficult level.
   The only hitch is that the squares aren't numbered. Going from upper left to lower right (just as you do when you're reading) the highest board is numbered 1-9, the middle board is 10-18, and the bottom board is 19-27. You can refer to Figure 1 as you play.
   The computer will keep score (without cheating) for as many matches as you care to play. When you're ready to quit, just type [0] to end the game.

The algorithm used in 3-D Tic Tac Toe is nothing more than a set of rules that the computer blindly follows. Modeled after the way a human would play tic tac toe, it begins at line 1280 as a series of subroutine calls.
   Imagine that a game is in progress and it's your turn. As a crafty tic tac toe player you would probably go through the following procedures:

   1. Examine the boards to determine if selection of one square can produce a winning combination on this turn. If not:

   2. Examine the boards to determine if it's necessary to block your opponent from winning on the next turn. If not:

   3. Examine the boards to detennine which square would most likely advance you towards a winning combination.

   The algorithm needs to recognize, examine and interpret developing cubic tic tac toe patterns on the playing boards, as well as matching and comparing them with established winning patterns. So it's necessary to find an efficient way to represent this information and make it available to the algorithm. This is accomplished in 3-D Tic Tac Toe with a combination of arrays (indexes subject to minimum and maximum allowable values) and Atari BASIC's LOCATE command.
   Every possible win is stored in every conceivable order in a two-dimensional array called TR. Each row of the array stores the X and Y screen coordinates corresponding to a point (or pixel) within the interior surface of a square represented on the actual screen. Each pair of X,Y coordinates defines one square. Three of these pairs in a row, horizontally, diagonally, etc., make up a winning pattern.

Numbered Square Layout
   An index, or pointer, Z1, enables the program to selectively access and compare any individual square or combination of squares stored within array TR. Z1 is assigned initial minimum and maximum values to define the range of the search and to limit the number of combinations checked. This greatly reduces the am-mount of time the computer needs to find a winning row. Index Z1 is incremented or decremented by the algorithm as needs require. You'll find SSI, the subroutine that sets up this search index, located at line 230.
   The program uses the LOCATE command extensively as a convenient way to extract information directly from the screen. Also the square currently being used by the game is noted internally, using a one-dimensional array (or list) CH. Each element of CH corresponds to a single square and is used to record a unique number. The algorithm interprets the number as a square's usage (or state) code. Four of these state codes are possible. After each player takes a turn, CH and its values are updated.
   CH has one-to-one correspondence with TMB, a third array used in the program. TMB provides unique XY coordinates to be used by the square as a go-between for the one-to-many relationships existing between TMB and TR.

Lines 50-190-Reset and draw boards for new game.

   200-220-XIO fill square and sound.

   230-250 - Set search index limits.

   260-280 - Convert index Sl to MOVE.

   290-460 - Inspect for existing win combination.

   470-620 - Block opponent if necessary.

   630-810 - Choose a winning square.

   820-990 - Advance towards win with logical choice.

   1000-1210 - Game end detect, user options.

   1220-1470 - Main playing control loop.

   1480-1490 - Flash colors.

   1500-1510 - Sound slide whistles.

   1520-1880 - Game initializations, data.

Pierre Desloover of Seattle, Washington is a programmer for Micro-Phonics Technology, a maker of speech recognition systems for IBM PC's and compatibles. He is the author of Anti Pong, available on Antic Public Domain Disk #PDOO9.

Listing 1   TTT.BAS Download