Some improvements. Testing variants: Baroque has some issues
[xogo.git] / variants / Allmate / class.js
1 import ChessRules from "/base_rules.js";
2 import PiPo from "/utils/PiPo.js";
3 import Move from "/utils/Move.js";
4
5 export 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
22 setOtherVariables(fenParsed) {
23 super.setOtherVariables(fenParsed);
24 this.curMove = null;
25 }
26
27 getPotentialMovesFrom(sq) {
28 // Remove direct captures:
29 return super.getPotentialMovesFrom(sq)
30 .filter(m => m.vanish.length == m.appear.length);
31 }
32
33 // Called "recursively" before a move is played, until no effect
34 computeNextMove(move) {
35 if (move.appear.length > 0)
36 this.curMove = move;
37 const color = this.turn;
38 const oppCol = C.GetOppTurn(this.turn);
39 let mv = new Move({
40 start: this.curMove.end,
41 end: this.curMove.end,
42 appear: [],
43 vanish: []
44 });
45 this.playOnBoard(move);
46 for (let i=0; i<this.size.x; i++) {
47 for (let j=0; j<this.size.y; j++) {
48 if (this.getColor(i, j) == oppCol && this.isMated(i, j, color)) {
49 mv.vanish.push(
50 new PiPo({x: i, y: j, c: oppCol, p: this.getPiece(i, j)})
51 );
52 }
53 }
54 }
55 this.undoOnBoard(move);
56 move.next = (mv.vanish.length > 0 ? mv : null);
57 }
58
59 // is piece on square x,y mated by color?
60 isMated(x, y, color) {
61 const myColor = C.GetOppTurn(color);
62 if (!super.underAttack([x, y], color))
63 return false;
64 for (let i=0; i<this.size.x; i++) {
65 for (let j=0; j<this.size.y; j++) {
66 if (this.getColor(i, j) == myColor) {
67 const movesIJ = super.getPotentialMovesFrom([i, j], myColor);
68 for (let move of movesIJ) {
69 this.playOnBoard(move);
70 let testSquare = [x, y];
71 if (i == x && j == y) {
72 // The mated-candidate has moved itself
73 testSquare = [move.end.x, move.end.y]; }
74 const res = this.underAttack(testSquare, color);
75 this.undoOnBoard(move);
76 if (!res)
77 return false;
78 }
79 }
80 }
81 }
82 return true;
83 }
84
85 underCheck() {
86 return false; //not relevant here
87 }
88
89 filterValid(moves) {
90 return moves; //TODO?: over-simplification to be fixed later
91 }
92
93 };