Fix chat issues when launching a game
[vchess.git] / client / src / variants / Losers.js
CommitLineData
26c1e3bd 1class LosersRules extends ChessRules
a6abf094 2{
2d7194bd 3 static get HasFlags() { return false; }
7931e479 4
a6abf094
BA
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;
a6abf094 11 const shift = (color == "w" ? -1 : 1);
0b7d99ec 12 const lastRank = (color == "w" ? 0 : V.size.x-1);
a6abf094
BA
13 if (x+shift == lastRank)
14 {
a6abf094
BA
15 // Normal move
16 if (this.board[x+shift][y] == V.EMPTY)
92342261 17 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING}));
a6abf094 18 // Captures
92342261
BA
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 }
0b7d99ec 24 if (y<V.size.y-1 && this.canTake([x,y], [x+shift,y+1])
92342261
BA
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 }
a6abf094
BA
29 }
30
31 return moves;
32 }
33
34 getPotentialKingMoves(sq)
35 {
2d7194bd 36 // No castle:
a6abf094
BA
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;
26b8e4f7 45 const oppCol = V.GetOppCol(color);
0b7d99ec 46 for (let i=0; i<V.size.x; i++)
a6abf094 47 {
0b7d99ec 48 for (let j=0; j<V.size.y; j++)
a6abf094 49 {
0b7d99ec 50 if (this.board[i][j] != V.EMPTY && this.getColor(i,j) != oppCol)
a6abf094
BA
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())
0b7d99ec 78 moves = V.KeepCaptures(moves);
a6abf094
BA
79 return moves;
80 }
81
82 getAllValidMoves()
83 {
84 let moves = super.getAllValidMoves();
85 if (moves.some(m => { return m.vanish.length == 2; }))
0b7d99ec 86 moves = V.KeepCaptures(moves);
a6abf094
BA
87 return moves;
88 }
89
f6dbe8e3 90 underCheck(color)
a6abf094
BA
91 {
92 return false; //No notion of check
93 }
94
95 getCheckSquares(move)
96 {
97 return [];
98 }
99
388e4c40 100 // No variables update because no royal king + no castling
6752407b
BA
101 updateVariables(move) { }
102 unupdateVariables(move) { }
a6abf094
BA
103
104 checkGameEnd()
105 {
106 // No valid move: you win!
107 return this.turn == "w" ? "1-0" : "0-1";
108 }
109
2d7194bd
BA
110 static get VALUES()
111 {
112 // Experimental...
a6abf094
BA
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 }
2eef6db6
BA
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 {
8d61fc4a 136 let positions = range(8);
2eef6db6
BA
137
138 // Get random squares for bishops
8d61fc4a 139 let randIndex = 2 * random(4);
2eef6db6
BA
140 let bishop1Pos = positions[randIndex];
141 // The second bishop must be on a square of different color
8d61fc4a 142 let randIndex_tmp = 2 * random(4) + 1;
2eef6db6
BA
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
8d61fc4a 149 randIndex = random(6);
2eef6db6
BA
150 let knight1Pos = positions[randIndex];
151 positions.splice(randIndex, 1);
8d61fc4a 152 randIndex = random(5);
2eef6db6
BA
153 let knight2Pos = positions[randIndex];
154 positions.splice(randIndex, 1);
155
156 // Get random square for queen
8d61fc4a 157 randIndex = random(4);
2eef6db6
BA
158 let queenPos = positions[randIndex];
159 positions.splice(randIndex, 1);
160
161 // Random square for king (no castle)
8d61fc4a 162 randIndex = random(3);
2eef6db6
BA
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() +
2d7194bd 183 " w -"; //no en-passant
2eef6db6 184 }
a6abf094 185}