f0d4f2ba64bdc57574655c358e8d97596be49d0c
1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export const VariantRules
= class BenedictRules
extends ChessRules
{
4 static get HasEnpassant() {
8 // TODO(?): some duplicated code in 2 next functions
9 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
11 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
12 const step
= steps
[loop
];
15 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
16 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
17 if (oneStep
) continue outerLoop
;
21 // No capture check: handled elsewhere (next method)
26 // Find possible captures from a square
27 // follow steps from x,y until something is met.
28 findCaptures([x
, y
]) {
29 const color
= this.getColor(x
, y
);
30 const piece
= this.getPiece(x
, y
);
34 ? [V
.QUEEN
,V
.KING
].includes(piece
)
35 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
46 const oneStep
= [V
.KNIGHT
,V
.PAWN
,V
.KING
].includes(piece
);
47 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
48 const step
= steps
[loop
];
51 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
52 if (oneStep
) continue outerLoop
;
58 this.getColor(i
, j
) == V
.GetOppCol(color
)
67 getPotentialPawnMoves([x
, y
]) {
68 const color
= this.getColor(x
, y
);
70 const sizeY
= V
.size
.y
;
71 const shift
= color
== "w" ? -1 : 1;
72 const startRank
= color
== "w" ? sizeY
- 2 : 1;
73 const firstRank
= color
== "w" ? sizeY
- 1 : 0;
74 const lastRank
= color
== "w" ? 0 : sizeY
- 1;
76 if (x
+ shift
!= lastRank
) {
78 if (this.board
[x
+ shift
][y
] == V
.EMPTY
) {
79 moves
.push(this.getBasicMove([x
, y
], [x
+ shift
, y
]));
81 [startRank
, firstRank
].includes(x
) &&
82 this.board
[x
+ 2 * shift
][y
] == V
.EMPTY
85 moves
.push(this.getBasicMove([x
, y
], [x
+ 2 * shift
, y
]));
91 let promotionPieces
= [V
.ROOK
, V
.KNIGHT
, V
.BISHOP
, V
.QUEEN
];
92 promotionPieces
.forEach(p
=> {
94 if (this.board
[x
+ shift
][y
] == V
.EMPTY
)
96 this.getBasicMove([x
, y
], [x
+ shift
, y
], { c: color
, p: p
})
101 // No en passant here
106 // No "under check" verifications:
107 getCastleMoves([x
, y
]) {
108 const c
= this.getColor(x
, y
);
109 if (x
!= (c
== "w" ? V
.size
.x
- 1 : 0) || y
!= this.INIT_COL_KING
[c
])
110 return []; //x isn't first rank, or king has moved (shortcut)
113 const oppCol
= V
.GetOppCol(c
);
117 const finalSquares
= [
119 [V
.size
.y
- 2, V
.size
.y
- 3]
124 castleSide
++ //large, then small
126 if (this.castleFlags
[c
][castleSide
] >= 8) continue;
127 // If this code is reached, rooks and king are on initial position
129 const rookPos
= this.castleFlags
[c
][castleSide
];
130 if (this.getColor(x
, rookPos
) != c
)
131 // Rook is here but changed color
134 // Nothing on the path of the king ?
135 const finDist
= finalSquares
[castleSide
][0] - y
;
136 let step
= finDist
/ Math
.max(1, Math
.abs(finDist
));
137 for (let i
= y
; i
!= finalSquares
[castleSide
][0]; i
+= step
) {
139 this.board
[x
][i
] != V
.EMPTY
&&
140 // NOTE: next check is enough, because of chessboard constraints
141 (this.getColor(x
, i
) != c
||
142 ![V
.KING
, V
.ROOK
].includes(this.getPiece(x
, i
)))
144 continue castlingCheck
;
148 // Nothing on the path to the rook?
149 step
= castleSide
== 0 ? -1 : 1;
150 for (i
= y
+ step
; i
!= rookPos
; i
+= step
) {
151 if (this.board
[x
][i
] != V
.EMPTY
) continue castlingCheck
;
154 // Nothing on final squares, except maybe king and castling rook?
155 for (i
= 0; i
< 2; i
++) {
157 this.board
[x
][finalSquares
[castleSide
][i
]] != V
.EMPTY
&&
158 this.getPiece(x
, finalSquares
[castleSide
][i
]) != V
.KING
&&
159 finalSquares
[castleSide
][i
] != rookPos
161 continue castlingCheck
;
165 // If this code is reached, castle is valid
169 new PiPo({ x: x
, y: finalSquares
[castleSide
][0], p: V
.KING
, c: c
}),
170 new PiPo({ x: x
, y: finalSquares
[castleSide
][1], p: V
.ROOK
, c: c
})
173 new PiPo({ x: x
, y: y
, p: V
.KING
, c: c
}),
174 new PiPo({ x: x
, y: rookPos
, p: V
.ROOK
, c: c
})
177 Math
.abs(y
- rookPos
) <= 2
178 ? { x: x
, y: rookPos
}
179 : { x: x
, y: y
+ 2 * (castleSide
== 0 ? -1 : 1) }
187 // TODO: appear/vanish description of a move is too verbose for Benedict.
188 // => Would need a new "flipped" array, to be passed in Game.vue...
189 getPotentialMovesFrom([x
, y
]) {
190 const color
= this.turn
;
191 const oppCol
= V
.GetOppCol(color
);
192 // Get all moves from x,y without captures:
193 let moves
= super.getPotentialMovesFrom([x
, y
]);
198 V
.PlayOnBoard(this.board
, m
);
199 // If castling, m.appear has 2 elements.
200 // In this case, consider the attacks of moving units only.
201 // (Sometimes the king or rook doesn't move).
202 for (let i
= 0; i
< m
.appear
.length
; i
++) {
203 const a
= m
.appear
[i
];
204 if (m
.vanish
[i
].x
!= a
.x
|| m
.vanish
[i
].y
!= a
.y
) {
205 const flipped
= this.findCaptures([a
.x
, a
.y
]);
206 flipped
.forEach(sq
=> {
207 const piece
= this.getPiece(sq
[0],sq
[1]);
208 const pipoA
= new PiPo({
214 const pipoV
= new PiPo({
220 newAppear
.push(pipoA
);
221 newVanish
.push(pipoV
);
225 Array
.prototype.push
.apply(m
.appear
, newAppear
);
226 Array
.prototype.push
.apply(m
.vanish
, newVanish
);
227 V
.UndoOnBoard(this.board
, m
);
232 // Moves cannot flip our king's color, so all are valid
237 // No notion of check here:
242 // Stop at the first move found
244 const color
= this.turn
;
245 const oppCol
= V
.GetOppCol(color
);
246 for (let i
= 0; i
< V
.size
.x
; i
++) {
247 for (let j
= 0; j
< V
.size
.y
; j
++) {
248 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) != oppCol
) {
249 const moves
= this.getPotentialMovesFrom([i
, j
]);
250 if (moves
.length
> 0)
259 const color
= this.turn
;
260 // Did a king change color?
261 const kp
= this.kingPos
[color
];
262 if (this.getColor(kp
[0], kp
[1]) != color
)
263 return color
== "w" ? "0-1" : "1-0";
264 if (this.atLeastOneMove())
271 // Just remove flips:
273 appear: [move.appear
[0]],
274 vanish: [move.vanish
[0]],
278 return super.getNotation(basicMove
);