Skip to contents

Returns the linear cell index of the AI's chosen move using depth-limited negamax with alpha-beta pruning. At difficulty >= 1, an immediate winning move is always taken and an immediate opponent threat is always blocked.

Usage

tsr_ai_move(board, difficulty = 2L, time_limit = NULL)

Arguments

board

A ttt_board.

difficulty

Integer in 1:4 (default 2L). Maps to search depth.

time_limit

Optional finite nonnegative elapsed-time budget in seconds. NULL (default) preserves unrestricted depth-limited search.

Value

Integer scalar: the chosen cell's linear index in 1:256.

Details

Tie-breaking among moves with equal evaluation is deterministic (lowest linear index wins).

Note: search is exponential in depth and runs in pure R; depth 4 may take several seconds on a 256-cell board. An optional cooperative time budget searches successively deeper levels and returns the last fully completed level. If none completes, the lowest legal cell is returned. Immediate wins and blocks are checked first, including with a zero budget. Checks occur between search operations; scheduling and a single operation can exceed the requested elapsed time. This is not a hard real-time limit.

Examples

tsr_ai_move(tsr_new_board(), difficulty = 1L)
#> [1] 1
# \donttest{
tsr_ai_move(tsr_new_board(), difficulty = 2L)
#> [1] 1
# }