tyess/tests/game.typ (view raw)
| 1 | #import "../game.typ": apply-moves, initial-state |
| 2 | #import "../board.typ": is-dark-sq, starting-grid |
| 3 | #import "../notation.typ": parse-move |
| 4 | #import "../rules.typ": has-legal-move, is-in-check |
| 5 | #import "testutil.typ": assert-eq, assert-grid, assert-no-error |
| 6 | |
| 7 | // pawn advances and turn switching |
| 8 | #let st = apply-moves(("e4",)).last() |
| 9 | #assert-eq(st.turn, "black") |
| 10 | #assert-no-error(st, "e4") |
| 11 | |
| 12 | #let st = apply-moves(("e4", "e5")).last() |
| 13 | #assert-eq(st.turn, "white") |
| 14 | #assert-grid(st, 5, 2, ".") // e2 empty |
| 15 | #assert-grid(st, 5, 4, "P") // e4 |
| 16 | #assert-grid(st, 5, 7, ".") // e7 empty |
| 17 | #assert-grid(st, 5, 5, "p") // e5 |
| 18 | |
| 19 | // "how knight moves?" |
| 20 | #let st = apply-moves(("e4", "e5", "Nf3")).last() |
| 21 | #assert-grid(st, 6, 3, "N") // f3 |
| 22 | #assert-grid(st, 7, 1, ".") // g1 empty |
| 23 | |
| 24 | // bishop + pawn + capture |
| 25 | #let st = apply-moves(("e4", "e5", "Nf3", "Nc6", "Bb5", "a6", "Bxc6")).last() |
| 26 | #assert-no-error(st, "Bxc6") |
| 27 | #assert-grid(st, 3, 6, "B") // bishop on c6 |
| 28 | |
| 29 | // took move after clearing a pawn |
| 30 | #let st = apply-moves(("a4", "d6", "Ra3")).last() |
| 31 | #assert-no-error(st, "Ra3") |
| 32 | #assert-grid(st, 1, 3, "R") // a3 |
| 33 | #assert-grid(st, 1, 1, ".") // a1 empty |
| 34 | |
| 35 | // lowercase notation |
| 36 | #let st = apply-moves(("e4", "d5", "nf3")).last() |
| 37 | #assert-no-error(st, "nf3") |
| 38 | #assert-grid(st, 6, 3, "N") |
| 39 | |
| 40 | // castling white, kingside |
| 41 | #let castling-setup = ("e4", "e5", "Nf3", "Nc6", "Bb5", "a6", "Bxc6", "dxc6") |
| 42 | #let st = apply-moves(castling-setup + ("O-O",)).last() |
| 43 | #assert-no-error(st, "O-O") |
| 44 | #assert-grid(st, 5, 1, ".") // e1 empty |
| 45 | #assert-grid(st, 7, 1, "K") // g1 |
| 46 | #assert-grid(st, 6, 1, "R") // f1 rook |
| 47 | #assert-grid(st, 8, 1, ".") // h1 empty |
| 48 | #assert(st.castles.find("K") == none) |
| 49 | #assert(st.castles.find("Q") == none) |
| 50 | |
| 51 | // castling blac, kingside |
| 52 | #let st = apply-moves(castling-setup + ("O-O", "O-O")).last() |
| 53 | #assert-no-error(st, "O-O") |
| 54 | #assert-grid(st, 5, 8, ".") // e8 empty |
| 55 | #assert-grid(st, 7, 8, "k") // g8 |
| 56 | #assert-grid(st, 6, 8, "r") // f8 rook |
| 57 | |
| 58 | // en passant |
| 59 | #let ep = apply-moves(("a4", "b5", "axb6")).last() |
| 60 | #assert-eq(ep.turn, "black") |
| 61 | #assert-grid(ep, 1, 4, ".") // a4 empty |
| 62 | #assert-grid(ep, 2, 6, "P") // b6 |
| 63 | #assert-grid(ep, 2, 5, ".") // b5 captured |
| 64 | |
| 65 | #let ep = apply-moves(("e4", "d5", "e5", "d4", "c4", "dxc3")).last() |
| 66 | #assert-grid(ep, 4, 4, ".") // d4 empty |
| 67 | #assert-grid(ep, 3, 3, "p") // c3 |
| 68 | #assert-grid(ep, 3, 4, ".") // c4 captured |
| 69 | |
| 70 | #let st = apply-moves(("a4", "b5", "a5")).last() |
| 71 | #assert-eq(st.ep, none) |
| 72 | |
| 73 | #let st = apply-moves(("e4", "d5", "e5", "d4", "exd6")).last() |
| 74 | #assert-eq(st.error, "exd6: illegal move") |