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