Commit | Line | Data |
---|---|---|
7d6b0773 BA |
1 | class AntikingRules |
2 | { | |
3 | // Path to pieces | |
4 | static getPpath(b) | |
5 | { | |
6 | return b[1]=='a' ? "Antiking/"+b : b; | |
7 | } | |
8 | ||
9 | static get ANTIKING() { return 'a'; } | |
10 | ||
11 | // TODO: more subtle than that, require changing args: wp, bk and not just colors | |
12 | canTake(color1, color2) | |
13 | { | |
14 | return color1 != color2; | |
15 | } | |
16 | ||
17 | getPotentialMovesFrom([x,y]) | |
18 | { | |
19 | let c = this.getColor(x,y); | |
20 | switch (this.getPiece(x,y)) | |
21 | { | |
22 | case VariantRules.ANTIKING: | |
23 | return this.getPotentialAntikingMoves(x,y,c); | |
24 | default: | |
25 | return super.getPotentielMovesFrom([x,y]); | |
26 | } | |
27 | } | |
28 | ||
29 | getPotentialAntikingMoves(x, y, c) | |
30 | { | |
31 | // TODO | |
32 | } | |
33 | ||
34 | // TODO: need to re-think some logic, since antikings capture same color | |
35 | ||
36 | isAttacked(sq, color) | |
37 | { | |
38 | return (this.isAttackedByPawn(sq, color) | |
39 | || this.isAttackedByRook(sq, color) | |
40 | || this.isAttackedByKnight(sq, color) | |
41 | || this.isAttackedByBishop(sq, color) | |
42 | || this.isAttackedByQueen(sq, color) | |
43 | || this.isAttackedByKing(sq, color)); //... | |
44 | } | |
45 | ||
46 | isAttackedByAntiking(sq, color) | |
47 | { | |
48 | // TODO | |
49 | } | |
50 | ||
51 | underCheck(move, c) | |
52 | { | |
53 | this.play(move); | |
54 | let res = this.isAttacked(this.kingPos[c], this.getOppCol(c)); | |
55 | // TODO: also check that antiking is still in check | |
56 | this.undo(move); | |
57 | return res; | |
58 | } | |
59 | ||
60 | getCheckSquares(move, c) | |
61 | { | |
62 | this.play(move); | |
63 | // TODO | |
64 | let res = this.isAttacked(this.kingPos[c], this.getOppCol(c)) | |
65 | ? [ JSON.parse(JSON.stringify(this.kingPos[c])) ] //need to duplicate! | |
66 | : [ ]; | |
67 | this.undo(move); | |
68 | return res; | |
69 | } | |
70 | ||
71 | // Apply a move on board | |
72 | static PlayOnBoard(board, move) | |
73 | { | |
74 | for (let psq of move.vanish) | |
75 | board[psq.x][psq.y] = VariantRules.EMPTY; | |
76 | for (let psq of move.appear) | |
77 | board[psq.x][psq.y] = psq.c + psq.p; | |
78 | } | |
79 | // Un-apply the played move | |
80 | static UndoOnBoard(board, move) | |
81 | { | |
82 | for (let psq of move.appear) | |
83 | board[psq.x][psq.y] = VariantRules.EMPTY; | |
84 | for (let psq of move.vanish) | |
85 | board[psq.x][psq.y] = psq.c + psq.p; | |
86 | } | |
87 | ||
88 | // TODO: need antikingPos as well | |
89 | updateVariables(move) | |
90 | { | |
91 | // ... | |
92 | } | |
93 | ||
94 | unupdateVariables(move) | |
95 | { | |
96 | // TODO | |
97 | } | |
98 | ||
99 | checkGameEnd(color) | |
100 | { | |
101 | // TODO | |
102 | if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))) | |
103 | return "1/2"; | |
104 | return color == "w" ? "0-1" : "1-0"; | |
105 | } | |
106 | ||
107 | // Pieces values | |
108 | static get VALUES() { | |
109 | return { | |
110 | 'p': 1, | |
111 | 'r': 5, | |
112 | 'n': 3, | |
113 | 'b': 3, | |
114 | 'q': 9, | |
115 | 'k': 1000, | |
116 | 'a': 1000 | |
117 | }; | |
118 | } | |
119 | ||
120 | static GenRandInitFen() | |
121 | { | |
122 | // TODO: no need all code, just add an antiking at rondom on 3rd ranks | |
123 | let pieces = [new Array(8), new Array(8)]; | |
124 | // Shuffle pieces on first and last rank | |
125 | for (let c = 0; c <= 1; c++) | |
126 | { | |
127 | let positions = _.range(8); | |
128 | ||
129 | // Get random squares for bishops | |
130 | let randIndex = 2 * _.random(3); | |
131 | let bishop1Pos = positions[randIndex]; | |
132 | // The second bishop must be on a square of different color | |
133 | let randIndex_tmp = 2 * _.random(3) + 1; | |
134 | let bishop2Pos = positions[randIndex_tmp]; | |
135 | // Remove chosen squares | |
136 | positions.splice(Math.max(randIndex,randIndex_tmp), 1); | |
137 | positions.splice(Math.min(randIndex,randIndex_tmp), 1); | |
138 | ||
139 | // Get random squares for knights | |
140 | randIndex = _.random(5); | |
141 | let knight1Pos = positions[randIndex]; | |
142 | positions.splice(randIndex, 1); | |
143 | randIndex = _.random(4); | |
144 | let knight2Pos = positions[randIndex]; | |
145 | positions.splice(randIndex, 1); | |
146 | ||
147 | // Get random square for queen | |
148 | randIndex = _.random(3); | |
149 | let queenPos = positions[randIndex]; | |
150 | positions.splice(randIndex, 1); | |
151 | ||
152 | // Rooks and king positions are now fixed, because of the ordering rook-king-rook | |
153 | let rook1Pos = positions[0]; | |
154 | let kingPos = positions[1]; | |
155 | let rook2Pos = positions[2]; | |
156 | ||
157 | // Finally put the shuffled pieces in the board array | |
158 | pieces[c][rook1Pos] = 'r'; | |
159 | pieces[c][knight1Pos] = 'n'; | |
160 | pieces[c][bishop1Pos] = 'b'; | |
161 | pieces[c][queenPos] = 'q'; | |
162 | pieces[c][kingPos] = 'k'; | |
163 | pieces[c][bishop2Pos] = 'b'; | |
164 | pieces[c][knight2Pos] = 'n'; | |
165 | pieces[c][rook2Pos] = 'r'; | |
166 | } | |
167 | let fen = pieces[0].join("") + | |
168 | "/pppppppp/8/8/8/8/PPPPPPPP/" + | |
169 | pieces[1].join("").toUpperCase() + | |
170 | " 1111"; //add flags | |
171 | return fen; | |
172 | } | |
173 | } |