Generalize code in VariantRules. Considerable speed-up for checkered bot. Prepare...
[vchess.git] / public / javascripts / variants / Antiking.js
index ac25c73..360deaf 100644 (file)
@@ -7,6 +7,12 @@ class AntikingRules
        }
 
        static get ANTIKING() { return 'a'; }
+       
+       initVariables(fen)
+       {
+               super.initVariables(fen);
+               // TODO: initialize this.antikingPos[...]
+       }
 
        canTake(color1, color2, [x,y])
        {
@@ -26,23 +32,14 @@ class AntikingRules
                }
        }
 
-// TODO: generaliser (à moindre coût) base_rules ? Ou spécialiser variantes ?
-
-       getPotentialAntikingMoves(x, y, c)
+       getPotentialAntikingMoves([x,y])
        {
                // TODO
        }
 
-// TODO: need to re-think some logic, since antikings capture same color
-
-       isAttacked(sq, color)
+       isAttacked(sq, colors)
        {
-               return (this.isAttackedByPawn(sq, color)
-                       || this.isAttackedByRook(sq, color)
-                       || this.isAttackedByKnight(sq, color)
-                       || this.isAttackedByBishop(sq, color)
-                       || this.isAttackedByQueen(sq, color)
-                       || this.isAttackedByKing(sq, color)); //...
+               return (super.isAttacked(sq, colors) || this.isAttackedByAntiking(sq, colors));
        }
 
        isAttackedByAntiking(sq, color)
@@ -50,8 +47,9 @@ class AntikingRules
                // TODO
        }
 
-       underCheck(move, c)
+       underCheck(move)
        {
+               const c = this.turn;
                this.play(move);
                let res = this.isAttacked(this.kingPos[c], this.getOppCol(c));
                // TODO: also check that antiking is still in check
@@ -59,34 +57,18 @@ class AntikingRules
                return res;
        }
 
-       getCheckSquares(move, c)
+       getCheckSquares(move)
        {
                this.play(move);
+               const c = this.turn;
                // TODO
                let res = this.isAttacked(this.kingPos[c], this.getOppCol(c))
-                       ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ] //need to duplicate!
+                       ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ]
                        : [ ];
                this.undo(move);
                return res;
        }
 
-       // Apply a move on board
-       static PlayOnBoard(board, move)
-       {
-               for (let psq of move.vanish)
-                       board[psq.x][psq.y] = VariantRules.EMPTY;
-               for (let psq of move.appear)
-                       board[psq.x][psq.y] = psq.c + psq.p;
-       }
-       // Un-apply the played move
-       static UndoOnBoard(board, move)
-       {
-               for (let psq of move.appear)
-                       board[psq.x][psq.y] = VariantRules.EMPTY;
-               for (let psq of move.vanish)
-                       board[psq.x][psq.y] = psq.c + psq.p;
-       }
-
        // TODO: need antikingPos as well
        updateVariables(move)
        {
@@ -106,7 +88,7 @@ class AntikingRules
                return color == "w" ? "0-1" : "1-0";
        }
 
-       // Pieces values
+       // Pieces values (TODO: use Object.assign() + ChessRules.VALUES ?)
        static get VALUES() {
                return {
                        'p': 1,
@@ -121,55 +103,8 @@ class AntikingRules
 
        static GenRandInitFen()
        {
-               // TODO: no need all code, just add an antiking at rondom on 3rd ranks
-               let pieces = [new Array(8), new Array(8)];
-               // Shuffle pieces on first and last rank
-               for (let c = 0; c <= 1; c++)
-               {
-                       let positions = _.range(8);
-
-                       // Get random squares for bishops
-                       let randIndex = 2 * _.random(3);
-                       let bishop1Pos = positions[randIndex];
-                       // The second bishop must be on a square of different color
-                       let randIndex_tmp = 2 * _.random(3) + 1;
-                       let bishop2Pos = positions[randIndex_tmp];
-                       // Remove chosen squares
-                       positions.splice(Math.max(randIndex,randIndex_tmp), 1);
-                       positions.splice(Math.min(randIndex,randIndex_tmp), 1);
-
-                       // Get random squares for knights
-                       randIndex = _.random(5);
-                       let knight1Pos = positions[randIndex];
-                       positions.splice(randIndex, 1);
-                       randIndex = _.random(4);
-                       let knight2Pos = positions[randIndex];
-                       positions.splice(randIndex, 1);
-
-                       // Get random square for queen
-                       randIndex = _.random(3);
-                       let queenPos = positions[randIndex];
-                       positions.splice(randIndex, 1);
-
-                       // Rooks and king positions are now fixed, because of the ordering rook-king-rook
-                       let rook1Pos = positions[0];
-                       let kingPos = positions[1];
-                       let rook2Pos = positions[2];
-
-                       // Finally put the shuffled pieces in the board array
-                       pieces[c][rook1Pos] = 'r';
-                       pieces[c][knight1Pos] = 'n';
-                       pieces[c][bishop1Pos] = 'b';
-                       pieces[c][queenPos] = 'q';
-                       pieces[c][kingPos] = 'k';
-                       pieces[c][bishop2Pos] = 'b';
-                       pieces[c][knight2Pos] = 'n';
-                       pieces[c][rook2Pos] = 'r';
-               }
-               let fen = pieces[0].join("") +
-                       "/pppppppp/8/8/8/8/PPPPPPPP/" +
-                       pieces[1].join("").toUpperCase() +
-                       " 1111"; //add flags
-               return fen;
+               let randFen = ChessRules.GenRandInitFen();
+               // TODO: just add an antiking at random on 3rd ranks
+               return randFen;
        }
 }