sourcelib/Play.xtl
1⍝# Play: reading what a player types at the terminal, shared by the
2⍝# games' play.xtl programs. Every function prints its prompt, reads a
3⍝# line ([]R_EAD) and returns what it found; it never asks again itself
4⍝# (a game decides what is acceptable and asks again).
5
6⍝ :: Any a => a -> Char
7⍝# The line typed, as it was typed.
8ˡl̲ine ← { prompt →
9 shown ← p̲rint! prompt
10 ⎕R̲EAD @
11}
12⍝ :: Char -> Int
13⍝# The whole numbers in a line (other characters dropped first), a
14⍝# vector that may be empty.
15⍝# >> "p:" u_se< "Play"
16⍝# >> p:n_umbersIn "move 3 to 12, then -1"
17⍝# 3 12 -1
18ˡn̲umbersIn ← { line → f̲loor n̲umbers (line m̲ember? "0123456789 -") r̲eplicate line }
19⍝ :: Any a => a -> Int
20⍝# The numbers typed, a vector that may be empty.
21ˡn̲umbers ← { prompt → ˡn̲umbersIn ˡl̲ine prompt }
22⍝ :: Any a => a -> Int
23⍝# The first number typed, or -1 when there is none.
24ˡn̲umber ← { prompt → f̲irst (ˡn̲umbers prompt) c̲at -1 }
25⍝ :: Any a => a -> Char -> Int
26⍝# Which of the letters the line starts with, 1 to the number of letters
27⍝# (one more when it is none of them): "MOVE ?" p:l_etter "adws".
28ˡl̲etter ← { prompt letters → letters i̲ndexOf f̲irst (ˡl̲ine prompt) c̲at " " }