sourcegames/minesweeper/play.xtl
1⍝!/usr/bin/env xetal
2⍝# Minesweeper at the terminal: a 9 by 9 field with 10 mines. Type a row
3⍝# and a column to open a square ("5 5"), or f and a row and a column to
4⍝# flag one ("f 4 4"). Open every safe square to win. The rules are
5⍝# those of Mines.xtl.
6ᵐ⁼u̲se< "Mines"
7ᵖ⁼u̲se< "Play"
8said ← p̲rint! "MINESWEEPER: 9 BY 9, 10 MINES"
9
10⍝# A command: f (flag) or not, then a row and a column from 1 to 9;
11⍝# anything else is asked for again.
12ᵘa̲sk ← { s →
13 line ← ᵖl̲ine "OPEN ROW COL, OR F ROW COL ?"
14 v ← ᵖn̲umbersIn line
15 ok ← (2 = t̲ally v) ∧ '∧ r̲/ (v ≥ 1) ∧ v ≤ 9
16 ok ? (('∨ r̲/ line m̲ember? "fF") c̲at v)◆ ᵘa̲sk s
17}
18⍝# A turn: the field (as text and as a picture), a command, its result.
19ᵘt̲urn ← { s →
20 said ← p̲rint! ""
21 shown ← p̲rint! ᵐv̲iew s
22 pic ← ⎕S̲HOW ᵐp̲icture s
23 1 = ᵐs̲tatus s ? "YOU CLEARED THE FIELD!"
24 2 = ᵐs̲tatus s ? "BOOM! YOU HIT A MINE."
25 c ← ᵘa̲sk s
26 1 = f̲irst c ? ᵘt̲urn s ᵐf̲lag 1 d̲rop c◆ ᵘt̲urn s ᵐr̲eveal 1 d̲rop c
27}
28ᵘt̲urn ᵐn̲ew 0