Slight improve in bug report instructions
[vchess.git] / public / javascripts / variants / Antiking.js
CommitLineData
204e289b 1class AntikingRules extends ChessRules
7d6b0773
BA
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'; }
46302e64
BA
10
11 initVariables(fen)
12 {
13 super.initVariables(fen);
204e289b
BA
14 this.antikingPos = {'w':[-1,-1], 'b':[-1,-1]};
15 const position = fen.split(" ")[0].split("/");
16 for (let i=0; i<position.length; i++)
17 {
6037f1d8
BA
18 let k = 0;
19 for (let j=0; j<position[i].length; j++)
204e289b
BA
20 {
21 switch (position[i].charAt(j))
22 {
23 case 'a':
6037f1d8 24 this.antikingPos['b'] = [i,k];
204e289b
BA
25 break;
26 case 'A':
6037f1d8 27 this.antikingPos['w'] = [i,k];
204e289b
BA
28 break;
29 default:
30 let num = parseInt(position[i].charAt(j));
31 if (!isNaN(num))
6037f1d8 32 k += (num-1);
204e289b 33 }
6037f1d8 34 k++;
204e289b
BA
35 }
36 }
46302e64 37 }
7d6b0773 38
204e289b 39 canTake([x1,y1], [x2,y2])
7d6b0773 40 {
204e289b
BA
41 const piece1 = this.getPiece(x1,y1);
42 const piece2 = this.getPiece(x2,y2);
43 const color1 = this.getColor(x1,y1);
44 const color2 = this.getColor(x2,y2);
45 return !["a","A"].includes(piece2) &&
46 ((piece1 != "a" && color1 != color2) || (piece1 == "a" && color1 == color2));
7d6b0773
BA
47 }
48
49 getPotentialMovesFrom([x,y])
50 {
7d6b0773
BA
51 switch (this.getPiece(x,y))
52 {
53 case VariantRules.ANTIKING:
204e289b 54 return this.getPotentialAntikingMoves([x,y]);
7d6b0773 55 default:
204e289b 56 return super.getPotentialMovesFrom([x,y]);
7d6b0773
BA
57 }
58 }
59
204e289b 60 getPotentialAntikingMoves(sq)
7d6b0773 61 {
204e289b 62 return this.getSlideNJumpMoves(sq, VariantRules.steps[VariantRules.QUEEN], "oneStep");
7d6b0773
BA
63 }
64
46302e64 65 isAttacked(sq, colors)
7d6b0773 66 {
46302e64 67 return (super.isAttacked(sq, colors) || this.isAttackedByAntiking(sq, colors));
7d6b0773
BA
68 }
69
6037f1d8
BA
70 isAttackedByKing([x,y], colors)
71 {
72 if (this.getPiece(x,y) == VariantRules.ANTIKING)
73 return false; //antiking is not attacked by king
74 return this.isAttackedBySlideNJump([x,y], colors,
75 VariantRules.KING, VariantRules.steps[VariantRules.QUEEN], "oneStep");
76 }
77
204e289b 78 isAttackedByAntiking([x,y], colors)
7d6b0773 79 {
204e289b
BA
80 if (this.getPiece(x,y) == VariantRules.KING)
81 return false; //king is not attacked by antiking
6037f1d8 82 return this.isAttackedBySlideNJump([x,y], colors,
204e289b 83 VariantRules.ANTIKING, VariantRules.steps[VariantRules.QUEEN], "oneStep");
7d6b0773
BA
84 }
85
46302e64 86 underCheck(move)
7d6b0773 87 {
46302e64 88 const c = this.turn;
204e289b
BA
89 const oppCol = this.getOppCol(c);
90 this.play(move)
91 let res = this.isAttacked(this.kingPos[c], oppCol)
92 || !this.isAttacked(this.antikingPos[c], oppCol);
7d6b0773
BA
93 this.undo(move);
94 return res;
95 }
96
46302e64 97 getCheckSquares(move)
7d6b0773 98 {
204e289b 99 let res = super.getCheckSquares(move);
7d6b0773 100 this.play(move);
46302e64 101 const c = this.turn;
204e289b
BA
102 if (!this.isAttacked(this.antikingPos[c], this.getOppCol(c)))
103 res.push(JSON.parse(JSON.stringify(this.antikingPos[c])));
7d6b0773
BA
104 this.undo(move);
105 return res;
106 }
107
7d6b0773
BA
108 updateVariables(move)
109 {
204e289b
BA
110 super.updateVariables(move);
111 const piece = this.getPiece(move.start.x,move.start.y);
112 const c = this.getColor(move.start.x,move.start.y);
113 // Update antiking position
114 if (piece == VariantRules.ANTIKING)
115 {
116 this.antikingPos[c][0] = move.appear[0].x;
117 this.antikingPos[c][1] = move.appear[0].y;
118 }
7d6b0773
BA
119 }
120
121 unupdateVariables(move)
122 {
204e289b
BA
123 super.unupdateVariables(move);
124 const c = this.getColor(move.start.x,move.start.y);
125 if (this.getPiece(move.start.x,move.start.y) == VariantRules.ANTIKING)
126 this.antikingPos[c] = [move.start.x, move.start.y];
7d6b0773
BA
127 }
128
204e289b 129 checkGameEnd()
7d6b0773 130 {
204e289b
BA
131 const color = this.turn;
132 const oppCol = this.getOppCol(color);
133 if (!this.isAttacked(this.kingPos[color], oppCol)
134 && this.isAttacked(this.antikingPos[color], oppCol))
135 {
7d6b0773 136 return "1/2";
204e289b 137 }
7d6b0773
BA
138 return color == "w" ? "0-1" : "1-0";
139 }
140
46302e64 141 // Pieces values (TODO: use Object.assign() + ChessRules.VALUES ?)
7d6b0773
BA
142 static get VALUES() {
143 return {
144 'p': 1,
145 'r': 5,
146 'n': 3,
147 'b': 3,
148 'q': 9,
149 'k': 1000,
150 'a': 1000
151 };
152 }
153
154 static GenRandInitFen()
155 {
46302e64 156 let randFen = ChessRules.GenRandInitFen();
204e289b
BA
157 // Black side
158 let antikingPos = _.random(7);
159 let ranks23 = "pppppppp/" + (antikingPos>0?antikingPos:"") + "A" + (antikingPos<7?7-antikingPos:"");
160 randFen = randFen.replace("pppppppp/8", ranks23);
161 // White side
162 antikingPos = _.random(7);
163 ranks23 = (antikingPos>0?antikingPos:"") + "a" + (antikingPos<7?7-antikingPos:"") + "/PPPPPPPP";
164 randFen = randFen.replace("8/PPPPPPPP", ranks23);
46302e64 165 return randFen;
7d6b0773
BA
166 }
167}