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).

source · imports t: lib/TTTML.xtl

The model

v : Float

value · line 16

The numbers of work/tttml.model: the codes, then the values.

v ← n̲umbers ⎕N̲GET "work/tttml.model"
Used in: m

m : Float

value · line 19

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

function · line 26

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

function · line 35

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

function · line 43

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
}
Used in: ᵘy̲ou!

ᵘp̲aint : Int -> Char -> Char

function · line 52

u:p_aint c t: text t in color c where the terminal draws colors (the fourth of []T_E's facts: a terminal, the live demo's pane), else as it is (piped to a file).

ᵘp̲aint ← { c t → 1 = 4 s̲elect ⎕T̲E @ ? (⎕C̲OLOR c) ⎕F̲G t◆ t }
Used in: ᵘv̲erdict

ᵘv̲erdict : Num a => a -> Char

function · line 55

u:v_erdict w: an outcome said from your side: 1 "you win" (green), -1 "it wins" (red), anything else "a draw".

ᵘv̲erdict ← { w → w = 1 ? 3 ᵘp̲aint "you win"◆ w = -1 ? 2 ᵘp̲aint "it wins"◆ "a draw" }