sourcedemos/tttml.xtl

1⍝!/usr/bin/env xetal 2⍝# TTTML: a machine that learns tic-tac-toe by playing itself, from the 3⍝# TTTML library (lib/TTTML.xtl, ported from sw-apl's TTTML workspace; 4⍝# docs/literate/tttml.org explains it). Run it with 5⍝# "xetal run demos/tttml.xtl", or as a notebook with "just tttml" (the 6⍝# optimized build: training takes seconds). 7⍝# 8⍝# It shows a board and its outcome, the codes that make turned and 9⍝# reflected boards one position, then trains a model on 2000 games 10⍝# against itself and measures it against a random player and itself. 11 12ᵗ⁼u̲se< "TTTML" 13 14⍝## Boards and positions 15 16⍝ A board: 1 for X, -1 for O, 0 for an empty square. 17ᵗs̲how 1 -1 0 0 1 0 0 0 -1 18 19⍝ Who has won: 1 X, -1 O, 2 a draw, 0 not over. 20ᵗo̲utcome 1 1 1 -1 -1 0 0 0 0 21ᵗo̲utcome 1 -1 1 1 -1 -1 -1 1 1 22 23⍝ A position's code is the same for the board turned or reflected. 24ᵗc̲ode 1 0 0 0 0 0 0 0 0 25ᵗc̲ode 0 0 1 0 0 0 0 0 0 26 27⍝## Training and measuring 28 29⍝# The model learned from 2000 games against itself. 30m ← 2000 ᵗt̲rain! ᵗempty 31⍝ How many positions it met. 32t̲ally 1 s̲elect m 33 34⍝ 50 games against a random player as X, then 50 as O: won, lost, 35⍝ drawn. 3650 ᵗt̲rial! m 37 38⍝ 50 games against itself, its best move on both sides from a random 39⍝ first move: X won, O won, drawn. Played well, all are draws. 4050 ᵗs̲elfTrial! m 41 42⍝ The game it plays against itself, move by move. 43ᵗb̲oards ᵗb̲est m