|
1
|
#import "board.typ": apply-move, at, copy-grid, file-to-col, find-pieces, is-black, is-white, on-board, piece-type, render-board, square-to-coords, starting-grid |
|
2
|
#import "notation.typ": parse-move |
|
3
|
#import "rules.typ": generate-moves-for, has-legal-move, is-attacked, is-in-check, pawn-moves |
|
4
|
|
|
5
|
#let initial-state() = ( |
|
6
|
grid: starting-grid, |
|
7
|
turn: "white", |
|
8
|
castles: "KQkq", |
|
9
|
ep: none, |
|
10
|
error: none, |
|
11
|
checkmate: false, |
|
12
|
stalemate: false, |
|
13
|
) |
|
14
|
|
|
15
|
#let apply-castling(state, side) = { |
|
16
|
let castles = state.castles |
|
17
|
let grid = copy-grid(state.grid) |
|
18
|
|
|
19
|
let row = if state.turn == "white" { 7 } else { 0 } |
|
20
|
let case-p = if state.turn == "white" { upper } else { lower } |
|
21
|
let (rook-from, rook-to, king-to) = if side == "kingside" { (7, 5, 6) } else { (0, 3, 2) } |
|
22
|
grid.at(row).at(4) = "." |
|
23
|
grid.at(row).at(rook-from) = "." |
|
24
|
grid.at(row).at(king-to) = case-p("K") |
|
25
|
grid.at(row).at(rook-to) = case-p("R") |
|
26
|
|
|
27
|
let lost = if state.turn == "white" { "KQ" } else { "kq" } |
|
28
|
for c in lost { castles = castles.replace(c, "") } |
|
29
|
|
|
30
|
( |
|
31
|
grid: grid, |
|
32
|
turn: if state.turn == "white" { "black" } else { "white" }, |
|
33
|
castles: castles, |
|
34
|
ep: none, |
|
35
|
error: none, |
|
36
|
checkmate: false, |
|
37
|
stalemate: false, |
|
38
|
) |
|
39
|
} |
|
40
|
|
|
41
|
#let apply-parsed-move(state, parsed) = { |
|
42
|
if parsed.kind == "error" { return (..state, error: parsed.raw + ": illegal notation") } |
|
43
|
if parsed.kind == "castle" { return apply-castling(state, parsed.side) } |
|
44
|
|
|
45
|
let dest-col = parsed.dest.at(0) |
|
46
|
let dest-row = parsed.dest.at(1) |
|
47
|
|
|
48
|
// find the source pieces that can reach the destination |
|
49
|
let candidates = () |
|
50
|
if parsed.piece == "P" { |
|
51
|
let pawn-file = none |
|
52
|
if parsed.disambig != none and parsed.disambig.type == "file" { |
|
53
|
pawn-file = file-to-col.at(parsed.disambig.value) + 1 |
|
54
|
} |
|
55
|
|
|
56
|
for (col, row) in find-pieces(state.grid, state.turn, "P") { |
|
57
|
if pawn-file != none and col != pawn-file { continue } |
|
58
|
for m in pawn-moves(state.grid, col, row, state.turn, ep: state.ep) { |
|
59
|
if m.at(0) == dest-col and m.at(1) == dest-row { |
|
60
|
candidates.push((col, row)) |
|
61
|
break |
|
62
|
} |
|
63
|
} |
|
64
|
} |
|
65
|
} else { |
|
66
|
for (col, row) in find-pieces(state.grid, state.turn, parsed.piece) { |
|
67
|
let moves = generate-moves-for(state.grid, col, row, state.turn, ep: state.ep) |
|
68
|
for m in moves { |
|
69
|
if m.at(0) == dest-col and m.at(1) == dest-row { |
|
70
|
candidates.push((col, row)) |
|
71
|
} |
|
72
|
} |
|
73
|
} |
|
74
|
|
|
75
|
// apply disambiguation |
|
76
|
if candidates.len() > 1 and parsed.disambig != none { |
|
77
|
if parsed.disambig.type == "file" { |
|
78
|
let f = file-to-col.at(parsed.disambig.value) + 1 |
|
79
|
candidates = candidates.filter(c => c.at(0) == f) |
|
80
|
|
|
81
|
} else if parsed.disambig.type == "rank" { |
|
82
|
let r = 9 - int(parsed.disambig.value) |
|
83
|
candidates = candidates.filter(c => c.at(1) == r) |
|
84
|
|
|
85
|
} else if parsed.disambig.type == "square" { |
|
86
|
let (cf, rf) = square-to-coords(parsed.disambig.value) |
|
87
|
candidates = candidates.filter(c => c.at(0) == cf and c.at(1) == rf) |
|
88
|
} |
|
89
|
} |
|
90
|
} |
|
91
|
|
|
92
|
// auto-promote to queen |
|
93
|
let promotion = parsed.promotion |
|
94
|
if promotion == none and parsed.piece == "P" and (dest-row == 1 or dest-row == 8) { promotion = "Q" } |
|
95
|
if promotion != none and "QRBN".position(upper(promotion)) == none { return (..state, error: parsed.raw + "=" + promotion + ": illegal notation") } |
|
96
|
|
|
97
|
// check if it's an en passant capture |
|
98
|
let is-ep = parsed.piece == "P" and state.ep != none and dest-col == state.ep.at(0) and dest-row == state.ep.at(1) |
|
99
|
let ep-captured = if is-ep { (dest-col, if state.turn == "white" { dest-row + 1 } else { dest-row - 1 }) } else { none } |
|
100
|
|
|
101
|
// filter to only legal moves |
|
102
|
let legal-candidates = () |
|
103
|
for (sc, sr) in candidates { |
|
104
|
let sim = apply-move(state.grid, sc, sr, dest-col, dest-row, promotion: promotion, en-passant: ep-captured) |
|
105
|
if not is-in-check(sim, state.turn) { legal-candidates.push((sc, sr)) } |
|
106
|
} |
|
107
|
candidates = legal-candidates |
|
108
|
|
|
109
|
if candidates.len() == 0 { return (..state, error: parsed.raw + ": illegal move") } |
|
110
|
let (src-col, src-row) = candidates.at(0) |
|
111
|
|
|
112
|
let new-grid = apply-move(state.grid, src-col, src-row, dest-col, dest-row, promotion: promotion, en-passant: ep-captured) |
|
113
|
let new-turn = if state.turn == "white" { "black" } else { "white" } |
|
114
|
|
|
115
|
// update castling rights |
|
116
|
let castles = state.castles |
|
117
|
let lost = if parsed.piece == "K" { if state.turn == "white" { "KQ" } else { "kq" } } else { |
|
118
|
let flags = "" |
|
119
|
for (rc, rr, flag) in ((1, 8, "Q"), (8, 8, "K"), (1, 1, "q"), (8, 1, "k")) { |
|
120
|
if src-col == rc and src-row == rr { flags += flag } |
|
121
|
if dest-col == rc and dest-row == rr { flags += flag } |
|
122
|
} |
|
123
|
flags |
|
124
|
} |
|
125
|
for c in lost { castles = castles.replace(c, "") } |
|
126
|
|
|
127
|
// set en passant target for the next move |
|
128
|
let new-ep = none |
|
129
|
if parsed.piece == "P" and calc.abs(dest-row - src-row) == 2 { new-ep = (dest-col, int((src-row + dest-row) / 2)) } |
|
130
|
|
|
131
|
// check for checkmate or stalemate |
|
132
|
let in-check = is-in-check(new-grid, new-turn) |
|
133
|
let no-legal = not has-legal-move(new-grid, new-turn, ep: new-ep, castles: castles) |
|
134
|
|
|
135
|
( |
|
136
|
grid: new-grid, |
|
137
|
turn: new-turn, |
|
138
|
castles: castles, |
|
139
|
ep: new-ep, |
|
140
|
error: none, |
|
141
|
checkmate: in-check and no-legal, |
|
142
|
stalemate: not in-check and no-legal, |
|
143
|
) |
|
144
|
} |
|
145
|
|
|
146
|
#let apply-moves(moves-strs) = { |
|
147
|
let state = initial-state() |
|
148
|
let states = (state,) |
|
149
|
for ms in moves-strs { |
|
150
|
if state.at("checkmate", default: false) or state.at("stalemate", default: false) { break } |
|
151
|
let new-state = apply-parsed-move(state, parse-move(ms)) |
|
152
|
states += (new-state,) |
|
153
|
state = new-state |
|
154
|
} |
|
155
|
states |
|
156
|
} |
|
157
|
|
|
158
|
#let parse-moves(body) = { |
|
159
|
let extract(it) = { |
|
160
|
if it == [ ] { " " } else if it.func() == text { it.text } else if it.func() == [].func() { |
|
161
|
it.children.map(extract).join() |
|
162
|
} else { "" } |
|
163
|
} |
|
164
|
extract(body).split().filter(s => s != "") |
|
165
|
} |
|
166
|
|
|
167
|
#let render(state) = { |
|
168
|
let banner(body) = text(fill: rgb("#0a7e8c"), weight: "bold", size: 1.2em, body) |
|
169
|
|
|
170
|
let label = none |
|
171
|
let label-fill = none |
|
172
|
if state.at("checkmate", default: false) { |
|
173
|
label = banner[Checkmate!] |
|
174
|
label-fill = rgb("#0a7e8c").transparentize(75%) |
|
175
|
} else if state.at("stalemate", default: false) { |
|
176
|
label = banner[Stalemate!] |
|
177
|
label-fill = rgb("#0a7e8c").transparentize(75%) |
|
178
|
} else { |
|
179
|
let err = state.at("error", default: none) |
|
180
|
if err != none { |
|
181
|
label = text(fill: red, weight: "bold", err) |
|
182
|
label-fill = red.transparentize(75%) |
|
183
|
} else if is-in-check(state.grid, state.turn) { |
|
184
|
label = text(fill: yellow, weight: "bold")[Check!] |
|
185
|
label-fill = yellow.transparentize(75%) |
|
186
|
} |
|
187
|
} |
|
188
|
|
|
189
|
block(height: 1.8em, width: 100%, inset: 0pt, above: 0pt, below: 0pt, |
|
190
|
align(center, { |
|
191
|
if label != none { box(fill: label-fill, inset: 0.3em, label) } |
|
192
|
})) |
|
193
|
v(0.15em) |
|
194
|
render-board(state.grid) |
|
195
|
} |
|
196
|
|
|
197
|
// Usage: |
|
198
|
// #show: chess.with() |
|
199
|
// g4 d5 Bg5 Bxg4 c4 c6 cxd5 cxd5 Qb3 |
|
200
|
// |
|
201
|
#let chess(body) = { |
|
202
|
// Fixed page size: board (8 × 2.8em) + label area (1.8em) + spacing (0.15em) + margins |
|
203
|
set page( |
|
204
|
width: 22.4em + 1in, |
|
205
|
height: 24.65em + 1in, |
|
206
|
margin: 0.5in, |
|
207
|
) |
|
208
|
let moves = parse-moves(body) |
|
209
|
let states = apply-moves(moves) |
|
210
|
render(states.last()) |
|
211
|
} |