Play computer move in webworker to not freeze interface
[vchess.git] / public / javascripts / variants / Antiking.js
1 class AntikingRules extends ChessRules
2 {
3 static getPpath(b)
4 {
5 return b[1]=='a' ? "Antiking/"+b : b;
6 }
7
8 static get ANTIKING() { return 'a'; }
9
10 static get PIECES()
11 {
12 return ChessRules.PIECES.concat([V.ANTIKING]);
13 }
14
15 setOtherVariables(fen)
16 {
17 super.setOtherVariables(fen);
18 this.antikingPos = {'w':[-1,-1], 'b':[-1,-1]};
19 const rows = V.ParseFen(fen).position.split("/");
20 for (let i=0; i<rows.length; i++)
21 {
22 let k = 0;
23 for (let j=0; j<rows[i].length; j++)
24 {
25 switch (rows[i].charAt(j))
26 {
27 case 'a':
28 this.antikingPos['b'] = [i,k];
29 break;
30 case 'A':
31 this.antikingPos['w'] = [i,k];
32 break;
33 default:
34 const num = parseInt(rows[i].charAt(j));
35 if (!isNaN(num))
36 k += (num-1);
37 }
38 k++;
39 }
40 }
41 }
42
43 canTake([x1,y1], [x2,y2])
44 {
45 const piece1 = this.getPiece(x1,y1);
46 const piece2 = this.getPiece(x2,y2);
47 const color1 = this.getColor(x1,y1);
48 const color2 = this.getColor(x2,y2);
49 return piece2 != "a" &&
50 ((piece1 != "a" && color1 != color2) || (piece1 == "a" && color1 == color2));
51 }
52
53 getPotentialMovesFrom([x,y])
54 {
55 switch (this.getPiece(x,y))
56 {
57 case V.ANTIKING:
58 return this.getPotentialAntikingMoves([x,y]);
59 default:
60 return super.getPotentialMovesFrom([x,y]);
61 }
62 }
63
64 getPotentialAntikingMoves(sq)
65 {
66 return this.getSlideNJumpMoves(sq,
67 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
68 }
69
70 isAttacked(sq, colors)
71 {
72 return (super.isAttacked(sq, colors) || this.isAttackedByAntiking(sq, colors));
73 }
74
75 isAttackedByKing([x,y], colors)
76 {
77 if (this.getPiece(x,y) == V.ANTIKING)
78 return false; //antiking is not attacked by king
79 return this.isAttackedBySlideNJump([x,y], colors, V.KING,
80 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
81 }
82
83 isAttackedByAntiking([x,y], colors)
84 {
85 if ([V.KING,V.ANTIKING].includes(this.getPiece(x,y)))
86 return false; //(anti)king is not attacked by antiking
87 return this.isAttackedBySlideNJump([x,y], colors, V.ANTIKING,
88 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
89 }
90
91 underCheck(move)
92 {
93 const c = this.turn;
94 const oppCol = this.getOppCol(c);
95 this.play(move)
96 let res = this.isAttacked(this.kingPos[c], [oppCol])
97 || !this.isAttacked(this.antikingPos[c], [oppCol]);
98 this.undo(move);
99 return res;
100 }
101
102 getCheckSquares(move)
103 {
104 let res = super.getCheckSquares(move);
105 this.play(move);
106 const c = this.turn;
107 if (!this.isAttacked(this.antikingPos[c], [this.getOppCol(c)]))
108 res.push(JSON.parse(JSON.stringify(this.antikingPos[c])));
109 this.undo(move);
110 return res;
111 }
112
113 updateVariables(move)
114 {
115 super.updateVariables(move);
116 const piece = this.getPiece(move.start.x,move.start.y);
117 const c = this.getColor(move.start.x,move.start.y);
118 // Update antiking position
119 if (piece == V.ANTIKING)
120 {
121 this.antikingPos[c][0] = move.appear[0].x;
122 this.antikingPos[c][1] = move.appear[0].y;
123 }
124 }
125
126 unupdateVariables(move)
127 {
128 super.unupdateVariables(move);
129 const c = this.getColor(move.start.x,move.start.y);
130 if (this.getPiece(move.start.x,move.start.y) == V.ANTIKING)
131 this.antikingPos[c] = [move.start.x, move.start.y];
132 }
133
134 checkGameEnd()
135 {
136 const color = this.turn;
137 const oppCol = this.getOppCol(color);
138 if (!this.isAttacked(this.kingPos[color], [oppCol])
139 && this.isAttacked(this.antikingPos[color], [oppCol]))
140 {
141 return "1/2";
142 }
143 return color == "w" ? "0-1" : "1-0";
144 }
145
146 static get VALUES() {
147 return Object.assign(
148 ChessRules.VALUES,
149 { 'a': 1000 }
150 );
151 }
152
153 static GenRandInitFen()
154 {
155 let pieces = { "w": new Array(8), "b": new Array(8) };
156 let antikingPos = { "w": -1, "b": -1 };
157 for (let c of ["w","b"])
158 {
159 let positions = _.range(8);
160
161 // Get random squares for bishops, but avoid corners; because,
162 // if an antiking blocks a cornered bishop, it can never be checkmated
163 let randIndex = 2 * _.random(1,3);
164 const bishop1Pos = positions[randIndex];
165 let randIndex_tmp = 2 * _.random(2) + 1;
166 const bishop2Pos = positions[randIndex_tmp];
167 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
168 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
169
170 randIndex = _.random(5);
171 const knight1Pos = positions[randIndex];
172 positions.splice(randIndex, 1);
173 randIndex = _.random(4);
174 const knight2Pos = positions[randIndex];
175 positions.splice(randIndex, 1);
176
177 randIndex = _.random(3);
178 const queenPos = positions[randIndex];
179 positions.splice(randIndex, 1);
180
181 const rook1Pos = positions[0];
182 const kingPos = positions[1];
183 const rook2Pos = positions[2];
184
185 // Random squares for antikings
186 antikingPos[c] = _.random(7);
187
188 pieces[c][rook1Pos] = 'r';
189 pieces[c][knight1Pos] = 'n';
190 pieces[c][bishop1Pos] = 'b';
191 pieces[c][queenPos] = 'q';
192 pieces[c][kingPos] = 'k';
193 pieces[c][bishop2Pos] = 'b';
194 pieces[c][knight2Pos] = 'n';
195 pieces[c][rook2Pos] = 'r';
196 }
197 const ranks23_black = "pppppppp/" + (antikingPos["w"]>0?antikingPos["w"]:"")
198 + "A" + (antikingPos["w"]<7?7-antikingPos["w"]:"");
199 const ranks23_white = (antikingPos["b"]>0?antikingPos["b"]:"") + "a"
200 + (antikingPos["b"]<7?7-antikingPos["b"]:"") + "/PPPPPPPP";
201 return pieces["b"].join("") + "/" + ranks23_black +
202 "/8/8/" +
203 ranks23_white + "/" + pieces["w"].join("").toUpperCase() +
204 " w 1111";
205 }
206 }
207
208 const VariantRules = AntikingRules;