Switching chess almost OK, Extinction seems OK, Crazyhouse advanced draft but to...
[vchess.git] / public / javascripts / variants / Crazyhouse.js
index 0ee4bd3..9cb0768 100644 (file)
@@ -2,7 +2,7 @@ class CrazyhouseRules extends ChessRules
 {
        initVariables(fen)
        {
-               super.initVariables();
+               super.initVariables(fen);
                // Also init reserves (used by the interface to show landing pieces)
                const V = VariantRules;
                this.reserve =
@@ -24,59 +24,111 @@ class CrazyhouseRules extends ChessRules
                                [V.QUEEN]: 0,
                        }
                };
-               // It may be a continuation: adjust numbers of pieces according to captures + rebirths
-               // TODO
+               // May be a continuation: adjust numbers of pieces according to captures + rebirths
+               this.moves.forEach(m => {
+                       if (m.vanish.length == 2)
+                               this.reserve[m.appear[0].c][m.vanish[1].p]++;
+                       else if (m.vanish.length == 0)
+                               this.reserve[m.appear[0].c][m.appear[0].p]--;
+               });
        }
 
        // Used by the interface:
-       getReservePieces(color)
+       getReservePpath(color, index)
        {
-               return {
-                       [color+V.PAWN]: this.reserve[color][V.PAWN],
-                       [color+V.ROOK]: this.reserve[color][V.ROOK],
-                       [color+V.KNIGHT]: this.reserve[color][V.KNIGHT],
-                       [color+V.BISHOP]: this.reserve[color][V.BISHOP],
-                       [color+V.QUEEN]: this.reserve[color][V.QUEEN],
-               };
+               return color + VariantRules.RESERVE_PIECES[index];
        }
 
-       getPotentialMovesFrom([x,y])
+       // Put an ordering on reserve pieces
+       static get RESERVE_PIECES() {
+               const V = VariantRules;
+               return [V.PAWN,V.ROOK,V.KNIGHT,V.BISHOP,V.QUEEN];
+       }
+
+       getReserveMoves([x,y])
        {
-               let moves = super.getPotentialMovesFrom([x,y]);
-               // Add landing moves:
                const color = this.turn;
-               Object.keys(this.reserve[color]).forEach(p => {
-
-                       moves.push(...); //concat... just appear
-               });
+               const p = VariantRules.RESERVE_PIECES[y];
+               if (this.reserve[color][p] == 0)
+                       return [];
+               let moves = [];
+               for (let i=0; i<sizeX; i++)
+               {
+                       for (let j=0; j<sizeY; j++)
+                       {
+                               if (this.board[i][j] != VariantRules.EMPTY)
+                               {
+                                       let mv = new Move({
+                                               appear: [
+                                                       new PiPo({
+                                                               x: i,
+                                                               y: j,
+                                                               c: color,
+                                                               p: p
+                                                       })
+                                               ]
+                                       });
+                                       moves.push(mv);
+                               }
+                       }
+               }
                return moves;
        }
 
-       // TODO: condition "if this is reserve" --> special square !!! coordinates ??
-       getPossibleMovesFrom(sq)
+       getPotentialMovesFrom([x,y])
        {
-               // Assuming color is right (already checked)
-               return this.filterValid( this.getPotentialMovesFrom(sq) );
+               const sizeX = VariantRules.size[0];
+               if (x < sizeX)
+                       return super.getPotentialMovesFrom([x,y]);
+               // Reserves, outside of board: x == sizeX
+               return this.getReserveMoves([x,y]);
        }
 
-       // TODO: add reserve moves
        getAllValidMoves()
        {
-
+               let moves = super.getAllValidMoves();
+               const color = this.turn;
+               const sizeX = VariantRules.size[0];
+               for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++)
+                       moves = moves.concat(this.getReserveMoves([sizeX,i]));
+               return this.filterValid(moves);
        }
 
-       // TODO: also
        atLeastOneMove()
        {
-
+               if (!super.atLeastOneMove())
+               {
+                       const sizeX = VariantRules.size[0];
+                       // Scan for reserve moves
+                       for (let i=0; i<VariantRules.RESERVE_PIECES.length; i++)
+                       {
+                               let moves = this.filterValid(this.getReserveMoves([sizeX,i]));
+                               if (moves.length > 0)
+                                       return true;
+                       }
+                       return false;
+               }
+               return true;
        }
 
-       // TODO: update reserve
        updateVariables(move)
        {
+               super.updateVariables(move);
+               const color = this.turn;
+               if (move.vanish.length==2)
+                       this.reserve[color][move.appear[0].p]++;
+               if (move.vanish.length==0)
+                       this.reserve[color][move.appear[0].p]--;
        }
+
        unupdateVariables(move)
        {
+               super.unupdateVariables(move);
+               const color = this.turn;
+               if (move.vanish.length==2)
+                       this.reserve[color][move.appear[0].p]--;
+               if (move.vanish.length==0)
+                       this.reserve[color][move.appear[0].p]++;
        }
 
        static get SEARCH_DEPTH() { return 2; } //high branching factor