Save current state (unfinished, untested)
[vchess.git] / public / javascripts / variants / Extinction.js
CommitLineData
a6abf094
BA
1class ExtinctionRules extends ChessRules
2{
3 initVariables(fen)
4 {
5 super.initVariables(fen);
a6abf094
BA
6 this.material =
7 {
8 "w":
9 {
10 [V.KING]: 1,
11 [V.QUEEN]: 1,
12 [V.ROOK]: 2,
13 [V.KNIGHT]: 2,
14 [V.BISHOP]: 2,
15 [V.PAWN]: 8
16 },
17 "b":
18 {
19 [V.KING]: 1,
20 [V.QUEEN]: 1,
21 [V.ROOK]: 2,
22 [V.KNIGHT]: 2,
23 [V.BISHOP]: 2,
24 [V.PAWN]: 8
25 }
26 };
27 }
28
1221ac47
BA
29 getPotentialPawnMoves([x,y])
30 {
31 let moves = super.getPotentialPawnMoves([x,y]);
32 // Add potential promotions into king
33 const color = this.turn;
1221ac47 34 const shift = (color == "w" ? -1 : 1);
0b7d99ec 35 const lastRank = (color == "w" ? 0 : V.size.x-1);
1221ac47
BA
36
37 if (x+shift == lastRank)
38 {
39 // Normal move
40 if (this.board[x+shift][y] == V.EMPTY)
41 moves.push(this.getBasicMove([x,y], [x+shift,y], {c:color,p:V.KING}));
42 // Captures
0b7d99ec
BA
43 if (y>0 && this.board[x+shift][y-1] != V.EMPTY
44 && this.canTake([x,y], [x+shift,y-1]))
1221ac47
BA
45 {
46 moves.push(this.getBasicMove([x,y], [x+shift,y-1], {c:color,p:V.KING}));
47 }
0b7d99ec
BA
48 if (y<V.size.y-1 && this.board[x+shift][y+1] != V.EMPTY
49 && this.canTake([x,y], [x+shift,y+1]))
1221ac47
BA
50 {
51 moves.push(this.getBasicMove([x,y], [x+shift,y+1], {c:color,p:V.KING}));
52 }
53 }
54
55 return moves;
56 }
57
a6abf094
BA
58 // TODO: verify this assertion
59 atLeastOneMove()
60 {
61 return true; //always at least one possible move
62 }
63
64 underCheck(move)
65 {
66 return false; //there is no check
67 }
68
69 getCheckSquares(move)
70 {
71 return [];
72 }
73
74 updateVariables(move)
75 {
76 super.updateVariables(move);
1221ac47
BA
77 // Treat the promotion case: (not the capture part)
78 if (move.appear[0].p != move.vanish[0].p)
79 {
80 this.material[move.appear[0].c][move.appear[0].p]++;
0b7d99ec 81 this.material[move.appear[0].c][V.PAWN]--;
1221ac47
BA
82 }
83 if (move.vanish.length==2 && move.appear.length==1) //capture
a6abf094
BA
84 this.material[move.vanish[1].c][move.vanish[1].p]--;
85 }
86
87 unupdateVariables(move)
88 {
89 super.unupdateVariables(move);
1221ac47
BA
90 if (move.appear[0].p != move.vanish[0].p)
91 {
92 this.material[move.appear[0].c][move.appear[0].p]--;
0b7d99ec 93 this.material[move.appear[0].c][V.PAWN]++;
1221ac47 94 }
a6abf094
BA
95 if (move.vanish.length==2 && move.appear.length==1)
96 this.material[move.vanish[1].c][move.vanish[1].p]++;
97 }
98
99 checkGameOver()
100 {
101 if (this.checkRepetition())
102 return "1/2";
103
104 if (this.atLeastOneMove()) // game not over?
105 {
106 const color = this.turn;
107 if (Object.keys(this.material[color]).some(
108 p => { return this.material[color][p] == 0; }))
109 {
110 return this.checkGameEnd();
111 }
112 return "*";
113 }
114
1221ac47
BA
115 return this.checkGameEnd(); //NOTE: currently unreachable...
116 }
117
118 checkGameEnd()
119 {
120 return this.turn == "w" ? "0-1" : "1-0";
a6abf094
BA
121 }
122
a6abf094
BA
123 evalPosition()
124 {
1221ac47
BA
125 const color = this.turn;
126 if (Object.keys(this.material[color]).some(
127 p => { return this.material[color][p] == 0; }))
128 {
92342261 129 // Very negative (resp. positive) if white (reps. black) pieces set is incomplete
0b7d99ec 130 return (color=="w"?-1:1) * V.INFINITY;
1221ac47 131 }
a6abf094
BA
132 return super.evalPosition();
133 }
134}