programgames/sudoku/play.xtl

source · imports s: games/sudoku/SudokuGrid.xtl; p: lib/Play.xtl

said : Char

value · line 9
said ← p̲rint! "SUDOKU: EVERY ROW, COLUMN AND BOX HOLDS 1 TO 9"

ᵘp̲uzzle : Unit -> Int

function · line 11
ᵘp̲uzzle ← { @ →
  n ← ᵖn̲umber "PUZZLE 1 TO 4 ?"
  (n ≥ 1) ∧ n ≤ 4 ? n◆ ᵘp̲uzzle @
}

ᵘr̲efused : Num a => a -> b -> Char

function · line 16

Why a move was refused (w from s:w_hy), as the player reads it.

ᵘr̲efused ← { w d →
  1 = w ? "ROW, COLUMN AND DIGIT ARE 1 TO 9 (DIGIT 0 CLEARS)"
  2 = w ? "THAT CELL IS GIVEN"
  @ f̲ormat< "{d} IS ALREADY IN THAT ROW, COLUMN OR BOX"
}
f̲ormat< expands to
(f̲ormat (d)) c̲at " IS ALREADY IN THAT ROW, COLUMN OR BOX"
Used in: ᵘa̲sk

ᵘh̲int : Int -> Char

function · line 21
ᵘh̲int ← { s →
  h ← ˢh̲int s
  0 = f̲irst h ? "A DIGIT IS WRONG SOMEWHERE: THE GRID HAS NO SOLUTION"
  @ f̲ormat< "TRY ROW {f_irst h} COLUMN {2 s_elect h}: {3 s_elect h}"
}
f̲ormat< expands to
"TRY ROW " c̲at (f̲ormat (f̲irst h)) c̲at " COLUMN " c̲at (f̲ormat (2 s̲elect h)) c̲at ": " c̲at (f̲ormat (3 s̲elect h))
Used in: ᵘa̲sk

ᵘc̲ell : Int -> Int -> Char

function · line 26
ᵘc̲ell ← { s rc →
  k ← 1 + (9 × (f̲irst rc) − 1) + (2 s̲elect rc) − 1
  @ f̲ormat< "ROW {f_irst rc} COLUMN {2 s_elect rc}: {w_here k s_elect s:l_egal s}"
}
f̲ormat< expands to
"ROW " c̲at (f̲ormat (f̲irst rc)) c̲at " COLUMN " c̲at (f̲ormat (2 s̲elect rc)) c̲at ": " c̲at (f̲ormat (w̲here k s̲elect ˢl̲egal s))
Used in: ᵘa̲sk

ᵘt̲urn : Int -> Char

function · line 31

A turn: the grid, then commands until a move changes it.

ᵘt̲urn ← { s →
  said ← p̲rint! ""
  shown ← p̲rint! ˢv̲iew s
  1 = ˢs̲tatus s ? "SOLVED!"
  ᵘa̲sk s
}

ᵘa̲sk : Int -> Char

function · line 39

A command: a letter (h, c, s, q) or a move, with its numbers. A hint, a cell's candidates or a refusal is printed and asked again.

ᵘa̲sk ← { s →
  line ← ᵖl̲ine "ROW COL DIGIT, H, C ROW COL, S OR Q ?"
  v ← ᵖn̲umbersIn line
  cmd ← (1 + "hHcCsSqQ" i̲ndexOf f̲irst line c̲at " ") d̲iv 2
  4 = cmd ? "STOPPED"
  3 = cmd ? ᵘs̲olution s
  1 = cmd ? s ᵘs̲ay ᵘh̲int s
  (2 = cmd) ∧ 2 = t̲ally v ? s ᵘs̲ay s ᵘc̲ell v
  3 ≠ t̲ally v ? s ᵘs̲ay "TYPE A ROW, A COLUMN AND A DIGIT, OR A LETTER"
  w ← s ˢw̲hy v
  0 = w ? ᵘt̲urn s ˢm̲ove v
  s ᵘs̲ay w ᵘr̲efused 3 s̲elect v
}

ᵘs̲ay : Int -> Char -> Char

function · line 52
ᵘs̲ay ← { s text →
  said ← p̲rint! text
  ᵘa̲sk s
}
Used in: ᵘa̲sk

ᵘs̲olution : Int -> Char

function · line 57

The puzzle's solution, and the game is over.

ᵘs̲olution ← { s →
  shown ← p̲rint! ˢs̲hown ˢs̲olve ˢg̲ivens s
  "THE SOLUTION"
}
Used in: ᵘa̲sk