all repos

tyess @ 3f3048b6eabb39e39333e63cf4d67ba48587a099

chess in typst

tyess/tests/check.typ (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
what's creamy, 27 days ago
1
#import "testutil.typ": assert-eq
2
#import "../game.typ": apply-moves, initial-state
3
#import "../rules.typ": has-legal-move, is-in-check
4
5
// no one is in check at the start
6
#assert(not is-in-check(initial-state().grid, "white"))
7
#assert(not is-in-check(initial-state().grid, "black"))
8
9
// queen gives check without bishop cover: Qxf7+ after e4 e5 Qh5 Nc6
10
// and black king can escape by capturing the queen (king on e8, queen on f7)
11
#let st = apply-moves(("e4", "e5", "Qh5", "Nc6", "Qxf7+")).last()
12
#assert(is-in-check(st.grid, st.turn))
13
#assert(has-legal-move(st.grid, st.turn))
14
#assert(not st.checkmate)
15
16
// scholar's mate
17
#let st = apply-moves(("e4", "e5", "Qh5", "a6", "Bc4", "a5", "Qxf7")).last()
18
#assert(is-in-check(st.grid, st.turn))
19
#assert(not has-legal-move(st.grid, st.turn))
20
#assert(st.checkmate)
21
22
// stalemate
23
#let stale = (
24
  ..initial-state(),
25
  grid: (
26
    ("k", ".", ".", ".", ".", ".", ".", "."),
27
    (".", ".", ".", ".", ".", ".", ".", "."),
28
    (".", "Q", ".", ".", ".", ".", ".", "."),
29
    (".", ".", ".", ".", ".", ".", ".", "."),
30
    (".", ".", ".", ".", ".", ".", ".", "."),
31
    (".", ".", ".", ".", ".", ".", ".", "."),
32
    (".", ".", ".", ".", ".", ".", ".", "."),
33
    ("K", ".", ".", ".", ".", ".", ".", "."),
34
  ),
35
  turn: "black",
36
  castles: "",
37
  ep: none,
38
)
39
#assert(not is-in-check(stale.grid, stale.turn))
40
#assert(not has-legal-move(stale.grid, stale.turn))