all repos

tyess @ 87a9bef

chess in typst

tyess/tests/move-gen.typ (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
only export chess as part of public api; merge moves.typ and rules.typ, 27 days ago
1
#import "../rules.typ": generate-moves-for
2
#import "../board.typ": starting-grid
3
#import "testutil.typ": assert-eq
4
5
// king,  no moves in starting position
6
#assert-eq(generate-moves-for(starting-grid, 5, 8, "white").len(), 0)
7
8
// pawn e2 → e3, e4
9
#let pmoves = generate-moves-for(starting-grid, 5, 7, "white")
10
#assert(pmoves.len() >= 2)
11
#assert-eq(pmoves.at(0), (5, 6))
12
#assert-eq(pmoves.at(1), (5, 5))
13
14
// knight b1 → a3, c3
15
#let km = generate-moves-for(starting-grid, 2, 8, "white")
16
#assert-eq(km.len(), 2)
17
#assert-eq(km.at(0), (1, 6))
18
#assert-eq(km.at(1), (3, 6))
19
20
// en passant: white pawn on a4 with ep = b6
21
#let ep-grid = (
22
  (".", ".", ".", ".", ".", ".", ".", "."),
23
  (".", ".", ".", ".", ".", ".", ".", "."),
24
  (".", ".", ".", ".", ".", ".", ".", "."),
25
  (".", "p", ".", ".", ".", ".", ".", "."),
26
  ("P", ".", ".", ".", ".", ".", ".", "."),
27
  (".", ".", ".", ".", ".", ".", ".", "."),
28
  (".", ".", ".", ".", ".", ".", ".", "."),
29
  (".", ".", ".", ".", ".", ".", ".", "."),
30
)
31
#let epm = generate-moves-for(ep-grid, 1, 5, "white", ep: (2, 3))
32
#let found = false
33
#for m in epm { if m.at(0) == 2 and m.at(1) == 3 { found = true; break } }
34
#assert(found)