Some code cleaning + clarifying (TODO: work on variables names)
[vchess.git] / public / javascripts / variants / Loser.js
1 class LoserRules extends ChessRules
2 {
3 initVariables(fen)
4 {
5 const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
6 this.epSquares = [ epSq ];
7 }
8
9 setFlags(fen)
10 {
11 // No castling, hence no flags; but flags defined for compatibility
12 this.castleFlags = { "w":[false,false], "b":[false,false] };
13 }
14
15 getPotentialPawnMoves([x,y])
16 {
17 let moves = super.getPotentialPawnMoves([x,y]);
18
19 // Complete with promotion(s) into king, if possible
20 const color = this.turn;
21 const V = VariantRules;
22 const [sizeX,sizeY] = VariantRules.size;
23 const shift = (color == "w" ? -1 : 1);
24 const lastRank = (color == "w" ? 0 : sizeX-1);
25 if (x+shift == lastRank)
26 {
27 // Normal move
28 if (this.board[x+shift][y] == V.EMPTY)
29 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING}));
30 // Captures
31 if (y>0 && this.canTake([x,y], [x+shift,y-1])
32 && this.board[x+shift][y-1] != V.EMPTY)
33 {
34 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:V.KING}));
35 }
36 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1])
37 && this.board[x+shift][y+1] != V.EMPTY)
38 {
39 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:V.KING}));
40 }
41 }
42
43 return moves;
44 }
45
46 getPotentialKingMoves(sq)
47 {
48 const V = VariantRules;
49 return this.getSlideNJumpMoves(sq,
50 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
51 }
52
53 // Stop at the first capture found (if any)
54 atLeastOneCapture()
55 {
56 const color = this.turn;
57 const oppCol = this.getOppCol(color);
58 const [sizeX,sizeY] = VariantRules.size;
59 for (let i=0; i<sizeX; i++)
60 {
61 for (let j=0; j<sizeY; j++)
62 {
63 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
64 {
65 const moves = this.getPotentialMovesFrom([i,j]);
66 if (moves.length > 0)
67 {
68 for (let k=0; k<moves.length; k++)
69 {
70 if (moves[k].vanish.length==2 && this.filterValid([moves[k]]).length > 0)
71 return true;
72 }
73 }
74 }
75 }
76 }
77 return false;
78 }
79
80 // Trim all non-capturing moves
81 static KeepCaptures(moves)
82 {
83 return moves.filter(m => { return m.vanish.length == 2; });
84 }
85
86 getPossibleMovesFrom(sq)
87 {
88 let moves = this.filterValid( this.getPotentialMovesFrom(sq) );
89 // This is called from interface: we need to know if a capture is possible
90 if (this.atLeastOneCapture())
91 moves = VariantRules.KeepCaptures(moves);
92 return moves;
93 }
94
95 getAllValidMoves()
96 {
97 let moves = super.getAllValidMoves();
98 if (moves.some(m => { return m.vanish.length == 2; }))
99 moves = VariantRules.KeepCaptures(moves);
100 return moves;
101 }
102
103 underCheck(move)
104 {
105 return false; //No notion of check
106 }
107
108 getCheckSquares(move)
109 {
110 return [];
111 }
112
113 // Unused:
114 updateVariables(move) { }
115 unupdateVariables(move) { }
116 parseFlags(flags) { }
117
118 getFlagsFen()
119 {
120 return "-";
121 }
122
123 checkGameEnd()
124 {
125 // No valid move: you win!
126 return this.turn == "w" ? "1-0" : "0-1";
127 }
128
129 static get VALUES() { //experimental...
130 return {
131 'p': 1,
132 'r': 7,
133 'n': 3,
134 'b': 3,
135 'q': 5,
136 'k': 5
137 };
138 }
139
140 static get SEARCH_DEPTH() { return 4; }
141
142 evalPosition()
143 {
144 return - super.evalPosition(); //better with less material
145 }
146 }