Files
arcade/tests/chess_engine_behavior.zi

336 lines
16 KiB
Ziran

// Chess rules and search: perft counts of the standard test positions,
// notation, the draw rules and the computer's choices.
#import, file "../apps/chess/engine.zi";
KIWIPETE :: "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
ENDGAME :: "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
MIRRORED :: "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1"
PROMOTIONS :: "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"
MIDDLE :: "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10"
PerftIs :: (fen: string, depth: s32, expected: u64) -> bool {
if !ChessLoadFen(fen) {
return false
}
return ChessPerft(depth) == expected
}
PerftChecks :: () -> s32 {
if !PerftIs(CHESS_START_FEN, 1, 20) { return 1 }
if !PerftIs(CHESS_START_FEN, 2, 400) { return 2 }
if !PerftIs(CHESS_START_FEN, 3, 8902) { return 3 }
if !PerftIs(CHESS_START_FEN, 4, 197281) { return 4 }
if !PerftIs(CHESS_START_FEN, 5, 4865609) { return 5 }
if !PerftIs(KIWIPETE, 1, 48) { return 6 }
if !PerftIs(KIWIPETE, 2, 2039) { return 7 }
if !PerftIs(KIWIPETE, 3, 97862) { return 8 }
if !PerftIs(KIWIPETE, 4, 4085603) { return 9 }
if !PerftIs(ENDGAME, 1, 14) { return 10 }
if !PerftIs(ENDGAME, 3, 2812) { return 11 }
if !PerftIs(ENDGAME, 5, 674624) { return 12 }
if !PerftIs(MIRRORED, 1, 6) { return 13 }
if !PerftIs(MIRRORED, 3, 9467) { return 14 }
if !PerftIs(MIRRORED, 4, 422333) { return 15 }
if !PerftIs(PROMOTIONS, 1, 44) { return 16 }
if !PerftIs(PROMOTIONS, 3, 62379) { return 17 }
if !PerftIs(PROMOTIONS, 4, 2103487) { return 18 }
if !PerftIs(MIDDLE, 1, 46) { return 19 }
if !PerftIs(MIDDLE, 3, 89890) { return 20 }
return 0
}
// Plays coordinate moves from the start; false at the first illegal one.
Play :: (line: []string) -> bool {
ChessReset()
for text: line {
move := ChessMoveFromText(text)
if move == 0 {
return false
}
ChessMakeMove(move)
}
return true
}
NotationChecks :: () -> s32 {
ChessReset()
if ChessFen() != CHESS_START_FEN { return 30 }
if !ChessLoadFen(KIWIPETE) || ChessFen() != KIWIPETE { return 31 }
if ChessLoadFen("8/8/8/8/8/8/8/8 w - - 0 1") { return 32 }
if ChessLoadFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR x KQkq - 0 1") { return 33 }
// The side not to move may not stand in check.
if ChessLoadFen("4k3/8/8/8/8/8/8/4K2r w - - 0 1") == false { return 34 }
if ChessLoadFen("4k3/8/8/8/8/8/8/4K2R w - - 0 1") == false { return 35 }
if ChessLoadFen("4k2R/8/8/8/8/8/8/4K3 w - - 0 1") { return 36 }
ChessReset()
knight := ChessMoveFromText("g1f3")
if knight == 0 || ChessMoveSan(knight) != "Nf3" { return 37 }
if ChessMoveFromText("e2e5") != 0 { return 38 }
if ChessMoveText(ChessMoveFromText("e2e4")) != "e2e4" { return 39 }
italian: [5]string = .["e2e4", "e7e5", "g1f3", "b8c6", "f1c4"]
if !Play(italian[:]) { return 40 }
if ChessFen() != "r1bqkbnr/pppp1ppp/2n5/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3" { return 41 }
// Castling, its notation and the rights it takes away.
castle: [6]string = .["e2e4", "e7e5", "g1f3", "b8c6", "f1c4", "g8f6"]
if !Play(castle[:]) { return 42 }
short := ChessMoveFromText("e1g1")
if short == 0 || ChessMoveSan(short) != "O-O" { return 43 }
ChessMakeMove(short)
if ChessFen() != "r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQ1RK1 b kq - 5 4" { return 44 }
// En passant: offered only right after the double step.
passant: [5]string = .["e2e4", "a7a6", "e4e5", "d7d5", "e5d6"]
if !Play(passant[:]) { return 45 }
if ChessPieceAt(ChessSquare(3, 4)) != CHESS_EMPTY { return 46 }
late: [7]string = .["e2e4", "a7a6", "e4e5", "d7d5", "h2h3", "h7h6", "e5d6"]
if Play(late[:]) { return 47 }
// Promotion needs its kind and names it.
if !ChessLoadFen("8/4P1k1/8/8/8/8/8/4K3 w - - 0 1") { return 48 }
if ChessMoveFromText("e7e8") != 0 { return 49 }
queen := ChessMoveFromText("e7e8q")
if queen == 0 || ChessMoveSan(queen) != "e8=Q" { return 50 }
if ChessMoveSan(ChessMoveFromText("e7e8n")) != "e8=N+" { return 51 }
// Two knights reaching one square are told apart by file.
if !ChessLoadFen("4k3/8/8/8/8/8/8/1N2KN2 w - - 0 1") { return 52 }
if ChessMoveSan(ChessMoveFromText("b1d2")) != "Nbd2" { return 53 }
// Check and mate signs.
fools: [3]string = .["f2f3", "e7e5", "g2g4"]
if !Play(fools[:]) { return 54 }
if ChessMoveSan(ChessMoveFromText("d8h4")) != "Qh4#" { return 55 }
return 0
}
StatusChecks :: () -> s32 {
fools: [4]string = .["f2f3", "e7e5", "g2g4", "d8h4"]
if !Play(fools[:]) || ChessStatus() != CHESS_CHECKMATE { return 60 }
if !ChessLoadFen("7k/5Q2/6K1/8/8/8/8/8 b - - 0 1") || ChessStatus() != CHESS_STALEMATE { return 61 }
if !ChessLoadFen("8/8/4k3/8/8/3NK3/8/8 w - - 0 1") || ChessStatus() != CHESS_DRAW_MATERIAL { return 62 }
if !ChessLoadFen("8/8/4k3/8/8/3BK3/8/8 w - - 0 1") || ChessStatus() != CHESS_DRAW_MATERIAL { return 63 }
// Bishops on one color cannot mate; on both colors they can.
if !ChessLoadFen("8/8/2b1k3/8/8/3BK3/8/8 w - - 0 1") || ChessStatus() != CHESS_DRAW_MATERIAL { return 64 }
if !ChessLoadFen("8/8/3bk3/8/8/3BK3/8/8 w - - 0 1") || ChessStatus() != CHESS_PLAYING { return 65 }
if !ChessLoadFen("8/8/4k3/8/8/2NNK3/8/8 w - - 0 1") || ChessStatus() != CHESS_PLAYING { return 66 }
if !ChessLoadFen("8/8/4k3/8/8/4K3/4P3/8 w - - 0 1") || ChessStatus() != CHESS_PLAYING { return 67 }
if !ChessLoadFen("8/8/4k3/8/8/4K3/4R3/8 w - - 99 80") || ChessStatus() != CHESS_PLAYING { return 68 }
if !ChessLoadFen("8/8/4k3/8/8/4K3/4R3/8 w - - 100 80") || ChessStatus() != CHESS_DRAW_FIFTY { return 69 }
// Mate on the fiftieth move is still mate.
if !ChessLoadFen("k7/8/1K6/8/8/8/8/7R w - - 99 80") { return 70 }
ChessMakeMove(ChessMoveFromText("h1h8"))
if ChessStatus() != CHESS_CHECKMATE { return 71 }
// The knights go out and back twice: the start position stands a third time.
dance: [8]string = .["g1f3", "g8f6", "f3g1", "f6g8", "g1f3", "g8f6", "f3g1", "f6g8"]
if !Play(dance[:]) || ChessStatus() != CHESS_DRAW_REPETITION { return 72 }
if !Play(dance[:6]) || ChessStatus() != CHESS_PLAYING { return 73 }
return 0
}
// Runs a whole search in small steps, as the app does between frames.
Think :: (depth: s32, limit: s32, quiescence: bool, noise: s32) -> s32 {
before := ChessFen()
ChessSearchBegin(depth, limit, quiescence, noise, 7)
steps: s32 = 0
while !ChessSearchRun(50) {
steps += 1
if steps > 100000 {
return -1
}
}
if ChessFen() != before {
return -2
}
if ChessSearchDepth() < 1 || ChessSearchDepth() > depth { return -3 }
return ChessSearchMove()
}
SearchChecks :: () -> s32 {
ChessForget()
// Mate in one.
if !ChessLoadFen("6k1/5ppp/8/8/8/8/8/R5K1 w - - 0 1") { return 80 }
if ChessMoveText(Think(3, 200000, true, 0)) != "a1a8" { return 81 }
// Win the hanging queen.
if !ChessLoadFen("4k3/8/8/3q4/8/8/8/3RK3 w - - 0 1") { return 82 }
if ChessMoveText(Think(2, 200000, true, 0)) != "d1d5" { return 83 }
// Defend against mate in one: Black must stop Qxf7 / Qh7.
if !ChessLoadFen("r1bqkb1r/pppp1ppp/2n2n2/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 4 4") { return 84 }
defence := ChessMoveText(Think(3, 200000, true, 0))
if defence != "d8e7" && defence != "h5f7" && defence != "f6h5" && defence != "d8f6" && defence != "g7g6" {
return 85
}
// Mate in two with a rook and king.
if !ChessLoadFen("k7/8/2K5/8/8/8/8/1R6 w - - 0 1") { return 86 }
two := Think(4, 400000, true, 0)
ChessMakeMove(two)
reply := Think(1, 1000, true, 0)
if reply == 0 {
return 87
}
ChessMakeMove(reply)
finish := Think(2, 100000, true, 0)
ChessMakeMove(finish)
if ChessStatus() != CHESS_CHECKMATE { return 88 }
// A position without moves has no answer, and the search says so.
if !ChessLoadFen("7k/5Q2/6K1/8/8/8/8/8 b - - 0 1") { return 89 }
if Think(3, 1000, true, 0) != 0 { return 90 }
// Every level answers the opening with a legal move, cancel restores.
ChessReset()
for level: cast(s32)1..cast(s32)4 {
move := Think(level, 20000, level > 1, 150 / level)
if move <= 0 || ChessFindMove(ChessMoveFrom(move), ChessMoveTo(move), ChessMovePromotion(move)) != move {
return 91
}
}
ChessSearchBegin(6, 1000000, true, 0, 1)
unused ChessSearchRun(500)
ChessSearchCancel()
if ChessFen() != CHESS_START_FEN { return 92 }
return 0
}
// Replaying coordinates moves only for legal moves, and the kept hash and
// value always match the board, through moves, takebacks and searches.
ReplayChecks :: () -> s32 {
ChessReset()
if ChessPlayText("e2e4") == 0 || ChessPlayText("e7e5") == 0 { return 100 }
before := ChessFen()
if ChessPlayText("e1e3") != 0 || ChessPlayText("d8h4x") != 0 || ChessPlayText("e7e5") != 0 { return 101 }
if ChessFen() != before || !ChessConsistent() { return 102 }
// A check must be answered: after Bb4+ the bishop on g5 may not leave.
pinned: [3]string = .["d2d4", "e7e5", "c1g5"]
if !Play(pinned[:]) { return 103 }
if ChessPlayText("f8b4") == 0 || ChessPlayText("g5h4") != 0 || ChessPlayText("c2c3") == 0 { return 104 }
if ChessPlayText("b4c3") == 0 || ChessPlayText("b1c3") == 0 { return 105 }
if !ChessConsistent() { return 106 }
if !ChessLoadFen(KIWIPETE) { return 107 }
count := ChessGenerateLegal()
index: s32 = 0
while index < count {
ChessMakeMove(ChessLegalAt(index))
if !ChessConsistent() { return 108 }
ChessUnmakeMove()
index += 1
}
if !ChessConsistent() || ChessFen() != KIWIPETE { return 109 }
ChessSearchBegin(4, 20000, true, 0, 3)
while !ChessSearchRun(37) {
}
if !ChessConsistent() || ChessFen() != KIWIPETE { return 110 }
return 0
}
#import, file "../apps/chess/notation.zi";
#import, file "../apps/chess/import.zi";
ImportAll :: (job: *ChessImport, line: *ChessNotation, text: string, fen: bool) -> bool {
ImportBegin(job, line, text, fen)
steps: s32 = 0
while !job.done && job.error == 0 {
ImportStep(job, line)
steps += 1
if steps > 10000 { return false }
}
return job.done && job.error == 0
}
ImportChecks :: () -> s32 {
line: ChessNotation
job: ChessImport
pgn := "[Event \"A \\\"study\\\"\"] [White \"Alice\"] [Black \"Bob\"] [Result \"*\"]\n1.e4 {hello (not a variation)} e5 $1 (1...c5 (2.Nf3 d6)) 2.Nf3 Nc6 ; comment\n3.Bb5 a6 4.Ba4 Nf6 5.0-0 *"
if !ImportAll(*job, *line, pgn, false) || line.count != 9 || line.san[8] != "O-O" { return 140 }
if line.white != "Alice" || line.black != "Bob" || line.event != "A \"study\"" { return 141 }
finish := ChessFen()
exported := NotationPgn(*line, CHESS_WHITE)
second: ChessNotation
if !ImportAll(*job, *second, exported, false) || ChessFen() != finish || !ChessConsistent() { return 142 }
if !ImportAll(*job, *line, "f3 e5 g4 Qh4# 0-1", false) || line.result != "0-1" { return 143 }
if ImportAll(*job, *line, "1.e4 e5 2.Bh6 *", false) || job.error != CHESS_IMPORT_MOVE || job.bad_move != "Bh6" { return 144 }
if ImportAll(*job, *line, "1.e4 {unclosed", false) || job.error != CHESS_IMPORT_INVALID { return 145 }
if ImportAll(*job, *line, "1.e4 (1.d4 d5", false) || job.error != CHESS_IMPORT_INVALID { return 146 }
if ImportAll(*job, *line, "[Result \"1-0\"] 1.e4 0-1", false) { return 147 }
if ImportAll(*job, *line, "[Variant \"Chess960\"] 1.e4 *", false) || job.error != CHESS_IMPORT_VARIANT { return 148 }
if ImportAll(*job, *line, "[SetUp \"1\"] *", false) { return 149 }
if ImportAll(*job, *line, "1.e4 * 1...e5", false) { return 150 }
if ImportAll(*job, *line, "1.e4 * [Event \"another\"]", false) { return 151 }
custom := "7k/8/8/8/8/8/p7/7K b - - 0 17"
if !ImportAll(*job, *line, custom, true) || line.first_side != CHESS_BLACK || line.first_number != 17 { return 152 }
if ChessFen() != custom || line.count != 0 || !ChessConsistent() { return 153 }
if !NotationPlay(*line, ChessFindMove(16, 0, CHESS_QUEEN)) || NotationMoveLabel(*line, 0) != "17... a1=Q+" { return 154 }
after_promotion := ChessFen()
NotationSeek(*line, 0)
if ChessFen() != custom { return 155 }
NotationSeek(*line, 1)
if ChessFen() != after_promotion { return 156 }
if !ImportAll(*job, *second, NotationPgn(*line, CHESS_WHITE), false) || second.first_number != 17 ||
second.san[0] != "a1=Q+" || ChessFen() != after_promotion { return 157 }
// Six fields, exact side/rights/numbers, and a possible en-passant
// double step are required rather than silently repaired.
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 w - -", true) { return 158 }
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 white - - 0 1", true) { return 159 }
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 w K - 0 1", true) { return 160 }
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 w - - 0x 1", true) { return 161 }
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 w - - 0 0", true) { return 162 }
if ImportAll(*job, *line, "8/8/8/8/8/4k3/8/4K3 w - e6 0 1", true) { return 163 }
if !ImportAll(*job, *line, "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1", true) { return 164 }
if !ImportAll(*job, *line, "[SetUp \"1\"] [FEN \"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\"] *", false) { return 165 }
// A comment-heavy import yields even when no move is being processed.
text: Vec(u8)
BuilderPrint(*text, "{")
for index: 0..3000 { unused VecPush(text, #char "x") }
BuilderPrint(*text, "} 1.e4 *")
ImportBegin(*job, *line, BuilderFinish(text), false)
ImportStep(*job, *line)
if job.done || job.error != 0 || job.at > 1024 || line.count != 0 { return 166 }
if ImportAll(*job, *line, "{only a comment}", false) { return 167 }
// Prefix truncation and arbitrary bytes must terminate without an out-of-bounds read.
for length: cast(s64)1..cast(s64)120 {
if length < pgn.count { unused ImportAll(*job, *line, pgn[:length], false) }
}
oversized: Vec(u8)
for index: 0..CHESS_IMPORT_BYTES { unused VecPush(oversized, #char "x") }
ImportBegin(*job, *line, BuilderFinish(oversized), false)
if job.error != CHESS_IMPORT_LIMIT || job.text.count != 0 { return 168 }
return 0
}
StudyNotationChecks :: () -> s32 {
line: ChessNotation
if !NotationLoad(*line, "e2e4 e7e5 g1f3 b8c6 f1b5 a7a6 b5a4 g8f6 e1g1", "*") { return 120 }
if line.count != 9 || line.san[0] != "e4" || line.san[2] != "Nf3" || line.san[8] != "O-O" { return 121 }
end := ChessFen()
NotationSeek(*line, 0)
if ChessFen() != CHESS_START_FEN || line.count != 9 { return 122 }
NotationSeek(*line, 9)
if ChessFen() != end || !ChessConsistent() { return 123 }
NotationSeek(*line, 2)
if !NotationPlay(*line, ChessFindMove(1, 34, 0)) || line.count != 3 || line.san[2] != "Nc3" { return 124 }
if NotationCoordinates(*line, 3) != "e2e4 e7e5 b1c3" { return 125 }
before := ChessFen()
if NotationPlay(*line, 0) || ChessFen() != before { return 126 }
text := NotationPgn(*line, CHESS_WHITE)
expected := "[Event \"Chess\"]\n[Site \"Arcade / Inbe\"]\n[Date \"????.??.??\"]\n[Round \"?\"]\n[White \"Player\"]\n[Black \"Computer\"]\n[Result \"*\"]\n\n1. e4 e5 2. Nc3 *"
if text != expected || ChessFen() != before { return 127 }
if !NotationLoad(*line, "f2f3 e7e5 g2g4 d8h4", "0-1") || line.san[3] != "Qh4#" { return 128 }
NotationSeek(*line, 3)
if !NotationPlay(*line, ChessFindMove(115, 55, 0)) || line.result != "0-1" { return 129 }
if NotationLoad(*line, "e2e5", "*") { return 130 }
ChessReset()
expert := Think(7, 50000, true, 0)
if expert == 0 || ChessFindMove(ChessMoveFrom(expert), ChessMoveTo(expert), ChessMovePromotion(expert)) != expert { return 131 }
return 0
}
#program_export
Check :: () -> s32 {
imported := ImportChecks()
if imported != 0 { return imported }
notation := StudyNotationChecks()
if notation != 0 { return notation }
result_replay := ReplayChecks()
if result_replay != 0 { return result_replay }
result := NotationChecks()
if result != 0 { return result }
result = StatusChecks()
if result != 0 { return result }
result = SearchChecks()
if result != 0 { return result }
return PerftChecks()
}