Alice rules implemented, but unfinished and with bugs...
[vchess.git] / public / javascripts / variants / Alice.js
1 class AliceRules extends ChessRules
2 {
3 static get ALICE_PIECES()
4 {
5 return {
6 's': 'p',
7 't': 'q',
8 'u': 'r',
9 'c': 'b',
10 'o': 'n',
11 'l': 'k',
12 };
13 }
14 static get ALICE_CODES()
15 {
16 return {
17 'p': 's',
18 'q': 't',
19 'r': 'u',
20 'b': 'c',
21 'n': 'o',
22 'k': 'l',
23 };
24 }
25
26 static getPpath(b)
27 {
28 return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
29 }
30
31 getBoardOfPiece([x,y])
32 {
33 const V = VariantRules;
34 // Build board where the piece is
35 const mirrorSide = (Object.keys(V.ALICE_CODES).includes(this.getPiece(x,y)) ? 1 : 2);
36 // Build corresponding board from complete board
37 const [sizeX,sizeY] = V.size;
38 let sideBoard = doubleArray(sizeX, sizeY, "");
39 for (let i=0; i<sizeX; i++)
40 {
41 for (let j=0; j<sizeY; j++)
42 {
43 const piece = this.getPiece(i,j);
44 if (mirrorSide==1 && Object.keys(V.ALICE_CODES).includes(piece))
45 sideBoard[i][j] = this.board[i][j];
46 else if (mirrorSide==2 && Object.keys(V.ALICE_PIECES).includes(piece))
47 sideBoard[i][j] = this.getColor(i,j) + V.ALICE_PIECES[piece];
48 }
49 }
50 return sideBoard;
51 }
52
53 // TODO: castle & enPassant https://www.chessvariants.com/other.dir/alice.html
54 // TODO: enPassant seulement si l'on est du même coté que le coté de départ du pion adverse
55 // (en passant en sortant du monde... : il faut donc ajouter des coups non trouvés)
56 // castle: check that all destination squares are not occupied
57 getPotentialMovesFrom([x,y])
58 {
59 let sideBoard = this.getBoardOfPiece([x,y]);
60
61 // Search valid moves on sideBoard
62 let saveBoard = this.board;
63 this.board = sideBoard;
64 let moves = super.getPotentialMovesFrom([x,y]);
65 this.board = saveBoard;
66
67 // Finally filter impossible moves
68 const mirrorSide = (Object.keys(VariantRules.ALICE_CODES).includes(this.getPiece(x,y)) ? 1 : 2);
69 return moves.filter(m => {
70 if (this.board[m.end.x][m.end.y] != VariantRules.EMPTY)
71 {
72 const piece = this.getPiece(m.end.x,m.end.y);
73 if ((mirrorSide==1 && Object.keys(VariantRules.ALICE_PIECES).includes(piece))
74 || (mirrorSide==2 && Object.keys(VariantRules.ALICE_CODES).includes(piece)))
75 {
76 return false;
77 }
78 }
79 m.appear.forEach(psq => {
80 if (Object.keys(VariantRules.ALICE_CODES).includes(psq.p))
81 psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2
82 else
83 psq.p = VariantRules.ALICE_PIECES[psq.p]; //goto board1
84 });
85 return true;
86 });
87 }
88
89 underCheck(move)
90 {
91 const color = this.turn;
92 this.play(move);
93 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
94 let saveBoard = this.board;
95 this.board = sideBoard;
96 let res = this.isAttacked(this.kingPos[color], this.getOppCol(color));
97 this.board = saveBoard;
98 this.undo(move);
99 return res;
100 }
101
102 getCheckSquares(move)
103 {
104 this.play(move);
105 const color = this.turn; //opponent
106 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
107 let saveBoard = this.board;
108 this.board = sideBoard;
109 let res = this.isAttacked(this.kingPos[color], this.getOppCol(color))
110 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
111 : [ ];
112 this.board = saveBoard;
113 this.undo(move);
114 return res;
115 }
116
117 getNotation(move)
118 {
119 if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
120 {
121 if (move.end.y < move.start.y)
122 return "0-0-0";
123 else
124 return "0-0";
125 }
126
127 const finalSquare =
128 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
129 const piece = this.getPiece(move.start.x, move.start.y);
130
131 // Piece or pawn movement
132 let notation = piece.toUpperCase() +
133 (move.vanish.length > move.appear.length ? "x" : "") + finalSquare;
134 if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
135 {
136 // Promotion
137 notation += "=" + move.appear[0].p.toUpperCase();
138 }
139 return notation;
140 }
141
142 checkGameEnd()
143 {
144 const color = this.turn;
145 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
146 let saveBoard = this.board;
147 this.board = sideBoard;
148 let res = "*";
149 if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
150 res = "1/2";
151 else
152 res = (color == "w" ? "0-1" : "1-0");
153 this.board = saveBoard;
154 return res;
155 }
156 }