Fix magnetic promotions, attempt to fix Alice chess
[vchess.git] / public / javascripts / variants / Alice.js
CommitLineData
270968d6 1class AliceRules extends ChessRules
5bfb0956 2{
0cd8f2bd 3 static get ALICE_PIECES()
5bfb0956 4 {
270968d6
BA
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 };
5bfb0956 24 }
a3eb4cc5 25
0cd8f2bd
BA
26 static getPpath(b)
27 {
270968d6 28 return (Object.keys(this.ALICE_PIECES).includes(b[1]) ? "Alice/" : "") + b;
0cd8f2bd
BA
29 }
30
270968d6 31 getBoardOfPiece([x,y])
0cd8f2bd 32 {
270968d6
BA
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, "");
0cd8f2bd
BA
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);
270968d6
BA
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];
0cd8f2bd
BA
48 }
49 }
270968d6
BA
50 return sideBoard;
51 }
0cd8f2bd 52
270968d6
BA
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]);
0cd8f2bd 60
270968d6
BA
61 // Search valid moves on sideBoard
62 let saveBoard = this.board;
63 this.board = sideBoard;
64 let moves = super.getPotentialMovesFrom([x,y]);
0cd8f2bd
BA
65 this.board = saveBoard;
66
67 // Finally filter impossible moves
270968d6
BA
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 }
f6cc7faf
BA
79 // If the move is computed on board1, m.appear change for Alice pieces.
80 if (mirrorSide==1)
81 {
82 m.appear.forEach(psq => { //forEach: castling taken into account
270968d6 83 psq.p = VariantRules.ALICE_CODES[psq.p]; //goto board2
f6cc7faf
BA
84 });
85 }
86 else //move on board2: mark vanishing piece as Alice
87 m.vanish[0].p = VariantRules.ALICE_CODES[m.vanish[0].p]
270968d6
BA
88 return true;
89 });
0cd8f2bd
BA
90 }
91
92 underCheck(move)
93 {
0cd8f2bd
BA
94 const color = this.turn;
95 this.play(move);
270968d6
BA
96 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
97 let saveBoard = this.board;
98 this.board = sideBoard;
0cd8f2bd 99 let res = this.isAttacked(this.kingPos[color], this.getOppCol(color));
270968d6 100 this.board = saveBoard;
0cd8f2bd
BA
101 this.undo(move);
102 return res;
103 }
104
270968d6 105 getCheckSquares(move)
0cd8f2bd 106 {
270968d6
BA
107 this.play(move);
108 const color = this.turn; //opponent
109 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
110 let saveBoard = this.board;
111 this.board = sideBoard;
112 let res = this.isAttacked(this.kingPos[color], this.getOppCol(color))
113 ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ]
114 : [ ];
115 this.board = saveBoard;
116 this.undo(move);
117 return res;
0cd8f2bd 118 }
270968d6
BA
119
120 getNotation(move)
0cd8f2bd 121 {
270968d6
BA
122 if (move.appear.length == 2 && move.appear[0].p == VariantRules.KING)
123 {
124 if (move.end.y < move.start.y)
125 return "0-0-0";
126 else
127 return "0-0";
128 }
129
130 const finalSquare =
131 String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
132 const piece = this.getPiece(move.start.x, move.start.y);
133
134 // Piece or pawn movement
135 let notation = piece.toUpperCase() +
136 (move.vanish.length > move.appear.length ? "x" : "") + finalSquare;
137 if (['s','p'].includes(piece) && !['s','p'].includes(move.appear[0].p))
138 {
139 // Promotion
140 notation += "=" + move.appear[0].p.toUpperCase();
141 }
142 return notation;
0cd8f2bd
BA
143 }
144
145 checkGameEnd()
146 {
147 const color = this.turn;
270968d6
BA
148 let sideBoard = this.getBoardOfPiece(this.kingPos[color]);
149 let saveBoard = this.board;
150 this.board = sideBoard;
151 let res = "*";
0cd8f2bd 152 if (!this.isAttacked(this.kingPos[color], this.getOppCol(color)))
270968d6
BA
153 res = "1/2";
154 else
155 res = (color == "w" ? "0-1" : "1-0");
156 this.board = saveBoard;
157 return res;
0cd8f2bd 158 }
5bfb0956 159}