1 import { ChessRules
, PiPo
} from "@/base_rules";
3 export const VariantRules
= class AtomicRules
extends ChessRules
{
4 getEpSquare(moveOrSquare
) {
5 if (typeof moveOrSquare
!== "object" || moveOrSquare
.appear
.length
> 0)
6 return super.getEpSquare(moveOrSquare
);
7 // Capturing move: no en-passant
11 getPotentialMovesFrom([x
, y
]) {
12 let moves
= super.getPotentialMovesFrom([x
, y
]);
16 // NOTE: if vanish.length==2 and appear.length==2, this is castle
17 if (m
.vanish
.length
> 1 && m
.appear
.length
<= 1) {
18 // Explosion! (TODO?: drop moves which explode our king here)
29 for (let step
of steps
) {
30 let x
= m
.end
.x
+ step
[0];
31 let y
= m
.end
.y
+ step
[1];
34 this.board
[x
][y
] != V
.EMPTY
&&
35 this.getPiece(x
, y
) != V
.PAWN
39 p: this.getPiece(x
, y
),
40 c: this.getColor(x
, y
),
47 m
.end
= { x: m
.appear
[0].x
, y: m
.appear
[0].y
};
48 m
.appear
.pop(); //Nothin appears in this case
55 getPotentialKingMoves([x
, y
]) {
56 // King cannot capture:
58 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
59 for (let step
of steps
) {
60 const i
= x
+ step
[0];
61 const j
= y
+ step
[1];
62 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
)
63 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
65 return moves
.concat(this.getCastleMoves([x
, y
]));
68 isAttacked(sq
, colors
) {
70 this.getPiece(sq
[0], sq
[1]) == V
.KING
&&
71 this.isAttackedByKing(sq
, colors
)
73 return false; //king cannot take...
75 this.isAttackedByPawn(sq
, colors
) ||
76 this.isAttackedByRook(sq
, colors
) ||
77 this.isAttackedByKnight(sq
, colors
) ||
78 this.isAttackedByBishop(sq
, colors
) ||
79 this.isAttackedByQueen(sq
, colors
)
85 if (move.appear
.length
== 0) {
87 const firstRank
= { w: 7, b: 0 };
88 for (let c
of ["w", "b"]) {
89 // Did we explode king of color c ? (TODO: remove move earlier)
91 Math
.abs(this.kingPos
[c
][0] - move.end
.x
) <= 1 &&
92 Math
.abs(this.kingPos
[c
][1] - move.end
.y
) <= 1
94 this.kingPos
[c
] = [-1, -1];
95 this.castleFlags
[c
] = [8, 8];
97 // Now check if init rook(s) exploded
98 if (Math
.abs(move.end
.x
- firstRank
[c
]) <= 1) {
99 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][0]) <= 1)
100 this.castleFlags
[c
][0] = 8;
101 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][1]) <= 1)
102 this.castleFlags
[c
][1] = 8;
110 super.postUndo(move);
112 const oppCol
= V
.GetOppCol(c
);
113 if ([this.kingPos
[c
][0], this.kingPos
[oppCol
][0]].some(e
=> e
< 0)) {
114 // There is a chance that last move blowed some king away..
115 for (let psq
of move.vanish
) {
117 this.kingPos
[psq
.c
== c
? c : oppCol
] = [psq
.x
, psq
.y
];
123 const oppCol
= V
.GetOppCol(color
);
125 // If our king disappeared, move is not valid
126 if (this.kingPos
[color
][0] < 0) res
= true;
127 // If opponent king disappeared, move is valid
128 else if (this.kingPos
[oppCol
][0] < 0) res
= false;
129 // Otherwise, if we remain under check, move is not valid
130 else res
= this.isAttacked(this.kingPos
[color
], [oppCol
]);
134 getCheckSquares(color
) {
137 this.kingPos
[color
][0] >= 0 && //king might have exploded
138 this.isAttacked(this.kingPos
[color
], [V
.GetOppCol(color
)])
140 res
= [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))];
146 const color
= this.turn
;
147 const kp
= this.kingPos
[color
];
150 return color
== "w" ? "0-1" : "1-0";
151 if (this.atLeastOneMove())
153 if (!this.isAttacked(kp
, [V
.GetOppCol(color
)])) return "1/2";
154 return color
== "w" ? "0-1" : "1-0"; //checkmate