Experimental: improve animation, reduce lags in stack moves sending. Add Allmate
[xogo.git] / variants / Allmate / class.js
CommitLineData
5f08c59b
BA
1import ChessRules from "/base_rules.js";
2import PiPo from "/utils/PiPo.js";
3import Move from "/utils/Move.js";
4
5export default class AllmateRules extends ChessRules {
6
7 static get Options() {
8 return {
9 select: C.Options.select,
10 styles: [
11 "cylinder",
12 "madrasi",
13 "zen"
14 ]
15 };
16 }
17
18 get hasEnpassant() {
19 return false;
20 }
21 get hasMoveStack() {
22 return true;
23 }
24
25 setOtherVariables(fenParsed) {
26 super.setOtherVariables(fenParsed);
27 this.curMove = null;
28 }
29
30 getPotentialMovesFrom(sq) {
31 // Remove direct captures:
32 return super.getPotentialMovesFrom(sq)
33 .filter(m => m.vanish.length == m.appear.length);
34 }
35
36 // Called "recursively" before a move is played, until no effect
37 computeNextMove(move) {
38 if (move.appear.length > 0)
39 this.curMove = move;
40 const color = this.turn;
41 const oppCol = C.GetOppCol(this.turn);
42 let mv = new Move({
43 start: this.curMove.end,
44 end: this.curMove.end,
45 appear: [],
46 vanish: []
47 });
48 this.playOnBoard(move);
49 for (let i=0; i<this.size.x; i++) {
50 for (let j=0; j<this.size.y; j++) {
51 if (this.getColor(i, j) == oppCol && this.isMated(i, j, color)) {
52 mv.vanish.push(
53 new PiPo({x: i, y: j, c: oppCol, p: this.getPiece(i, j)})
54 );
55 }
56 }
57 }
58 this.undoOnBoard(move);
59 move.next = (mv.vanish.length > 0 ? mv : null);
60 }
61
62 // is piece on square x,y mated by color?
63 isMated(x, y, color) {
64 const myColor = C.GetOppCol(color);
65 if (!this.underCheck([x, y], color))
66 return false;
67 for (let i=0; i<this.size.x; i++) {
68 for (let j=0; j<this.size.y; j++) {
69 if (this.getColor(i, j) == myColor) {
70 const movesIJ = super.getPotentialMovesFrom([i, j], myColor);
71 for (let move of movesIJ) {
72 this.playOnBoard(move);
73 let testSquare = [x, y];
74 if (i == x && j == y) {
75 // The mated-candidate has moved itself
76 testSquare = [move.end.x, move.end.y]; }
77 const res = this.underCheck(testSquare, color);
78 this.undoOnBoard(move);
79 if (!res)
80 return false;
81 }
82 }
83 }
84 }
85 return true;
86 }
87
88 // No "under check" conditions in castling
89 getCastleMoves(sq) {
90 return super.getCastleMoves(sq, null, "castleInCheck");
91 }
92
93 filterValid(moves) {
94 return moves; //TODO?: over-simplification to be fixed later
95 }
96
97};