Fix Loser chess
[vchess.git] / public / javascripts / variants / Loser.js
CommitLineData
a6abf094
BA
1class LoserRules extends ChessRules
2{
3 initVariables(fen)
4 {
a6abf094
BA
5 const epSq = this.moves.length > 0 ? this.getEpSquare(this.lastMove) : undefined;
6 this.epSquares = [ epSq ];
7 }
8
1970e049
BA
9 setFlags(fen)
10 {
11 // No castling, hence no flags; but flags defined for compatibility
12 this.castleFlags = "0000";
13 }
a6abf094
BA
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
6752407b
BA
108 // Unused:
109 updateVariables(move) { }
110 unupdateVariables(move) { }
111 parseFlags(flags) { }
a6abf094
BA
112
113 checkGameEnd()
114 {
115 // No valid move: you win!
116 return this.turn == "w" ? "1-0" : "0-1";
117 }
118
119 static get VALUES() { //experimental...
120 return {
121 'p': 1,
122 'r': 7,
123 'n': 3,
124 'b': 3,
125 'q': 5,
126 'k': 5
127 };
128 }
129
130 static get SEARCH_DEPTH() { return 4; }
131
132 evalPosition()
133 {
134 return - super.evalPosition(); //better with less material
135 }
136}