936c2590254cce5f1a764fe14a71e1909f098e14
[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 let p = V.KING;
28 // Normal move
29 if (this.board[x+shift][y] == V.EMPTY)
30 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:p}));
31 // Captures
32 if (y>0 && this.canTake([x,y], [x+shift,y-1]) && this.board[x+shift][y-1] != V.EMPTY)
33 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:p}));
34 if (y<sizeY-1 && this.canTake([x,y], [x+shift,y+1]) && this.board[x+shift][y+1] != V.EMPTY)
35 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:p}));
36 }
37
38 return moves;
39 }
40
41 getPotentialKingMoves(sq)
42 {
43 const V = VariantRules;
44 return this.getSlideNJumpMoves(sq,
45 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
46 }
47
48 // Stop at the first capture found (if any)
49 atLeastOneCapture()
50 {
51 const color = this.turn;
52 const oppCol = this.getOppCol(color);
53 const [sizeX,sizeY] = VariantRules.size;
54 for (let i=0; i<sizeX; i++)
55 {
56 for (let j=0; j<sizeY; j++)
57 {
58 if (this.board[i][j] != VariantRules.EMPTY && this.getColor(i,j) != oppCol)
59 {
60 const moves = this.getPotentialMovesFrom([i,j]);
61 if (moves.length > 0)
62 {
63 for (let k=0; k<moves.length; k++)
64 {
65 if (moves[k].vanish.length==2 && this.filterValid([moves[k]]).length > 0)
66 return true;
67 }
68 }
69 }
70 }
71 }
72 return false;
73 }
74
75 // Trim all non-capturing moves
76 static KeepCaptures(moves)
77 {
78 return moves.filter(m => { return m.vanish.length == 2; });
79 }
80
81 getPossibleMovesFrom(sq)
82 {
83 let moves = this.filterValid( this.getPotentialMovesFrom(sq) );
84 // This is called from interface: we need to know if a capture is possible
85 if (this.atLeastOneCapture())
86 moves = VariantRules.KeepCaptures(moves);
87 return moves;
88 }
89
90 getAllValidMoves()
91 {
92 let moves = super.getAllValidMoves();
93 if (moves.some(m => { return m.vanish.length == 2; }))
94 moves = VariantRules.KeepCaptures(moves);
95 return moves;
96 }
97
98 underCheck(move)
99 {
100 return false; //No notion of check
101 }
102
103 getCheckSquares(move)
104 {
105 return [];
106 }
107
108 // Unused:
109 updateVariables(move) { }
110 unupdateVariables(move) { }
111 parseFlags(flags) { }
112
113 getFlagsFen()
114 {
115 return "";
116 }
117
118 checkGameEnd()
119 {
120 // No valid move: you win!
121 return this.turn == "w" ? "1-0" : "0-1";
122 }
123
124 static get VALUES() { //experimental...
125 return {
126 'p': 1,
127 'r': 7,
128 'n': 3,
129 'b': 3,
130 'q': 5,
131 'k': 5
132 };
133 }
134
135 static get SEARCH_DEPTH() { return 4; }
136
137 evalPosition()
138 {
139 return - super.evalPosition(); //better with less material
140 }
141 }