Improve FEN length for Eightpieces variant
[vchess.git] / client / src / variants / Dynamo.js
CommitLineData
b866a62a
BA
1import { ChessRules, Move, PiPo } from "@/base_rules";
2
3// TODO: need FEN lastmove pour interdire défaire dernière poussée
4// --> check appear et vanish totally reversed.
5
6// TODO: pawn promotions by push (en + des promotions standard)
7// --> similar to Zen promotions.
0d5335de
BA
8
9export class DynamoRules extends ChessRules {
c7550017
BA
10 canIplay(side, [x, y]) {
11 // Sometimes opponent's pieces can be moved directly
12 return true;
13 }
14
c7550017
BA
15 getPPpath(m) {
16 let imgName = "";
17 if (m.vanish.length == 1) imgName = "empty";
18 else {
19 // Something is pushed or pull: count by how many squares
20 if (m.appear.length == 1)
21 // It just exit the board
22 imgName = "raus";
23 else {
24 const deltaX = Math.abs(m.appear[1].x - m.vanish[1].x);
25 const deltaY = Math.abs(m.appear[1].y - m.vanish[1].y);
b866a62a
BA
26 if (deltaX == 0) imgName = "shift_" + deltaY;
27 else if (deltaY == 0) imgName = "shift_" + deltaX;
c7550017
BA
28 else
29 // Special knight push/pull: just print "P"
30 imgName = "pstep";
31 }
32 }
33 return "Dynamo/" + imgName;
34 }
35
b866a62a
BA
36 getPactions(sq, by, color) {
37 const [x, y] = sq;
38 let moves = [];
39 let squares = {};
40// const lineAdd = (allowedPieces) = {
41// // attacking piece must be of the allowed types
42// };
43 if (!by) {
44 // Look in all directions for a "color" piece
45 for (let step of V.steps[V.KNIGHT]) {
46 const xx = x + step[0],
47 yy = y + step[1];
48 if (
49 V.OnBoard(xx, yy) &&
50 this.getPiece(xx, yy) == V.KNIGHT &&
51 this.getColor(xx, yy) == color
52 ) {
53 const px = x - step[0],
54 py = y - step[1];
55 if (V.OnBoard(px, py) && this.board[px][py] == V.EMPTY) {
56 const hash = "s" + px + py;
57 if (!squares[hash]) {
58 squares[hash] = true;
59 moves.push(this.getBasicMove([x, y], [px, py]));
60 }
61 } else {
62 const hash = "s" + xx + yy;
63 if (!squares[hash]) {
64 squares[hash] = true;
65 moves.push(
66 new Move({
67 start: { x: x, y: y },
68 end: { x: xx, y: yy },
69 appear: [],
70 vanish: [
71 new PiPo({
72 x: x,
73 y: y,
74 p: this.getPiece(x, y),
75 c: oppCol
76 })
77 ]
78 })
79 );
80 }
81 }
82 }
83 }
84 for (let step in V.steps[V.ROOK]) {
85 // color is enemy, so no pawn pushes: king, rook and queen
86 }
87 for (let step in V.steps[V.BISHOP]) {
88 // King, bishop, queen, and possibly pawns attacks
89 }
90 }
91// else {
92// // TODO: probably in a different function for now.
93// }
94 return moves;
95 }
96
97 // NOTE: to push a piece out of the board, make it slide until our piece
98 // (doing the action, moving or not)
99 getPotentialMovesFrom([x, y]) {
100 const color = this.turn;
101 const oppCol = V.GetOppCol(color);
102 if (this.getColor(x, y) != color) {
103 // Look in every direction for a friendly pusher/puller.
104 // This means that the action is done without moving.
105 return this.getPactions([x, y], null, color);
106 } else {
107 // Playing my pieces: do they attack an enemy?
108 // If yes ... TODO
109 //this.getPattacks(sq, [x, y]);
110 // Temporary:
111 return super.getPotentialMovesFrom([x, y]);
112 }
113 return []; //never reached
114 }
115
c7550017
BA
116 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
117 for (let step of steps) {
118 let rx = x + step[0],
119 ry = y + step[1];
120 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
121 rx += step[0];
122 ry += step[1];
123 }
124 if (
125 V.OnBoard(rx, ry) &&
126 this.getPiece(rx, ry) == piece &&
127 this.getColor(rx, ry) == color
128 ) {
129 // Now step in the other direction: if end of the world, then attacked
130 rx = x - step[0];
131 ry = y - step[1];
2c5d7b20
BA
132 while (
133 V.OnBoard(rx, ry) &&
134 this.board[rx][ry] == V.EMPTY &&
135 !oneStep
136 ) {
c7550017
BA
137 rx -= step[0];
138 ry -= step[1];
139 }
140 if (!V.OnBoard(rx, ry)) return true;
141 }
142 }
143 return false;
144 }
145
146 isAttackedByPawn([x, y], color) {
147 const lastRank = (color == 'w' ? 0 : 7);
148 if (y != lastRank)
149 // The king can be pushed out by a pawn only on last rank
150 return false;
151 const pawnShift = (color == "w" ? 1 : -1);
152 for (let i of [-1, 1]) {
153 if (
154 y + i >= 0 &&
155 y + i < V.size.y &&
156 this.getPiece(x + pawnShift, y + i) == V.PAWN &&
157 this.getColor(x + pawnShift, y + i) == color
158 ) {
159 return true;
160 }
161 }
162 return false;
163 }
0d5335de 164};