Skip to content

Commit

Permalink
Cannon shogi premoves
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Mar 18, 2024
1 parent 179146e commit c942dab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chessgroundx",
"version": "10.5.3",
"version": "10.5.4",
"description": "Extended lichess.org Chess UI",
"type": "module",
"module": "chessground.js",
Expand Down
40 changes: 32 additions & 8 deletions src/premove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ const wazir: Mobility = (x1, y1, x2, y2) => {
// ferz, met
const ferz: Mobility = (x1, y1, x2, y2) => diff(x1, x2) === diff(y1, y2) && diff(x1, x2) === 1;

const fersAlfil: Mobility = (x1, y1, x2, y2) => {
return diff(x1, x2) === diff(y1, y2) && diff(x1, x2) < 3;
};

const wazirDabbaba: Mobility = (x1, y1, x2, y2) => {
return (x1 === x2 && diff(y1, y2) < 3) || (y1 === y2 && diff(x1, x2) < 3) ;
};

// ouk ferz (can jump two squares forward on its first move)
function ferzOuk(color: cg.Color): Mobility {
return (x1, y1, x2, y2) =>
Expand Down Expand Up @@ -273,6 +281,16 @@ const shogiHorse: Mobility = (x1, y1, x2, y2) => {
return bishop(x1, y1, x2, y2) || wazir(x1, y1, x2, y2);
};

// cannon shogi promoted orthogonal (silver/gold) cannons
const flyingOrthogonalCannon: Mobility = (x1, y1, x2, y2) => {
return rook(x1, y1, x2, y2) || fersAlfil(x1, y1, x2, y2);
};

// cannon shogi promoted diagonal (iron/copper) cannons
const flyingDiagonalCannon: Mobility = (x1, y1, x2, y2) => {
return bishop(x1, y1, x2, y2) || wazirDabbaba(x1, y1, x2, y2);
};

// Define xiangqi palace based on geometry
// The palace is the 3x3 squares in the middle files at each side's end of the board
type Palace = cg.Pos[];
Expand Down Expand Up @@ -335,11 +353,6 @@ function xiangqiKing(color: cg.Color, bd: cg.BoardDimensions): Mobility {
return (x1, y1, x2, y2) => wazir(x1, y1, x2, y2) && p.some(point => point[0] === x2 && point[1] === y2);
}

// shako elephant
const shakoElephant: Mobility = (x1, y1, x2, y2) => {
return diff(x1, x2) === diff(y1, y2) && diff(x1, x2) < 3;
};

// janggi elephant
export const janggiElephant: Mobility = (x1, y1, x2, y2) => {
const xd = diff(x1, x2);
Expand Down Expand Up @@ -699,13 +712,14 @@ function builtinMobility(
case 'minishogi':
case 'gorogoro':
case 'gorogoroplus':
case 'cannonshogi':
return (boardState, key) => {
const piece = boardState.pieces.get(key)!;
const role = piece.role;
const color = piece.color;
switch (role) {
case 'p-piece': // pawn
return shogiPawn(color);
return variant === 'cannonshogi' ? minixiangqiPawn(color) : shogiPawn(color);
case 'l-piece': // lance
return shogiLance(color);
case 'n-piece': // knight
Expand All @@ -721,13 +735,23 @@ function builtinMobility(
case 'ps-piece': // promoted silver
return shogiGold(color);
case 'b-piece': // bishop
case 'u-piece': // gold cannon
case 'a-piece': // silver cannon
return bishop;
case 'r-piece': // rook
case 'i-piece': // iron cannon
case 'c-piece': // copper cannon
return rook;
case 'pr-piece': // dragon (promoted rook)
return shogiDragon;
case 'pb-piece': // horse (promoted bishop), not to be confused with the knight
return shogiHorse;
case 'pu-piece': // promoted gold cannon
case 'pa-piece': // promoted silver cannon
return flyingOrthogonalCannon;
case 'pi-piece': // promoted iron cannon
case 'pc-piece': // promoted copper cannon
return flyingDiagonalCannon;
default:
return noMove;
}
Expand Down Expand Up @@ -888,7 +912,7 @@ function builtinMobility(
case 'q-piece': // queen
return queen;
case 'e-piece': // elephant
return shakoElephant;
return fersAlfil;
case 'k-piece': // king
return kingShako(color, rookFilesOfShako(boardState.pieces, color), canCastle);
default:
Expand Down Expand Up @@ -983,7 +1007,7 @@ function builtinMobility(
case 's-piece': // soldier
return minixiangqiPawn(color);
case 'e-piece': // elephant
return shakoElephant;
return fersAlfil;
case 'a-piece': // advisor
return kingNoCastling;
case 'k-piece': // king
Expand Down

0 comments on commit c942dab

Please sign in to comment.