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