- const oppCol = V.GetOppCol(color);
- if (this.getColor(x, y) != color) {
- // Look in every direction for a friendly pusher/puller.
- // This means that the action is done without moving.
- return this.getPactions([x, y], null, color);
- } else {
- // Playing my pieces: do they attack an enemy?
- // If yes ... TODO
- //this.getPattacks(sq, [x, y]);
- // Temporary:
- return super.getPotentialMovesFrom([x, y]);
+ if (this.subTurn == 1) {
+ // Free to play any move or action:
+ return (
+ super.getPotentialMovesFrom([x, y])
+ .concat(this.getPactions([x, y], color))
+ );
+ }
+ // If subTurn == 2 then we should have a first move,
+ // which restrict what we can play now.
+ // Case 1: an opponent's piece moved: we can only move the piece which
+ // did the action, in the moving direction.
+ // Case 2: one of our pieces moved: either by action or by itself.
+ // Just check if it could be a normal move. If yes, allow both.
+ const L = this.firstMove.length;
+ const fm = this.firstMove[L-1];
+ if (fm.vanish[0].c != color) {
+ // Case 1: TODO
+ }
+ else {
+ // Case 2: TODO
+ // Use fm.start.x, fm.start.y, fm.end.x, fm.end.y, fm.vanish[0].c
+ // Search for the piece doing the action "pa": the action type
+ // is deduced from pa relative positon then.
+ }
+ }
+
+ // Does m2 un-do m1 ? (to disallow undoing actions)
+ oppositeMoves(m1, m2) {
+ const isEqual = (av1, av2) => {
+ // Precondition: av1 and av2 length = 2
+ for (let av of av1) {
+ const avInAv2 = av2.find(elt => {
+ return (
+ elt.x == av.x &&
+ elt.y == av.y &&
+ elt.c == av.c &&
+ elt.p == av.p
+ );
+ });
+ if (!avInAv2) return false;
+ }
+ return true;
+ };
+ return (
+ m1.appear.length == 2 &&
+ m2.appear.length == 2 &&
+ m1.vanish.length == 2 &&
+ m2.vanish.length == 2 &&
+ isEqual(m1.appear, m2.vanish) &&
+ isEqual(m1.vanish, m2.appear)
+ );
+ }
+
+ getAmove(move1, move2) {
+ // Just merge (one is action one is move, one may be empty)
+ return {
+ appear: move1.appear.concat(move2.appear),
+ vanish: move1.vanish.concat(move2.vanish)
+ }
+ }
+
+ filterValid(moves) {
+ const color = this.turn;
+ if (this.subTurn == 1) {
+ return moves.filter(m => {
+ // A move is valid either if it doesn't result in a check,
+ // or if a second move is possible to counter the check
+ // (not undoing a potential move + action of the opponent)
+ this.play(m);
+ let res = this.underCheck(color);
+ if (res) {
+ const moves2 = this.getAllPotentialMoves();
+ for (m2 of moves2) {
+ this.play(m2);
+ const res2 = this.underCheck(color);
+ this.undo(m2);
+ if (!res2) {
+ res = false;
+ break;
+ }
+ }
+ }
+ this.undo(m);
+ return !res;
+ });