Skip to contents

Tutorial page 4 of 4. Previous: « Seeing in four dimensions · Back to the start: The rules

The app is the friendly front end, but the whole game is scriptable. This page is the console version of the same moves.

A board and a move

A game starts from an empty board. X is always to move first.

b <- tsr_new_board()
b
#> 
#> ── <ttt_board> ──
#> 
#> 4x4x4x4 board, 256 cells
#> moves played: 0
#> to move: X
#> legal moves: 256

Make a move with either explicit coordinates or a linear cell index — the two calls below are equivalent ways of naming cell 1, (0, 0, 0, 0):

b <- tsr_move(b, 0L, 0L, 0L, 0L)   # X at (0,0,0,0)
b <- tsr_move(b, cell = 17L)        # O at cell 17 = (0,0,1,0)
b <- tsr_move(b, 1L, 0L, 0L, 0L)   # X at (1,0,0,0)
b
#> 
#> ── <ttt_board> ──
#> 
#> 4x4x4x4 board, 256 cells
#> moves played: 3
#> to move: O
#> legal moves: 253

tsr_status() is the machine-readable summary the app shows in its sidebar:

tsr_status(b)
#> # A tibble: 1 × 6
#>   winner is_full is_over n_moves to_move n_legal
#>    <int> <lgl>   <lgl>     <int>   <int>   <int>
#> 1      0 FALSE   FALSE         3       2     253

Made a mistake? tsr_undo() rolls back one or more plies:

b <- tsr_undo(b, n = 1L)    # take back O's reply... err, the last move
tsr_status(b)$n_moves
#> [1] 2

tsr_legal_moves() returns the cell indices that are still empty:

moves <- tsr_legal_moves(b)
length(moves)
#> [1] 254
head(moves, 8)
#> [1] 2 3 4 5 6 7 8 9

Asking the AI for a move

tsr_ai_move() returns the cell the negamax engine would play, with a tunable search depth via difficulty (1–4):

ai_cell <- tsr_ai_move(b, difficulty = 2L)
ai_cell
#> [1] 4
b <- tsr_move(b, cell = ai_cell)
tsr_status(b)
#> # A tibble: 1 × 6
#>   winner is_full is_over n_moves to_move n_legal
#>    <int> <lgl>   <lgl>     <int>   <int>   <int>
#> 1      0 FALSE   FALSE         3       2     253

A complete game loop

Putting it together: a short game where you play a fixed opening and the AI answers each move. (In a real session you would choose moves interactively; here the human side is scripted so the article is reproducible.)

set.seed(1)
b <- tsr_new_board()
my_moves <- c(1L, 2L, 3L, 86L, 171L)   # a scripted "human" (player X)

for (cell in my_moves) {
  if (tsr_status(b)$is_over) break
  legal <- tsr_legal_moves(b)
  if (!(cell %in% legal)) cell <- legal[1]            # fall back if AI took it
  b <- tsr_move(b, cell = cell)                       # X (you)
  if (tsr_status(b)$is_over) break
  b <- tsr_move(b, cell = tsr_ai_move(b, difficulty = 2L))  # O (AI)
}

tsr_status(b)
#> # A tibble: 1 × 6
#>   winner is_full is_over n_moves to_move n_legal
#>    <int> <lgl>   <lgl>     <int>   <int>   <int>
#> 1      0 FALSE   FALSE        10       1     246

The board after a short scripted game between a human opening and the AI.

Evaluating a position

Three functions read a position, all sharing one evaluation core:

tsr_evaluate(b)                       # raw score for the side to move
#> [1] -53
tsr_win_prob(b, method = "heuristic") # squashed to a 0..1 win probability
#> [1] 0.3857965

tsr_rate_moves() scores every legal move at once and is the engine behind the app’s overlay. The columns is_best, is_winning, and is_blocking are the quickest way to find the move that matters:

rated <- tsr_rate_moves(b, method = "heuristic")
head(rated[order(-rated$win_prob),
           c("cell", "i", "j", "k", "l", "win_prob", "is_best")], 5)
#> # A tibble: 5 × 7
#>    cell     i     j     k     l win_prob is_best
#>   <int> <int> <int> <int> <int>    <dbl> <lgl>  
#> 1   253     0     3     3     3    0.474 TRUE   
#> 2    22     1     1     1     0    0.454 FALSE  
#> 3    43     2     2     2     0    0.454 FALSE  
#> 4    70     1     1     0     1    0.454 FALSE  
#> 5    82     1     0     1     1    0.454 FALSE

Where to go next

Back to the start: The rules of 4D tic-tac-toe »