added insufficient material condition for draw
This commit is contained in:
+121
-1
@@ -2,7 +2,7 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { applyInput, initTable, type CreateTable, type Input } from "@3miz/game-server-sdk";
|
||||
import { chess, isCheckmate, isKingInCheck, isStalemate, type State } from "../server/game.ts";
|
||||
import { chess, isCheckmate, isKingInCheck, isStalemate, hasInsufficientMaterial, type State } from "../server/game.ts";
|
||||
|
||||
export const squareIndex = (name: string): number => {
|
||||
const f = name.charCodeAt(0) - 97;
|
||||
@@ -244,3 +244,123 @@ test("stalemate: lone king with no flight squares is a draw", () => {
|
||||
assert.equal(isCheckmate(s, "b"), false);
|
||||
assert.equal(isStalemate(s, "b"), true);
|
||||
});
|
||||
|
||||
test("insufficient material: K vs K, KB vs K, KN vs K, KB vs KB, KN vs KN are recognized", () => {
|
||||
const mk = (seat: number, user_id: string, color: "w" | "b"): State["players"][number] =>
|
||||
({ seat, user_id, name: user_id, color, bot: false, level: "basic", conns: [], left: false });
|
||||
const makeState = (pieces: { sq: string; type: "k" | "p" | "r" | "n" | "b" | "q"; color: "w" | "b" }[]): State => {
|
||||
const s: State = {
|
||||
players: [mk(0, "u0", "w"), mk(1, "u1", "b")],
|
||||
board: Array(64).fill(null) as State["board"],
|
||||
turn: 0, phase: "play", winner: null, result_reason: "",
|
||||
turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0,
|
||||
moves_played: 40, captures: 28,
|
||||
rights: { wk: false, wq: false, bk: false, bq: false }, ep: null,
|
||||
};
|
||||
for (const p of pieces) {
|
||||
s.board[squareIndex(p.sq)] = { type: p.type, color: p.color };
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
// 1. King vs King
|
||||
const kVsK = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(kVsK), true);
|
||||
|
||||
// 2. King + Bishop vs King
|
||||
const kbVsK = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "c4", type: "b", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(kbVsK), true);
|
||||
|
||||
// 3. King + Knight vs King
|
||||
const knVsK = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
{ sq: "f6", type: "n", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(knVsK), true);
|
||||
|
||||
// 4. King + Bishop vs King + Bishop
|
||||
const kbVsKb = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "c1", type: "b", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
{ sq: "c8", type: "b", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(kbVsKb), true);
|
||||
|
||||
// 5. King + Knight vs King + Knight
|
||||
const knVsKn = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "b1", type: "n", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
{ sq: "b8", type: "n", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(knVsKn), true);
|
||||
|
||||
// 6. Pawn exists -> NOT insufficient (can promote)
|
||||
const withPawn = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "a2", type: "p", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(withPawn), false);
|
||||
|
||||
// 7. Queen or Rook exists -> NOT insufficient
|
||||
const withRook = makeState([
|
||||
{ sq: "e1", type: "k", color: "w" },
|
||||
{ sq: "a1", type: "r", color: "w" },
|
||||
{ sq: "e8", type: "k", color: "b" },
|
||||
]);
|
||||
assert.equal(hasInsufficientMaterial(withRook), false);
|
||||
});
|
||||
|
||||
test("insufficient material: capturing last pawn triggers draw immediately", () => {
|
||||
const mk = (seat: number, user_id: string, color: "w" | "b"): State["players"][number] =>
|
||||
({ seat, user_id, name: user_id, color, bot: false, level: "basic", conns: [], left: false });
|
||||
const s0: State = {
|
||||
players: [mk(0, "u0", "w"), mk(1, "u1", "b")],
|
||||
board: Array(64).fill(null) as State["board"],
|
||||
turn: 0, phase: "play", winner: null, result_reason: "",
|
||||
turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0,
|
||||
moves_played: 50, captures: 29,
|
||||
rights: { wk: false, wq: false, bk: false, bq: false }, ep: null,
|
||||
};
|
||||
s0.board[squareIndex("d4")] = { type: "k", color: "w" };
|
||||
s0.board[squareIndex("d5")] = { type: "p", color: "b" };
|
||||
s0.board[squareIndex("f8")] = { type: "k", color: "b" };
|
||||
|
||||
const w = {
|
||||
create: create(false),
|
||||
game: s0,
|
||||
status: "running",
|
||||
seq: 1,
|
||||
vseq: {},
|
||||
sseq: 1,
|
||||
timers: {},
|
||||
last_cid: {},
|
||||
snap_hash: {},
|
||||
sys_hash: "",
|
||||
result: null,
|
||||
rng: [1, 2, 3, 4],
|
||||
last_ts_ms: 1700000000000,
|
||||
};
|
||||
|
||||
// White king captures the black pawn on d5 -> only Kings left!
|
||||
const res = applyInput(chess, w as any, {
|
||||
v: 1, type: "move", payload: { from: "d4", to: "d5" },
|
||||
uid: "u0", ci: "c0", cid: 1, role: "player", seat: 0, ts_ms: 1700000001000,
|
||||
} as Input, 1);
|
||||
|
||||
assert.equal(res.status, "accepted");
|
||||
assert.equal((w.game as State).phase, "over");
|
||||
assert.equal((w.game as State).winner, null);
|
||||
assert.equal((w.game as State).result_reason, "insufficient_material");
|
||||
assert.equal(w.status, "finished");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user