Improve Crazyhouse; still not debugged
[vchess.git] / public / javascripts / variants / Loser.js
1 class LoserRules extends ChessRules
2 {
3 initVariables(fen)
4 {
5 // No castling, hence no flags; but flags defined for compatibility
6 this.flags = "-";
7 const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
8 this.epSquares = [ epSq ];
9 }
10
11 setFlags(fen) { }
12
13 getPotentialPawnMoves([x,y])
14 {
15 let moves = super.getPotentialPawnMoves([x,y]);
16
17 // Complete with promotion(s) into king, if possible
18 const color = this.turn;
19 const V = VariantRules;
20 const [sizeX,sizeY] = VariantRules.size;
21 const shift = (color == "w" ? -1 : 1);
22 const lastRank = (color == "w" ? 0 : sizeX-1);
23 if (x+shift == lastRank)
24 {
25 let p = V.KING;
26 // Normal move
27 if (this.board[x+shift][y] == V.EMPTY)
28 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
29 // Captures
30 if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
31 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
32 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
33 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
34 }
35
36 return moves;
37 }
38
39 getPotentialKingMoves(sq)
40 {
41 const V = VariantRules;
42 return this.getSlideNJumpMoves(sq,
43 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
44 }
45
46 // Stop at the first capture found (if any)
47 atLeastOneCapture()
48 {
49 const color = this.turn;
50 const oppCol = this.getOppCol(color);
51 const [sizeX,sizeY] = VariantRules.size;
52 for (let i=0; i<sizeX; i++)
53 {
54 for (let j=0; j<sizeY; j++)
55 {
56 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
57 {
58 const moves = this.getPotentialMovesFrom([i,j]);
59 if (moves.length > 0)
60 {
61 for (let k=0; k<moves.length; k++)
62 {
63 if (moves[k].vanish.length==2 && this.filterValid([moves[k]]).length > 0)
64 return true;
65 }
66 }
67 }
68 }
69 }
70 return false;
71 }
72
73 // Trim all non-capturing moves
74 static KeepCaptures(moves)
75 {
76 return moves.filter(m => { return m.vanish.length == 2; });
77 }
78
79 getPossibleMovesFrom(sq)
80 {
81 let moves = this.filterValid( this.getPotentialMovesFrom(sq) );
82 // This is called from interface: we need to know if a capture is possible
83 if (this.atLeastOneCapture())
84 moves = VariantRules.KeepCaptures(moves);
85 return moves;
86 }
87
88 getAllValidMoves()
89 {
90 let moves = super.getAllValidMoves();
91 if (moves.some(m => { return m.vanish.length == 2; }))
92 moves = VariantRules.KeepCaptures(moves);
93 return moves;
94 }
95
96 underCheck(move)
97 {
98 return false; //No notion of check
99 }
100
101 getCheckSquares(move)
102 {
103 return [];
104 }
105
106 // Unused:
107 updateVariables(move) { }
108 unupdateVariables(move) { }
109 parseFlags(flags) { }
110
111 checkGameEnd()
112 {
113 // No valid move: you win!
114 return this.turn == "w" ? "1-0" : "0-1";
115 }
116
117 static get VALUES() { //experimental...
118 return {
119 'p': 1,
120 'r': 7,
121 'n': 3,
122 'b': 3,
123 'q': 5,
124 'k': 5
125 };
126 }
127
128 static get SEARCH_DEPTH() { return 4; }
129
130 evalPosition()
131 {
132 return - super.evalPosition(); //better with less material
133 }
134 }