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