added insufficient material condition for draw
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "chess-client",
|
"name": "chess-client",
|
||||||
"version": "1.3.0",
|
"version": "1.4.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
+11
-3
@@ -306,7 +306,7 @@ function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isAc
|
|||||||
const botBadge = player.bot ? `<span class="bot-tag">BOT</span>` : "";
|
const botBadge = player.bot ? `<span class="bot-tag">BOT</span>` : "";
|
||||||
const youBadge = isMe ? `<span class="bot-tag you-tag">${L("شما", "YOU")}</span>` : "";
|
const youBadge = isMe ? `<span class="bot-tag you-tag">${L("شما", "YOU")}</span>` : "";
|
||||||
const statusLabel = S.phase === "over"
|
const statusLabel = S.phase === "over"
|
||||||
? (S.winner === player.seat ? L("برنده 🏆", "Winner 🏆") : "")
|
? (S.winner === player.seat ? L("برنده 🏆", "Winner 🏆") : (S.winner === null ? L("تساوی 🤝", "Draw 🤝") : ""))
|
||||||
: (isActive ? (isMe ? L("نوبت شماست", "Your turn") : L("در حال تفکر…", "Thinking…")) : "");
|
: (isActive ? (isMe ? L("نوبت شماست", "Your turn") : L("در حال تفکر…", "Thinking…")) : "");
|
||||||
|
|
||||||
el.innerHTML = `
|
el.innerHTML = `
|
||||||
@@ -428,8 +428,16 @@ function renderModalContainer() {
|
|||||||
let subtitle = "";
|
let subtitle = "";
|
||||||
|
|
||||||
if (isDraw) {
|
if (isDraw) {
|
||||||
title = S.result_reason === "stalemate" ? L("پات — مساوی!", "Stalemate — Draw!") : L("بازی مساوی شد", "Draw!");
|
if (S.result_reason === "stalemate") {
|
||||||
subtitle = L("هیچکدام از طرفین برنده نشد", "No side achieved victory");
|
title = L("پات — مساوی!", "Stalemate — Draw!");
|
||||||
|
subtitle = L("شاه راه حرکتی ندارد", "King has no legal moves");
|
||||||
|
} else if (S.result_reason === "insufficient_material") {
|
||||||
|
title = L("تساوی — عدم کفایت مهرهها", "Draw — Insufficient Material");
|
||||||
|
subtitle = L("مهره کافی برای مات کردن در صفحه وجود ندارد", "Neither side has sufficient pieces to force checkmate");
|
||||||
|
} else {
|
||||||
|
title = L("بازی مساوی شد", "Draw!");
|
||||||
|
subtitle = L("هیچکدام از طرفین برنده نشد", "No side achieved victory");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const winnerName = S.players[S.winner!]?.name;
|
const winnerName = S.players[S.winner!]?.name;
|
||||||
title = `${winnerName} ${L("برنده شد! 🏆", "Wins! 🏆")}`;
|
title = `${winnerName} ${L("برنده شد! 🏆", "Wins! 🏆")}`;
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Chess — 3miz</title>
|
<title>Chess — 3miz</title>
|
||||||
<script type="module" crossorigin src="./assets/index-Cj8AUTlf.js"></script>
|
<script type="module" crossorigin src="./assets/index-C5l45jZa.js"></script>
|
||||||
<link rel="stylesheet" crossorigin href="./assets/index-BJNpcCwx.css">
|
<link rel="stylesheet" crossorigin href="./assets/index-BJNpcCwx.css">
|
||||||
</head>
|
</head>
|
||||||
<body><div id="app">…</div></body>
|
<body><div id="app">…</div></body>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
"bundle_format": 1,
|
"bundle_format": 1,
|
||||||
"kind": "source",
|
"kind": "source",
|
||||||
"game_id": "chess",
|
"game_id": "chess",
|
||||||
"version": "1.3.0",
|
"version": "1.4.0",
|
||||||
"server": { "entry": "server/main.ts" },
|
"server": { "entry": "server/main.ts" },
|
||||||
"client": { "entry": "index.html" },
|
"client": { "entry": "index.html" },
|
||||||
"catalog": {
|
"catalog": {
|
||||||
|
|||||||
+3
-3
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 1,
|
"manifest_version": 1,
|
||||||
"game_id": "chess",
|
"game_id": "chess",
|
||||||
"version": "1.3.0",
|
"version": "1.4.0",
|
||||||
"protocol": "socketio",
|
"protocol": "socketio",
|
||||||
"protocol_version": 1,
|
"protocol_version": 1,
|
||||||
"title": {
|
"title": {
|
||||||
@@ -81,14 +81,14 @@
|
|||||||
],
|
],
|
||||||
"achievements": [],
|
"achievements": [],
|
||||||
"client": {
|
"client": {
|
||||||
"entry": "https://g.3miz.ir/chess/1.3.0/index.html",
|
"entry": "https://g.3miz.ir/chess/1.4.0/index.html",
|
||||||
"integrity": "sha384-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
"integrity": "sha384-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
||||||
"aspect": "1:1",
|
"aspect": "1:1",
|
||||||
"min_width_px": 320,
|
"min_width_px": 320,
|
||||||
"supports_portrait": true
|
"supports_portrait": true
|
||||||
},
|
},
|
||||||
"server": {
|
"server": {
|
||||||
"image": "registry.3miz.ir/games/chess:1.3.0",
|
"image": "registry.3miz.ir/games/chess:1.4.0",
|
||||||
"max_tables_per_instance": 500,
|
"max_tables_per_instance": 500,
|
||||||
"checkpoint_every_inputs": 20,
|
"checkpoint_every_inputs": 20,
|
||||||
"snapshot_every_messages": 50,
|
"snapshot_every_messages": 50,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
isMoveLegalFor,
|
isMoveLegalFor,
|
||||||
isKingInCheck,
|
isKingInCheck,
|
||||||
isCheckmate,
|
isCheckmate,
|
||||||
|
hasInsufficientMaterial,
|
||||||
type State,
|
type State,
|
||||||
type Piece,
|
type Piece,
|
||||||
type PieceType,
|
type PieceType,
|
||||||
@@ -293,6 +294,7 @@ function quiescence(
|
|||||||
color: "w" | "b",
|
color: "w" | "b",
|
||||||
depthLimit: number
|
depthLimit: number
|
||||||
): number {
|
): number {
|
||||||
|
if (hasInsufficientMaterial(s)) return 0;
|
||||||
const standPat = (color === "w" ? 1 : -1) * evaluateStatic(s);
|
const standPat = (color === "w" ? 1 : -1) * evaluateStatic(s);
|
||||||
if (standPat >= beta) return beta;
|
if (standPat >= beta) return beta;
|
||||||
if (standPat > alpha) alpha = standPat;
|
if (standPat > alpha) alpha = standPat;
|
||||||
@@ -333,6 +335,10 @@ function negamax(
|
|||||||
return -100000 + ply;
|
return -100000 + ply;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasInsufficientMaterial(s)) {
|
||||||
|
return 0; // Drawn position
|
||||||
|
}
|
||||||
|
|
||||||
if (depth <= 0) {
|
if (depth <= 0) {
|
||||||
if (quiesceDepth > 0) {
|
if (quiesceDepth > 0) {
|
||||||
return quiescence(s, alpha, beta, color, quiesceDepth);
|
return quiescence(s, alpha, beta, color, quiesceDepth);
|
||||||
|
|||||||
@@ -290,6 +290,65 @@ export function isStalemate(state: State, color: Color): boolean {
|
|||||||
return state.phase === "play" && !isKingInCheck(state, color) && !hasAnyLegalMove(state, color);
|
return state.phase === "play" && !isKingInCheck(state, color) && !hasAnyLegalMove(state, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if neither side has sufficient material to force checkmate:
|
||||||
|
* - King vs King
|
||||||
|
* - King + Bishop vs King
|
||||||
|
* - King + Knight vs King
|
||||||
|
* - King + Minor vs King + Minor (KB vs KB, KN vs KN, KB vs KN)
|
||||||
|
* - King + two Knights vs King (KNN vs K cannot force checkmate)
|
||||||
|
*/
|
||||||
|
export function hasInsufficientMaterial(state: State): boolean {
|
||||||
|
if (state.phase !== "play") return false;
|
||||||
|
|
||||||
|
let whiteKnights = 0;
|
||||||
|
let whiteBishops: number[] = [];
|
||||||
|
let blackKnights = 0;
|
||||||
|
let blackBishops: number[] = [];
|
||||||
|
|
||||||
|
for (let sq = 0; sq < 64; sq++) {
|
||||||
|
const p = state.board[sq];
|
||||||
|
if (!p) continue;
|
||||||
|
// Any pawn, rook, or queen means checkmate can be forced (or pawn can promote)
|
||||||
|
if (p.type === "p" || p.type === "r" || p.type === "q") {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (p.type === "n") {
|
||||||
|
if (p.color === "w") whiteKnights++;
|
||||||
|
else blackKnights++;
|
||||||
|
} else if (p.type === "b") {
|
||||||
|
if (p.color === "w") whiteBishops.push(sq);
|
||||||
|
else blackBishops.push(sq);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const whiteMinors = whiteKnights + whiteBishops.length;
|
||||||
|
const blackMinors = blackKnights + blackBishops.length;
|
||||||
|
|
||||||
|
// 1. King vs King
|
||||||
|
if (whiteMinors === 0 && blackMinors === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. King + Minor vs King (KB vs K or KN vs K)
|
||||||
|
if ((whiteMinors === 1 && blackMinors === 0) || (whiteMinors === 0 && blackMinors === 1)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. King + Minor vs King + Minor (KB vs KB, KN vs KN, KB vs KN)
|
||||||
|
if (whiteMinors === 1 && blackMinors === 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. King + two Knights vs King (KNN vs K cannot force checkmate)
|
||||||
|
if ((whiteKnights === 2 && whiteMinors === 2 && blackMinors === 0) ||
|
||||||
|
(blackKnights === 2 && blackMinors === 2 && whiteMinors === 0)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
function setDeadline(s: State, t: { now: number; setTimer(id: string, due: number): void; clearTimer?(id: string): void }) {
|
function setDeadline(s: State, t: { now: number; setTimer(id: string, due: number): void; clearTimer?(id: string): void }) {
|
||||||
if (s.timer_id) t.clearTimer?.(s.timer_id);
|
if (s.timer_id) t.clearTimer?.(s.timer_id);
|
||||||
s.timer_n += 1;
|
s.timer_n += 1;
|
||||||
@@ -516,6 +575,7 @@ function postMove(s: State, ctx: Ctx, mover: number) {
|
|||||||
const oppColor = s.players[1 - mover].color;
|
const oppColor = s.players[1 - mover].color;
|
||||||
if (isCheckmate(s, oppColor)) return finish(s, ctx, mover, "checkmate");
|
if (isCheckmate(s, oppColor)) return finish(s, ctx, mover, "checkmate");
|
||||||
if (isStalemate(s, oppColor)) return finish(s, ctx, null, "stalemate");
|
if (isStalemate(s, oppColor)) return finish(s, ctx, null, "stalemate");
|
||||||
|
if (hasInsufficientMaterial(s)) return finish(s, ctx, null, "insufficient_material");
|
||||||
setDeadline(s, ctx);
|
setDeadline(s, ctx);
|
||||||
scheduleBot(s, ctx);
|
scheduleBot(s, ctx);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@3miz/chess-server",
|
"name": "@3miz/chess-server",
|
||||||
"version": "1.3.0",
|
"version": "1.4.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": { "start": "tsx main.ts", "test": "node --test --import tsx ../tests/*.test.ts" },
|
"scripts": { "start": "tsx main.ts", "test": "node --test --import tsx ../tests/*.test.ts" },
|
||||||
|
|||||||
+121
-1
@@ -2,7 +2,7 @@
|
|||||||
import { test } from "node:test";
|
import { test } from "node:test";
|
||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
import { applyInput, initTable, type CreateTable, type Input } from "@3miz/game-server-sdk";
|
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 => {
|
export const squareIndex = (name: string): number => {
|
||||||
const f = name.charCodeAt(0) - 97;
|
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(isCheckmate(s, "b"), false);
|
||||||
assert.equal(isStalemate(s, "b"), true);
|
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