New variant idea
[xogo.git] / variants / Convert / class.js
CommitLineData
3e72fcf5
BA
1import ChessRules from "/base_rules.js";
2import PiPo from "/utils/PiPo.js";
3import Move from "/utils/Move.js";
4
5export default class ConvertRules extends ChessRules {
6
3e72fcf5
BA
7 static get Options() {
8 return {
9 select: C.Options.select,
b98feb3f 10 styles: ["cylinder", "dark", "recycle", "teleport"]
3e72fcf5
BA
11 };
12 }
13
14 get hasEnpassant() {
15 return false;
16 }
17
3232aba3
BA
18 setOtherVariables(fenParsed) {
19 super.setOtherVariables(fenParsed);
3e72fcf5 20 // Stack of "last move" only for intermediate chaining
b8950be5 21 this.lastMoveEnd = [];
3e72fcf5
BA
22 }
23
24 genRandInitBaseFen() {
25 const baseFen = super.genRandInitBaseFen();
26 return {
27 fen: baseFen.fen.replace("pppppppp/8", "8/pppppppp")
28 .replace("8/PPPPPPPP", "PPPPPPPP/8"),
29 o: baseFen.o
30 };
31 }
32
33 getBasicMove([sx, sy], [ex, ey], tr) {
34 const L = this.lastMoveEnd.length;
b98feb3f 35 const piece = (L >= 1 ? this.lastMoveEnd[L-1].p : null);
3e72fcf5
BA
36 if (this.board[ex][ey] == "") {
37 if (piece && !tr)
b98feb3f 38 tr = {c: this.turn, p: piece};
3e72fcf5
BA
39 let mv = super.getBasicMove([sx, sy], [ex, ey], tr);
40 if (piece)
b98feb3f 41 mv.vanish.pop(); //end of a chain: initial piece remains
3e72fcf5
BA
42 return mv;
43 }
44 // Capture: initial, or inside a chain
b98feb3f
BA
45 const initPiece = (piece || this.getPiece(sx, sy)),
46 destPiece = this.getPiece(ex, ey);
3e72fcf5
BA
47 let mv = new Move({
48 start: {x: sx, y: sy},
49 end: {x: ex, y: ey},
50 appear: [
51 new PiPo({
52 x: ex,
53 y: ey,
b98feb3f 54 c: this.turn,
3e72fcf5
BA
55 p: (!!tr ? tr.p : initPiece)
56 })
57 ],
58 vanish: [
59 new PiPo({
60 x: ex,
61 y: ey,
b98feb3f
BA
62 c: C.GetOppTurn(this.turn),
63 p: destPiece
3e72fcf5
BA
64 })
65 ]
66 });
67 if (!piece) {
68 // Initial capture
69 mv.vanish.unshift(
70 new PiPo({
71 x: sx,
72 y: sy,
b98feb3f 73 c: this.turn,
3e72fcf5
BA
74 p: initPiece
75 })
76 );
77 }
b98feb3f 78 mv.converted = destPiece; //easier (no need to detect it)
24a6312d 79// mv.drag = {c: this.turn, p: initPiece}; //TODO: doesn't work
3e72fcf5
BA
80 return mv;
81 }
82
b8950be5
BA
83 getPiece(x, y) {
84 const L = this.lastMoveEnd.length;
85 if (L >= 1 && this.lastMoveEnd[L-1].x == x && this.lastMoveEnd[L-1].y == y)
86 return this.lastMoveEnd[L-1].p;
87 return super.getPiece(x, y);
88 }
89
90 getPotentialMovesFrom([x, y], color) {
91 const L = this.lastMoveEnd.length;
92 if (
93 L >= 1 &&
94 (x != this.lastMoveEnd[L-1].x || y != this.lastMoveEnd[L-1].y)
95 ) {
96 // A capture was played: wrong square
97 return [];
98 }
99 return super.getPotentialMovesFrom([x, y], color);
100 }
101
102 underAttack_aux([x, y], color, explored) {
103 if (explored.some(sq => sq[0] == x && sq[1] == y))
104 // Start of an infinite loop: exit
105 return false;
106 explored.push([x, y]);
107 if (super.underAttack([x, y], [color]))
108 return true;
109 // Maybe indirect "chaining" attack:
110 const myColor = this.turn;
111 let res = false;
112 let toCheck = []; //check all but king (no need)
113 // Pawns:
114 const shiftToPawn = (myColor == 'w' ? -1 : 1);
115 for (let yShift of [-1, 1]) {
116 const [i, j] = [x + shiftToPawn, y + yShift];
117 if (
118 this.onBoard(i, j) &&
119 this.board[i][j] != "" &&
120 // NOTE: no need to check color (no enemy pawn can take directly)
121 this.getPiece(i, j) == 'p'
122 ) {
123 toCheck.push([i, j]);
124 }
125 }
126 // Knights:
127 this.pieces()['n'].both[0].steps.forEach(s => {
128 const [i, j] = [x + s[0], y + s[1]];
129 if (
130 this.onBoard(i, j) &&
131 this.board[i][j] != "" &&
132 this.getPiece(i, j) == 'n'
133 ) {
134 toCheck.push([i, j]);
135 }
136 });
137 // Sliders:
138 this.pieces()['q'].both[0].steps.forEach(s => {
139 let [i, j] = [x + s[0], y + s[1]];
140 while (this.onBoard(i, j) && this.board[i][j] == "") {
141 i += s[0];
142 j += s[1];
143 }
144 if (!this.onBoard(i, j))
145 return;
146 const piece = this.getPiece(i, j);
147 if (
148 piece == 'q' ||
149 (piece == 'r' && (s[0] == 0 || s[1] == 0)) ||
150 (piece == 'b' && (s[0] != 0 && s[1] != 0))
151 ) {
152 toCheck.push([i, j]);
153 }
154 });
155 for (let ij of toCheck) {
156 if (this.underAttack_aux(ij, color, explored))
157 return true;
158 }
159 return false;
160 }
161
b98feb3f 162 underAttack([x, y], [color]) {
b8950be5
BA
163 let explored = [];
164 return this.underAttack_aux([x, y], color, explored);
165 }
166
167 filterValid(moves) {
168 // No "checks" (except to forbid castle)
169 return moves;
170 }
171
172 isLastMove(move) {
b98feb3f 173 return !move.converted;
b8950be5
BA
174 }
175
176 postPlay(move) {
177 super.postPlay(move);
b98feb3f 178 if (!!move.converted) {
b8950be5
BA
179 this.lastMoveEnd.push({
180 x: move.end.x,
181 y: move.end.y,
b98feb3f 182 p: move.converted
b8950be5
BA
183 });
184 }
b98feb3f
BA
185 else
186 this.lastMoveEnd = [];
b8950be5
BA
187 }
188
3e72fcf5 189};