librarylib/Board.xtl

Board: square boards as arrays, shared by the games. Rows and columns count from 0; a square's number counts from 1, row by row.

source

ˡc̲ell : Num a => a -> a -> a

function · line 12

The number of square rc (row, column) on a board n wide; rc may hold many squares, as a 2-row matrix (rows, then columns).

      ᵇ⁼u̲se< "Board"
      3 ᵇc̲ell 1 2
6
      3 ᵇc̲ell 2 2 r̲eshape 0 2 1 0
2 7
ˡc̲ell ← { n rc → 1 + (n × 1 s̲elect rc) + 2 s̲elect rc }

ˡr̲c : Int -> Int -> Int

function · line 22

The row and column of squares k on a board n wide (a 2-row matrix for many squares).

      ᵇ⁼u̲se< "Board"
      3 ᵇr̲c 6
1 2
      3 ᵇr̲c 1 9
0 2
0 2
ˡr̲c ← { n k → (2 c̲at s̲hape k) r̲eshape ((k − 1) d̲iv n) c̲at (k − 1) m̲od n }

ˡa̲round : Num a => a -> a

function · line 33

Every square's count of 1s among its eight neighbors, all at once: the board in a border of zeros, rotated along both axes by -1, 0 and 1 (nine boards), summed, less the board, the border dropped.

      ᵇ⁼u̲se< "Board"
      ᵇa̲round 3 3 r̲eshape 1 0 0 0 0 0 0 0 1
0 1 0
1 2 1
0 1 0
ˡa̲round ← { b →
  p ← 0 c̲at₂ (0 c̲at b c̲at 0) c̲at₂ 0
  t ← ('+ r̲/₁₂ -1 0 1 o̲-₁₂ p) − p
  1 d̲rop₂ -1 d̲rop₂ 1 d̲rop -1 d̲rop t
}
Used in: ˡa̲round

ˡp̲lus : Num a => a -> a

function · line 46

Every square with its four neighbors (up, down, left, right) as 1s: the plus around each 1 of the board, edges not wrapping.

      ᵇ⁼u̲se< "Board"
      ᵇp̲lus 3 3 r̲eshape 0 0 0 0 1 0 0 0 0
0 1 0
1 1 1
0 1 0
ˡp̲lus ← { b →
  p ← 0 c̲at₂ (0 c̲at b c̲at 0) c̲at₂ 0
  t ← ('+ r̲/ -1 0 1 o̲- p) + ('+ r̲/ -1 1 o̲-₂ p)
  1 d̲rop₂ -1 d̲rop₂ 1 d̲rop -1 d̲rop t
}

ˡs̲paced : Char -> Char

function · line 60

A character board with a space after every character, as the games print boards: the columns 1, c+1, 2, c+1, ... of the board with a column of spaces added.

      ᵇ⁼u̲se< "Board"
      ᵇs̲paced 2 3 r̲eshape "abcdef"
a b c
d e f
ˡs̲paced ← { m →
  c ← 2 s̲elect s̲hape m
  spaces ← ((f̲irst s̲hape m) c̲at 1) r̲eshape " "
  (r̲avel₂ (2 c̲at c) r̲eshape (r̲ange c) c̲at c r̲eshape c + 1) s̲elect₂ m c̲at₂ spaces
}