Skip to content
Game 13 Unit 5 of 6 1 hr learning time

Strategy

Replace the random fallback with positional sense — take the centre, then a corner, then an edge — and the computer plays well enough to force draws.

83% of Three In A Row

The opponent wins and blocks, but when neither applies it still flails at a random square — and a random opening move is how you beat it. The fix is positional sense: when there is no win to take and no threat to stop, prefer the squares that matter. With that, the computer plays a sound game, and most games end in a draw.

  10 BORDER 0: PAPER 0: INK 7: CLS
  80 RANDOMIZE
  90 DIM b(9)
 100 LET moves = 0
 120 CLS
 130 INVERSE 1: PRINT AT 0, 0; "    *** THREE IN A ROW ***      ": INVERSE 0
 150 INK 7: FOR i = 0 TO 3: PLOT 80 + i * 32, 55: DRAW 0, 96: NEXT i
 160 FOR i = 0 TO 3: PLOT 80, 55 + i * 32: DRAW 96, 0: NEXT i
 170 FOR n = 1 TO 9
 180 LET row = INT ((n - 1) / 3)
 190 LET col = n - 1 - row * 3
 200 LET cx = 100 + col * 32: LET cy = 131 - row * 32
 210 IF b(n) = 0 THEN PRINT AT 5 + row * 4, 12 + col * 4; CHR$ (48 + n)
 220 IF b(n) = 1 THEN INK 5: PLOT cx - 8, cy - 8: DRAW 16, 16: PLOT cx - 8, cy + 8: DRAW 16, -16: INK 7
 230 IF b(n) = 2 THEN INK 2: CIRCLE cx, cy, 8: INK 7
 240 NEXT n
 250 INPUT "Your move (1-9): "; m
 260 IF m < 1 OR m > 9 THEN GO TO 250
 270 IF b(m) <> 0 THEN PRINT AT 18, 4; "Already taken!": PAUSE 30: GO TO 120
 280 LET b(m) = 1
 290 LET moves = moves + 1
 300 GO SUB 420
 310 IF winner = 1 THEN PRINT AT 18, 4; "You win!": STOP
 330 IF moves = 9 THEN PRINT AT 18, 4; "Draw!": STOP
 340 GO SUB 600
 350 LET b(mv) = 2
 360 LET moves = moves + 1
 370 BEEP 0.05, 5
 380 GO SUB 420
 390 IF winner = 2 THEN PRINT AT 18, 4; "Computer wins!": STOP
 410 GO TO 120
 420 REM --- Check winner ---
 430 RESTORE 1200
 440 LET winner = 0
 450 FOR w = 1 TO 8
 460 READ p, q, f
 470 IF b(p) <> 0 AND b(p) = b(q) AND b(q) = b(f) THEN LET winner = b(p)
 480 NEXT w
 490 RETURN
 600 REM --- Computer: win, block, strategy ---
 610 LET mv = 0
 620 RESTORE 1200
 630 FOR w = 1 TO 8
 640 READ p, q, f
 650 IF mv = 0 AND b(p) = 2 AND b(q) = 2 AND b(f) = 0 THEN LET mv = f
 660 IF mv = 0 AND b(p) = 2 AND b(f) = 2 AND b(q) = 0 THEN LET mv = q
 670 IF mv = 0 AND b(q) = 2 AND b(f) = 2 AND b(p) = 0 THEN LET mv = p
 680 NEXT w
 690 IF mv > 0 THEN GO TO 850
 700 RESTORE 1200
 710 FOR w = 1 TO 8
 720 READ p, q, f
 730 IF mv = 0 AND b(p) = 1 AND b(q) = 1 AND b(f) = 0 THEN LET mv = f
 740 IF mv = 0 AND b(p) = 1 AND b(f) = 1 AND b(q) = 0 THEN LET mv = q
 750 IF mv = 0 AND b(q) = 1 AND b(f) = 1 AND b(p) = 0 THEN LET mv = p
 760 NEXT w
 770 IF mv > 0 THEN GO TO 850
 780 IF b(5) = 0 THEN LET mv = 5: GO TO 850
 790 IF b(1) = 0 THEN LET mv = 1: GO TO 850
 800 IF b(3) = 0 THEN LET mv = 3: GO TO 850
 810 IF b(7) = 0 THEN LET mv = 7: GO TO 850
 820 IF b(9) = 0 THEN LET mv = 9: GO TO 850
 830 FOR i = 1 TO 9: IF b(i) = 0 THEN LET mv = i
 840 NEXT i
 850 RETURN
1200 DATA 1,2,3, 4,5,6, 7,8,9
1210 DATA 1,4,7, 2,5,8, 3,6,9
1220 DATA 1,5,9, 3,5,7
ZX Spectrum Three in a Row: a board filled with crosses and noughts and the message Draw!
With a sense of position, the computer holds its own — a well-played game fills the board with no winner.

Best square first

The win and block scans are unchanged; only the fallback changes. Lines 780–820 replace the random pick with a priority list: take the centre (b(5)) if it is free, then a corner (1, 3, 7, 9), and only then settle for whatever is left (lines 830–840). Each line jumps out the moment it finds a free square, so the order is the strategy — centre controls the most lines, corners the next most, edges the fewest. Positional judgement that a strong player applies by instinct becomes five ordered IFs.

Why the game now draws

Noughts and Crosses is a solved game: when both sides play soundly, neither can win. Your opponent now plays soundly — it never misses a win, never lets one through, and opens on strong squares — so a careful player can hold it to a draw but not beat it (line 330 declares the draw once all nine moves are spent with no winner). Reaching that point is the lesson: a short ladder of rules, with no lookahead at all, is enough to play a small game correctly.

The game is complete and fair. The last unit frames it: a title, proper win/lose/draw screens, a running score, and a play-again loop.