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

Your Move

Pick a square, guard against taking one already filled, and draw a cross — the board carries three states in one array.

33% of Three In A Row

The board exists; now the player takes a square. A move is a single number, 1 to 9, and because that number indexes straight into the board array, placing a cross is one line — guarded so the player cannot overwrite a square that is already taken.

  10 BORDER 0: PAPER 0: INK 7: CLS
  90 DIM b(9)
 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
 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
 300 GO TO 120
ZX Spectrum Three in a Row: the grid with a cyan X cross drawn at the centre square
A move places the player's cross — a cyan X drawn over the cell's number.

Read the square, guard it, mark it

Line 250 asks for a square and line 260 keeps it in range. Line 270 is the guard a board game needs: IF b(m) <> 0 — the cell is already taken, so say so and loop back instead of overwriting it. This is the same already-probed guard you wrote in Sonar, and it works for the same reason — the array value already records whether a cell is free. Line 280 then marks it: LET b(m) = 1, where 1 means the player's cross.

Three states, one array

A cell now holds one of three values, and the drawing loop reads them: 0 empty (shows its number), 1 the player's cross (a cyan X drawn with PLOT/DRAW, line 220), and 2 the computer's nought, coming soon. Storing several meanings as different numbers in one array is the pattern that has carried every Volume 2 game — and it makes the board self-documenting, because the squares still showing a number are exactly the ones still free.

Next: spot when three of those marks line up.