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