0fd7647e62414ec5edf60f38604a9a640d422ee5
1 class AtomicRules
extends ChessRules
3 getPotentialMovesFrom([x
,y
], c
, lastMove
)
5 let moves
= super.getPotentialMovesFrom([x
,y
], c
, lastMove
);
9 if (m
.vanish
.length
> 1 && m
.appear
.length
<= 1) //avoid castles
11 // Explosion! TODO: drop moves which explode our king here
12 let steps
= [ [-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1] ];
13 for (let step
of steps
)
15 let x
= m
.end
.x
+ step
[0];
16 let y
= m
.end
.y
+ step
[1];
17 if (x
>=0 && x
<8 && y
>=0 && y
<8 && this.board
[x
][y
] != VariantRules
.EMPTY
18 && this.getPiece(x
,y
) != VariantRules
.PAWN
)
20 m
.vanish
.push(new PiPo({p:this.getPiece(x
,y
),c:this.getColor(x
,y
),x:x
,y:y
}));
23 m
.end
= {x:m
.appear
[0].x
, y:m
.appear
[0].y
};
24 m
.appear
.pop(); //Nothin appears in this case
31 getPotentialKingMoves(x
, y
, c
)
33 // King cannot capture:
35 let [sizeX
,sizeY
] = VariantRules
.size
;
36 const steps
= VariantRules
.steps
[VariantRules
.QUEEN
];
37 for (let step
of steps
)
41 if (i
>=0 && i
<sizeX
&& j
>=0 && j
<sizeY
&& this.board
[i
][j
] == VariantRules
.EMPTY
)
42 moves
.push(this.getBasicMove(x
, y
, i
, j
));
44 return moves
.concat(this.getCastleMoves(x
,y
,c
));
49 if (this.getPiece(sq
[0],sq
[1]) == VariantRules
.KING
&& this.isAttackedByKing(sq
, color
))
50 return false; //king cannot take...
51 return (this.isAttackedByPawn(sq
, color
)
52 || this.isAttackedByRook(sq
, color
)
53 || this.isAttackedByKnight(sq
, color
)
54 || this.isAttackedByBishop(sq
, color
)
55 || this.isAttackedByQueen(sq
, color
));
60 super.updateVariables(move);
62 const c
= this.getColor(move.start
.x
,move.start
.y
);
63 // Next condition to avoid conflicts with harmless castle moves
64 if (c
!= this.getColor(move.end
.x
,move.end
.y
)
65 && this.board
[move.end
.x
][move.end
.y
] != VariantRules
.EMPTY
)
67 const oppCol
= this.getOppCol(c
);
68 const oppFirstRank
= (oppCol
== "w" ? 7 : 0);
70 // Did we explode our king ? (TODO: remove move earlier)
71 if (Math
.abs(this.kingPos
[c
][0]-move.end
.x
) <= 1
72 && Math
.abs(this.kingPos
[c
][1]-move.end
.y
) <= 1)
74 this.kingPos
[c
] = [-1,-1];
75 this.flags
[c
] = [false,false];
78 // Did we explode opponent king ?
79 if (Math
.abs(this.kingPos
[oppCol
][0]-move.end
.x
) <= 1
80 && Math
.abs(this.kingPos
[oppCol
][1]-move.end
.y
) <= 1)
82 this.kingPos
[oppCol
] = [-1,-1];
83 this.flags
[oppCol
] = [false,false];
87 // Now check if opponent init rook(s) exploded
88 if (Math
.abs(move.end
.x
-oppFirstRank
) <= 1)
90 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[oppCol
][0]) <= 1)
91 this.flags
[oppCol
][0] = false;
92 if (Math
.abs(move.end
.y
-this.INIT_COL_ROOK
[oppCol
][1]) <= 1)
93 this.flags
[oppCol
][1] = false;
99 unupdateVariables(move)
101 super.unupdateVariables(move);
102 const c
= this.getColor(move.start
.x
,move.start
.y
);
103 const oppCol
= this.getOppCol(c
);
104 if ([this.kingPos
[c
][0],this.kingPos
[oppCol
][0]].some(e
=> { return e
< 0; }))
106 // There is a chance that last move blowed some king away..
107 for (let psq
of move.vanish
)
110 this.kingPos
[psq
.c
==c
? c : oppCol
] = [psq
.x
, psq
.y
];
117 const oppCol
= this.getOppCol(c
);
120 // If our king disappeared, move is not valid
121 if (this.kingPos
[c
][0] < 0)
123 // If opponent king disappeared, move is valid
124 else if (this.kingPos
[oppCol
][0] < 0)
126 // Otherwise, if we remain under check, move is not valid
128 res
= this.isAttacked(this.kingPos
[c
], oppCol
);
133 getCheckSquares(move, c
)
135 const saveKingPos
= this.kingPos
[c
]; //king might explode
138 if (this.kingPos
[c
][0] < 0)
140 else if (this.isAttacked(this.kingPos
[c
], this.getOppCol(c
)))
141 res
= [ this.kingPos
[c
] ]
148 const kp
= this.kingPos
[color
];
149 if (kp
[0] < 0) //king disappeared
150 return color
== "w" ? "0-1" : "1-0";
151 if (!this.isAttacked(kp
, this.getOppCol(color
)))
154 return color
== "w" ? "0-1" : "1-0";