8a4981f71062075bc58fba36210fa1416be1e1e3
1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
3 import Move
from "/utils/Move.js";
5 export default class AvalancheRules
extends ChessRules
{
9 select: C
.Options
.select
,
40 {promotion: o
.init
? false : this.promotion
},
45 setOtherVariables(fenParsed
) {
46 super.setOtherVariables(fenParsed
);
47 this.promotion
= (fenParsed
.promotion
== '1');
48 this.subTurn
= 1 - (this.promotion
? 1 : 0);
52 const myLastRank
= (this.turn
== 'w' ? 0 : this.size
.x
- 1);
55 coords
.x
!= myLastRank
||
56 this.getPiece(coords
.x
, coords
.y
) != 'p'
61 this.pawnPromotions
.forEach(pr
=> {
64 vanish: [new PiPo({x: coords
.x
, y: coords
.y
, c: this.turn
, p: 'p'})],
65 appear: [new PiPo({x: coords
.x
, y: coords
.y
, c: this.turn
, p: pr
})]
69 super.showChoices(moves
);
73 const pieceColor
= this.getColor(x
, y
);
75 this.playerColor
== this.turn
&&
77 (this.subTurn
<= 1 && pieceColor
== this.playerColor
) ||
80 pieceColor
!= this.playerColor
&&
81 this.getPiece(x
, y
) == 'p'
87 getPotentialMovesFrom([x
, y
]) {
88 if (this.subTurn
== 0)
90 if (this.subTurn
== 1)
92 return super.getPotentialMovesFrom([x
, y
]);
93 // subTurn == 2: only allowed to push an opponent's pawn (if possible)
94 const oppPawnShift
= (this.turn
== 'w' ? 1 : -1);
96 this.onBoard(x
+ oppPawnShift
, y
) &&
97 this.board
[x
+ oppPawnShift
][y
] == ""
99 return [this.getBasicMove([x
, y
], [x
+ oppPawnShift
, y
])];
105 if (this.subTurn
!= 1)
106 return moves
; //self-checks by pawns are allowed
107 return super.filterValid(moves
);
110 atLeastOnePawnPush(color
) {
111 const pawnShift
= (color
== 'w' ? -1 : 1);
112 for (let i
= 0; i
< 8; i
++) {
113 for (let j
= 0; j
< 8; j
++) {
115 this.board
[i
][j
] != "" &&
116 this.getColor(i
, j
) == color
&&
117 this.getPiece(i
, j
) == 'p' &&
118 this.board
[i
+ pawnShift
][j
] == ""
127 tryChangeTurn(move) {
128 const color
= this.turn
;
129 const oppCol
= C
.GetOppTurn(color
);
130 const incrementTurn
= () => {
131 if (this.options
["balanced"] && this.movesCount
== 0) {
132 // No pawn push on move 1:
137 move.end
.x
== (oppCol
== 'w' ? 0 : this.size
.x
- 1) &&
138 move.vanish
[0].p
== 'p'
140 if (this.subTurn
== 0) {
142 if (!this.atLeastOneMove(color
)) {
143 move.result
= "1/2"; //avoid re-computation
148 if (this.subTurn
== 2) {
149 this.subTurn
= (this.promotion
? 0 : 1);
152 // subTurn == 1, usual case
153 const kingCapture
= this.searchKingPos(oppCol
).length
== 0;
155 move.result
= (color
== 'w' ? "1-0" : "0-1");
156 if (kingCapture
|| !this.atLeastOnePawnPush(oppCol
)) {
157 this.subTurn
= (this.promotion
? 0 : 1);
160 // A pawn push is possible: usual case
164 if (incrementTurn()) {
170 atLeastOneMove(color
) {
171 if (this.subTurn
== 0)
173 return super.atLeastOneMove(color
);