The Museum of HP Calculators

# 3-D Tic Tac Toe for the HP-41C

This program was first published in the HP-41C Users' Library Solutions: Games by Hewlett-Packard and is used here by permission.

This program is supplied without representation or warranty of any kind. Hewlett-Packard Company and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

## Overview

This program pits the HP-41C against a human opponent in a game of 3-D Tic Tac Toe. The rules of this game are simple:

1) The board consists of 4 levels, each of which is 4 rows deep and 4 columns across, making a total of 64 squares on a 3 dimensional board.
2) Two players move alternately by placing a black or white marker on a square (or making an X or a O on a paper layout of the board). Once a move is made, the piece is never moved or removed. In this game, the human always goes first.
3) A player wins by placing four markers in a straight line. The line can lie in more than one level, and diagonals are perfectly legitimate wins.

In short, the game is played just like regular Tic Tac Toe, except that the board has one additional dimension, and is one square bigger in all dimensions. Unlike regular Tic Tac Toe, there is no known winning strategy for the 3-D version. It is a much more complex game which can require considerable skill in a player, allowing for very complicated strategies.

The 41C plays and remembers the game by dividing the board into its 16 component rows and storing an entire row in one register. The registers R0 through R15 are reserved for the game board.

Each square on the board can be characterized by its level=z, its row=y and its column=x. x,y, and z can have values from 1 through 4. When entering moves, make sure they are 3 digit numbers. All three digits must be between 1 and 4 inclusive. Entering a move outside this range may cause the program to make erroneous entries in the board.

The boards look like:

Note:Requires 2 Memory Modules for use on the HP-41C.

## Instructions

 Step Instructions Input Data/Units Keys Output Data/Units 1 Enter program 2 Initialize [XEQ] 3DTTT READY 3 Key in  your move xyz [R/S] MY MOVE: Repeat 3 until you win or lose

## Example

```
Keystrokes:           Display:
[XEQ] [ALPHA] SIZE
[ALPHA] 026
[XEQ] [ALPHA]
242 [R/S]             MY MOVE: 322
414 [R/S]             MY MOVE: 134
123 [R/S]             MY MOVE: 234
441 [R/S]             MY MOVE: 423
141 [R/S]             MY MOVE: 232
214 [R/S]             MY MOVE: 114
424 [R/S]             MY MOVE: 434
111 [R/S]             334, I WIN
```

The boards look like:

## The Program

```LINE  KEYS
001 LBL "3DTTT"       ; Initialize
002  FIX 0
003  CLRG
004  CF 00
005  CF 01
006  CF 02
007  CF 03
008  CF 29
010  AVIEW              ;_________________
011 LBL A
012  STOP
013  1 E3               ; Input players
014  ÷                  ; move
015  STO 19
016  0
017  STO 16
018  2
019  STO 17
020  RDN
021  RDN
022  XEQ 10
023  STO 25
024  RCL 20
025  RCL IND 25
026  *
027  INT
028  1 E2
029  ÷
030  FRC
031  "ILLEGAL MOVE"
032  X!=0?
033  AVIEW              ; Illegal move
034  X!=0?
035  GTO A
036  5
037  RCL 20
038  ÷
039  1
040  +                  ; Store move
041  S1+ IND 25
042  RCL 18
043  X=0?
044  GTO 01
045  SF 00              ; Test move
046  XEQ 04
047  CF 02
048  CF 03
049  CF 00
050  .06
051  RCL 18
052  FRC
053  STO 18
054  X>Y?
055  GTO 02
056  X=Y?
057  GTO 03
058 LBL 01            ;_________________
059  1 E-2
060  ST+ 18             ; Increment round count
061  SF 02
062  GTO 03
063 LBL 02            ;_________________
064  XEQ 10
065  XEQ 04             ; Test machine move
066  FS?C 02
067  GTO 03
068  RCL 19
069  XEQ 10
070 LBL 03
071  RCL 16
072  INT
073  1
074  -
075  3
076  ÷
077  21
078  +
079  ENTER
080  FRC
081  X=0?
082  CF 01
083  .5
085  SF 00              ; test routine
086  GTO IND Z
087 LBL 10            ;_________________
088  10                 ; Parse move
089  *
090  INT
091  LASTX
092  FRC
093  10
094  *
095  INT
096  LASTX
097  FRC
098  .5
099  -
100  CHS
101  20
102  *
103  10X
104  STO 20
105  RDN
106  1
107  -
108  STO 21
109  X<>Y
110  1
111  -
112  4
113  *
114  STO 22
115  +
116  STO 23
117  RTN                ;_________________
118 LBL 04
119  1                  ; Initialize test controls
120  RCL 22
121  XEQ 01
122  4
123  RCL 21
124  XEQ 01
125  5
126  ENTER
12Z  0
128  XEQ 01
129  3
130  ENTER
131  XEQ 01
132  0
133  STO 24
134  RCL 23
135  STO 25
136  GTO 02
137 LBL 01            ;_________________
138  CF 01
139  STO 25             ; Set test points
140  RDN
141  STO 24
142  XEQ 02
143  SF 01
144  RCL 24
145  CHS
146  STO 24
147  XEQ 02
148  RCL 24
149  CHS
150  STO 24             ;_________________
151 LBL 02
152  1                  ; Testing cycle
153  ST+ 18
154  1 E2
155  ENTER
156  ENTER
157  RCL IND 25
158  X<> 25
159  RCL 24
160  +
161  X<> 25
162  FS? 01
163  *
164  RCL IND 25
165  +
166  X<> 25
167  RCL 24
168  +
169  X<> 25
170  FS? 01
171  *
172  RCL IND 25
173  +
174  X<> 25
175  RCL 24
176  +
177  X<> 25
178  FS? 01
179  *
180  RCL IND 25
181  +
182  FS? 01
183  GTO 01
184  roll up
185  RCL 20
186  ÷
187  ÷                  ;_________________
188 LBL 01
189  FRC                ; Analyze sum
190  roll up
191  *
192  INT
193  4
194  X<>Y
195  FS? 00
196  GTO 01
197  X>Y?
198  RTN
199  GTO 02
200 LBL 01
201  5                  ; Analyze player
202  ÷
203  "YOU WIN"
204  X=Y?
205  PROMPT             ; Player wins
206  FRC
207  X!=0?
208  RTN
209  LASTX
210 LBL 02            ;_________________
211  RCL 17             ; Test Tactical status
212  X>Y?
213  RTN
214  X<>Y
215  SF 02
216  STO 17
217  FC? 00
218  SF 03
219  RCL 18
220  STO 16
221  RTN                ;_________________
222 LBL 21
223  1
224  RCL 22
225  GTO 01
226 LBL 22
227  4
228  RCL 21
229  GTO 01
230 LBL 23
231  5
232  ENTER
233  0
234  GTO 01
235 LBL 24
236  3
237  ENTER
238  GTO 01
239 LBL 25
240  SF 01
241  0
242  RCL 23
243 LBL 01           ; Tactical play
244  STO 25
245  RDN
246  STO 24
247  1 E2
248  ENTER
249  1 E8
250  FS? 0
251  X<>Y
252  FS? 01
253  STO 20
254  RCL 20
255 LBL 05            ;_________________
256  XEQ 19
257  X<> 25            ; Find move
258  RCL 24
259  +
260  X<> 25
261  1 E-2
262  FS? 00
263  1/X
264  RCL 20
265  FS? 01
266  *
267  GTO 05             ;_________________
268 LBL 20
269  16
270  STO 20             ; Strategic play
271  4
272  STO 25
273  1
274  STO 24
275  XEQ 07
276  2
277  XEQ 07
278  3
279  XEQ 07
280  0
281  XEQ 08
282  RCL 22
283  4
284  STO 24
285  *
286  STO 23
287  16
288  STO 20
289  1
290  XEQ 08
291  2
292  XEQ 08
293  3
294  XEQ 08
295  0
296  XEQ 08
297  RCL 22
298  RCL 23
299  +
300  STO 25             ;_________________
301 LBL 09            ; Find move
302  RCL IND 25
303  RCL 24
304  X<=Y?
305  GTO 01
306  RCL 25
307  2
308  ÷
309  FRC
310  X=0?
311  GTO 02
312  GTO 03
313 LBL 01            ;_________________
314  RCL 25             ; Reset reg. #
315  +
316  16
317  X>Y?
318  CLX
319  -
320  STO 25
321  GTO 09
322 LBL 08            ;_________________
323  STO 25
324 LBL 07            ; Find total moves
325  RCL IND 25
326  X<> 25
327  RCL 24
328  +
329  X<> 25
330  RCL IND 25
331  +
332  X<> 25
333  RCL 24
334  +
335  X<> 25
336  RCL IND 25
337  +
338  X<> 25
339  RCL 24
340  +
341  X<> 25
342  RCL IND 25
343  +
344  X<> 25
345  RCL 24
346  +
347  X<> 25
348  INT
349  RCL 20
350  X<=Y?
351  RTN
352  RDN
353  STO 20
354  RDN
355  STO 22
356  RTN
357 LBL 02
358  1 E2               ; Find empty space
359  XEQ 19
360  1 E4
361  XEQ 19
362  1 E8
363  XEQ 19
364 LBL 03
365  1 E6
366  XEQ 19
367  1 E4
368  XEQ 19
369  1 E2
370  XEQ 19
371  1 E8
372 LBL 19
373  STO 20
374  RCL IND 25         ; Check/store move
375  *
376  INT
377  1 E2
378  ÷
379  FRC
380  X!=0?
381  RTN
382  RCL 20
383  1/X
384  ST+ IND 25
385  LOG
386  2
387  ÷
388  5
389  +
390  RCL 25
391  4
392  ÷
393  INT
394  LASTX
395  FRC
396  4
397  *
398  1
399  ST+ IND 25
400  +
401  X<>Y
402  1
403  +
404  10                 ; Place XYZ form
405  *
406  +
407  10
408  *
409  +
410  CLA
411  ARCL X
412  1 E3
413  ÷
414  FS?C 02
415  GTO 01
416  STO 18
417  RCL 17
418  3
419  X>Y?
420  GTO 01
421  FC? 03
422  GTO 01
423  ", I WIN"
424  AVIEW              ; 41C wins
425  GTO A
426 LBL 01
427  ASTO X
428  "MY MOVE"
429  ARCL X
430  AVIEW
431  GTO A              ; Get next player
432  .END.              ; move
```

## Register Use

```R00-R15  The Board
R16  Test #
R17  Tactic status
R18  machine move
R19  player move
R20  used
R21  Y-1
R22  Z-1
R23  Register #
R24  Position Inc.
R25  Used
```

00-03 Used