Add Orda variant. Some advances in Dynamo draft. Display error message when loading...
[vchess.git] / client / src / variants / Dynamo.js
1 import { ChessRules } from "@/base_rules";
2
3 export class DynamoRules extends ChessRules {
4 canIplay(side, [x, y]) {
5 // Sometimes opponent's pieces can be moved directly
6 return true;
7 }
8
9 // NOTE: to push a piece out of the board, make it slide until our piece
10 // (doing the action, moving or not)
11 getPotentialMovesFrom([x, y]) {
12 const color = this.turn;
13 let moves = [];
14 if (this.getColor(x, y) != color) {
15 // Push or pull something: freely only if subTurn == 1
16 if (this.subTurn == 2) {
17 // I know that someone is pushing/pulling: find out who,
18 // and deduce my possible squares (or exit).
19 // TODO
20 } else {
21 // Look in every direction for a friendly pusher/puller.
22 // This means that the action is done without moving.
23 // TODO
24 }
25 } else {
26 // My piece: fill first with normal moves (if any),
27 // and add pushes/pulls (if any).
28 // TODO
29 }
30 }
31
32 getPPpath(m) {
33 let imgName = "";
34 if (m.vanish.length == 1) imgName = "empty";
35 else {
36 // Something is pushed or pull: count by how many squares
37 if (m.appear.length == 1)
38 // It just exit the board
39 imgName = "raus";
40 else {
41 const deltaX = Math.abs(m.appear[1].x - m.vanish[1].x);
42 const deltaY = Math.abs(m.appear[1].y - m.vanish[1].y);
43 if (deltaX == 0) imgName = "shift" + deltaY;
44 else if (deltaY == 0) imgName = "shift" + deltaX;
45 else
46 // Special knight push/pull: just print "P"
47 imgName = "pstep";
48 }
49 }
50 return "Dynamo/" + imgName;
51 }
52
53 isAttackedBySlideNJump([x, y], color, piece, steps, oneStep) {
54 for (let step of steps) {
55 let rx = x + step[0],
56 ry = y + step[1];
57 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
58 rx += step[0];
59 ry += step[1];
60 }
61 if (
62 V.OnBoard(rx, ry) &&
63 this.getPiece(rx, ry) == piece &&
64 this.getColor(rx, ry) == color
65 ) {
66 // Now step in the other direction: if end of the world, then attacked
67 rx = x - step[0];
68 ry = y - step[1];
69 while (V.OnBoard(rx, ry) && this.board[rx][ry] == V.EMPTY && !oneStep) {
70 rx -= step[0];
71 ry -= step[1];
72 }
73 if (!V.OnBoard(rx, ry)) return true;
74 }
75 }
76 return false;
77 }
78
79 isAttackedByPawn([x, y], color) {
80 const lastRank = (color == 'w' ? 0 : 7);
81 if (y != lastRank)
82 // The king can be pushed out by a pawn only on last rank
83 return false;
84 const pawnShift = (color == "w" ? 1 : -1);
85 for (let i of [-1, 1]) {
86 if (
87 y + i >= 0 &&
88 y + i < V.size.y &&
89 this.getPiece(x + pawnShift, y + i) == V.PAWN &&
90 this.getColor(x + pawnShift, y + i) == color
91 ) {
92 return true;
93 }
94 }
95 return false;
96 }
97 };