programdemos/tttml-play.xtl
TTTML, part 2: play the machine that demos/tttml-train.xtl trained (run that first), with "xetal run demos/tttml-play.xtl". You are X and move first: type a square, 1 to 9, numbered row by row. Something that is not an empty square is asked for again. Uses the TTTML library (lib/TTTML.xtl).
The model
v : Float
The numbers of work/tttml.model: the codes, then the values.
v ← n̲umbers ⎕N̲GET "work/tttml.model"
m : Float
The model: two rows, the codes of the positions met and their values.
m ← (2 c̲at (t̲ally v) d̲iv 2) r̲eshape v
The game
ᵘa̲sk! : Num a => a -> Int
u:a_sk! s: your move on board s, read from the keyboard: the square typed, 1 to 9; anything but an empty square asks again.
ᵘa̲sk! ← { s → _ ← p̲rint! "your move (1 to 9):" t ← ⎕R̲EAD @ k ← "123456789" i̲ndexOf f̲irst t c̲at " " (k ≤ 9) ∧ 0 = k s̲elect s c̲at 1 ? k◆ ᵘa̲sk! s }
ᵘy̲ou! : Int -> Int
u:y_ou! s: you move on board s, then it replies, until the game is over; the final board. Each board is printed as it is played.
ᵘy̲ou! ← { s → played ← s ᵗa̲fter ᵘa̲sk! s _ ← p̲rint! ᵗs̲how played _ ← p̲rint! "" 0 = ᵗo̲utcome played ? ᵘm̲e! played◆ played }
ᵘm̲e! : Int -> Int
u:m_e! s: the machine moves on board s (its best move from model m), then you, until the game is over; the final board.
ᵘm̲e! ← { s → played ← s ᵗa̲fter m ᵗc̲hoose s _ ← p̲rint! ᵗs̲how played _ ← p̲rint! "" 0 = ᵗo̲utcome played ? ᵘy̲ou! played◆ played }