sourcedemos/tttml-play.xtl
1⍝!/usr/bin/env xetal
2⍝# TTTML, part 2: play the machine that demos/tttml-train.xtl trained
3⍝# (run that first), with "xetal run demos/tttml-play.xtl". You are X and
4⍝# move first: type a square, 1 to 9, numbered row by row. Something
5⍝# that is not an empty square is asked for again. Uses the TTTML
6⍝# library (lib/TTTML.xtl).
7
8ᵗ⁼u̲se< "TTTML"
9
10⍝## The model
11
12⍝ The model, read back: []N_GET reads the file's text, n_umbers the
13⍝ numbers in it, and r_eshape makes the two rows again.
14
15⍝# The numbers of work/tttml.model: the codes, then the values.
16v ← n̲umbers ⎕N̲GET "work/tttml.model"
17⍝# The model: two rows, the codes of the positions met and their
18⍝# values.
19m ← (2 c̲at (t̲ally v) d̲iv 2) r̲eshape v
20t̲ally 1 s̲elect m
21
22⍝## The game
23
24⍝# u:a_sk s: your move on board s, read from the keyboard: the square
25⍝# typed, 1 to 9; anything but an empty square asks again.
26ᵘa̲sk ← { s →
27 shown ← p̲rint! "your move (1 to 9):"
28 t ← ⎕R̲EAD @
29 k ← "123456789" i̲ndexOf f̲irst t c̲at " "
30 (k ≤ 9) ∧ 0 = k s̲elect s c̲at 1 ? k◆ ᵘa̲sk s
31}
32
33⍝# u:y_ou s: you move on board s, then it replies, until the game is
34⍝# over; the final board. Each board is printed as it is played.
35ᵘy̲ou ← { s →
36 s ← s ᵗa̲fter ᵘa̲sk s
37 shown ← p̲rint! ᵗs̲how s
38 gap ← p̲rint! ""
39 0 = ᵗo̲utcome s ? ᵘm̲e s◆ s
40}
41⍝# u:m_e s: the machine moves on board s (its best move from model
42⍝# m), then you, until the game is over; the final board.
43ᵘm̲e ← { s →
44 s ← s ᵗa̲fter m ᵗc̲hoose s
45 shown ← p̲rint! ᵗs̲how s
46 gap ← p̲rint! ""
47 0 = ᵗo̲utcome s ? ᵘy̲ou s◆ s
48}
49⍝# u:v_erdict w: an outcome said from your side: 1 "you win", -1 "it
50⍝# wins", anything else "a draw".
51ᵘv̲erdict ← { w → w = 1 ? "you win"◆ w = -1 ? "it wins"◆ "a draw" }
52
53ᵘv̲erdict ᵗo̲utcome ᵘy̲ou 9 r̲eshape 0