sourcelib/Board.xtl
1⍝# Board: square boards as arrays, shared by the games. Rows and columns
2⍝# count from 0; a square's number counts from 1, row by row.
3
4⍝ :: Num a => a -> a -> a
5⍝# The number of square rc (row, column) on a board n wide; rc may hold
6⍝# many squares, as a 2-row matrix (rows, then columns).
7⍝# >> "b:" u_se< "Board"
8⍝# >> 3 b:c_ell 1 2
9⍝# 6
10⍝# >> 3 b:c_ell 2 2 r_eshape 0 2 1 0
11⍝# 2 7
12ˡc̲ell ← { n rc → 1 + (n × 1 s̲elect rc) + 2 s̲elect rc }
13⍝ :: Int -> Int -> Int
14⍝# The row and column of squares k on a board n wide (a 2-row matrix for
15⍝# many squares).
16⍝# >> "b:" u_se< "Board"
17⍝# >> 3 b:r_c 6
18⍝# 1 2
19⍝# >> 3 b:r_c 1 9
20⍝# 0 2
21⍝# 0 2
22ˡr̲c ← { n k → (2 c̲at s̲hape k) r̲eshape ((k − 1) d̲iv n) c̲at (k − 1) m̲od n }
23
24⍝ :: Num a => a -> a
25⍝# Every square's count of 1s among its eight neighbors, all at once:
26⍝# the board in a border of zeros, rotated along both axes by -1, 0 and
27⍝# 1 (nine boards), summed, less the board, the border dropped.
28⍝# >> "b:" u_se< "Board"
29⍝# >> b:a_round 3 3 r_eshape 1 0 0 0 0 0 0 0 1
30⍝# 0 1 0
31⍝# 1 2 1
32⍝# 0 1 0
33ˡa̲round ← { b →
34 p ← 0 c̲at₂ (0 c̲at b c̲at 0) c̲at₂ 0
35 t ← ('+ r̲/₁₂ -1 0 1 o̲-₁₂ p) − p
36 1 d̲rop₂ -1 d̲rop₂ 1 d̲rop -1 d̲rop t
37}
38⍝ :: Num a => a -> a
39⍝# Every square with its four neighbors (up, down, left, right) as 1s:
40⍝# the plus around each 1 of the board, edges not wrapping.
41⍝# >> "b:" u_se< "Board"
42⍝# >> b:p_lus 3 3 r_eshape 0 0 0 0 1 0 0 0 0
43⍝# 0 1 0
44⍝# 1 1 1
45⍝# 0 1 0
46ˡp̲lus ← { b →
47 p ← 0 c̲at₂ (0 c̲at b c̲at 0) c̲at₂ 0
48 t ← ('+ r̲/ -1 0 1 o̲- p) + ('+ r̲/ -1 1 o̲-₂ p)
49 1 d̲rop₂ -1 d̲rop₂ 1 d̲rop -1 d̲rop t
50}
51
52⍝ :: Char -> Char
53⍝# A character board with a space after every character, as the games
54⍝# print boards: the columns 1, c+1, 2, c+1, ... of the board with a
55⍝# column of spaces added.
56⍝# >> "b:" u_se< "Board"
57⍝# >> b:s_paced 2 3 r_eshape "abcdef"
58⍝# a b c
59⍝# d e f
60ˡs̲paced ← { m →
61 c ← 2 s̲elect s̲hape m
62 spaces ← ((f̲irst s̲hape m) c̲at 1) r̲eshape " "
63 (r̲avel₂ (2 c̲at c) r̲eshape (r̲ange c) c̲at c r̲eshape c + 1) s̲elect₂ m c̲at₂ spaces
64}