Step toward a one-page application
[vchess.git] / public / javascripts / variants / Losers.js
1 class LosersRules extends ChessRules
2 {
3 static get HasFlags() { return false; }
4
5 getPotentialPawnMoves([x,y])
6 {
7 let moves = super.getPotentialPawnMoves([x,y]);
8
9 // Complete with promotion(s) into king, if possible
10 const color = this.turn;
11 const shift = (color == "w" ? -1 : 1);
12 const lastRank = (color == "w" ? 0 : V.size.x-1);
13 if (x+shift == lastRank)
14 {
15 // Normal move
16 if (this.board[x+shift][y] == V.EMPTY)
17 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING}));
18 // Captures
19 if (y>0 && this.canTake([x,y], [x+shift,y-1])
20 && this.board[x+shift][y-1] != V.EMPTY)
21 {
22 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:V.KING}));
23 }
24 if (y<V.size.y-1 && this.canTake([x,y], [x+shift,y+1])
25 && this.board[x+shift][y+1] != V.EMPTY)
26 {
27 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:V.KING}));
28 }
29 }
30
31 return moves;
32 }
33
34 getPotentialKingMoves(sq)
35 {
36 // No castle:
37 return this.getSlideNJumpMoves(sq,
38 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
39 }
40
41 // Stop at the first capture found (if any)
42 atLeastOneCapture()
43 {
44 const color = this.turn;
45 const oppCol = V.GetOppCol(color);
46 for (let i=0; i<V.size.x; i++)
47 {
48 for (let j=0; j<V.size.y; j++)
49 {
50 if (this.board[i][j] != V.EMPTY && this.getColor(i,j) != oppCol)
51 {
52 const moves = this.getPotentialMovesFrom([i,j]);
53 if (moves.length > 0)
54 {
55 for (let k=0; k<moves.length; k++)
56 {
57 if (moves[k].vanish.length==2 && this.filterValid([moves[k]]).length > 0)
58 return true;
59 }
60 }
61 }
62 }
63 }
64 return false;
65 }
66
67 // Trim all non-capturing moves
68 static KeepCaptures(moves)
69 {
70 return moves.filter(m => { return m.vanish.length == 2; });
71 }
72
73 getPossibleMovesFrom(sq)
74 {
75 let moves = this.filterValid( this.getPotentialMovesFrom(sq) );
76 // This is called from interface: we need to know if a capture is possible
77 if (this.atLeastOneCapture())
78 moves = V.KeepCaptures(moves);
79 return moves;
80 }
81
82 getAllValidMoves()
83 {
84 let moves = super.getAllValidMoves();
85 if (moves.some(m => { return m.vanish.length == 2; }))
86 moves = V.KeepCaptures(moves);
87 return moves;
88 }
89
90 underCheck(color)
91 {
92 return false; //No notion of check
93 }
94
95 getCheckSquares(move)
96 {
97 return [];
98 }
99
100 // No variables update because no royal king + no castling
101 updateVariables(move) { }
102 unupdateVariables(move) { }
103
104 checkGameEnd()
105 {
106 // No valid move: you win!
107 return this.turn == "w" ? "1-0" : "0-1";
108 }
109
110 static get VALUES()
111 {
112 // Experimental...
113 return {
114 'p': 1,
115 'r': 7,
116 'n': 3,
117 'b': 3,
118 'q': 5,
119 'k': 5
120 };
121 }
122
123 static get SEARCH_DEPTH() { return 4; }
124
125 evalPosition()
126 {
127 return - super.evalPosition(); //better with less material
128 }
129
130 static GenRandInitFen()
131 {
132 let pieces = { "w": new Array(8), "b": new Array(8) };
133 // Shuffle pieces on first and last rank
134 for (let c of ["w","b"])
135 {
136 let positions = _.range(8);
137
138 // Get random squares for bishops
139 let randIndex = 2 * _.random(3);
140 let bishop1Pos = positions[randIndex];
141 // The second bishop must be on a square of different color
142 let randIndex_tmp = 2 * _.random(3) + 1;
143 let bishop2Pos = positions[randIndex_tmp];
144 // Remove chosen squares
145 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
146 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
147
148 // Get random squares for knights
149 randIndex = _.random(5);
150 let knight1Pos = positions[randIndex];
151 positions.splice(randIndex, 1);
152 randIndex = _.random(4);
153 let knight2Pos = positions[randIndex];
154 positions.splice(randIndex, 1);
155
156 // Get random square for queen
157 randIndex = _.random(3);
158 let queenPos = positions[randIndex];
159 positions.splice(randIndex, 1);
160
161 // Random square for king (no castle)
162 randIndex = _.random(2);
163 let kingPos = positions[randIndex];
164 positions.splice(randIndex, 1);
165
166 // Rooks positions are now fixed
167 let rook1Pos = positions[0];
168 let rook2Pos = positions[1];
169
170 // Finally put the shuffled pieces in the board array
171 pieces[c][rook1Pos] = 'r';
172 pieces[c][knight1Pos] = 'n';
173 pieces[c][bishop1Pos] = 'b';
174 pieces[c][queenPos] = 'q';
175 pieces[c][kingPos] = 'k';
176 pieces[c][bishop2Pos] = 'b';
177 pieces[c][knight2Pos] = 'n';
178 pieces[c][rook2Pos] = 'r';
179 }
180 return pieces["b"].join("") +
181 "/pppppppp/8/8/8/8/PPPPPPPP/" +
182 pieces["w"].join("").toUpperCase() +
183 " w -"; //no en-passant
184 }
185 }