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 getPotentialRookMoves(sq
) {
107 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.ROOK
]);
110 getPotentialKnightMoves(sq
) {
111 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.KNIGHT
], "oneStep");
114 getPotentialBishopMoves(sq
) {
115 return this.getSlideNJumpMoves(sq
, V
.steps
[V
.BISHOP
]);
118 getPotentialQueenMoves(sq
) {
119 return this.getSlideNJumpMoves(
121 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
125 getPotentialKingMoves(sq
) {
126 // Initialize with normal (non-capturing) moves
127 let noCaptures
= this.getSlideNJumpMoves(
129 V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]),
132 return noCaptures
.concat(this.getCastleMoves(sq
));
135 // TODO: appear/vanish description of a move is too verbose for Benedict.
136 // => Would need a new "flipped" array, to be passed in Game.vue...
137 getPotentialMovesFrom([x
, y
]) {
138 const color
= this.turn
;
139 const oppCol
= V
.GetOppCol(color
);
140 // Get all moves from x,y without captures:
141 let moves
= super.getPotentialMovesFrom([x
, y
]);
146 V
.PlayOnBoard(this.board
, m
);
147 // If castling, m.appear has 2 elements:
148 m
.appear
.forEach(a
=> {
149 const flipped
= this.findCaptures([a
.x
, a
.y
]);
150 flipped
.forEach(sq
=> {
151 const piece
= this.getPiece(sq
[0],sq
[1]);
152 const pipoA
= new PiPo({
158 const pipoV
= new PiPo({
164 newAppear
.push(pipoA
);
165 newVanish
.push(pipoV
);
168 Array
.prototype.push
.apply(m
.appear
, newAppear
);
169 Array
.prototype.push
.apply(m
.vanish
, newVanish
);
170 V
.UndoOnBoard(this.board
, m
);
175 // Moves cannot flip our king's color, so all are valid
180 // No notion of check here:
185 // Stop at the first move found
187 const color
= this.turn
;
188 const oppCol
= V
.GetOppCol(color
);
189 for (let i
= 0; i
< V
.size
.x
; i
++) {
190 for (let j
= 0; j
< V
.size
.y
; j
++) {
191 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) != oppCol
) {
192 const moves
= this.getPotentialMovesFrom([i
, j
]);
193 if (moves
.length
> 0)
202 const color
= this.turn
;
203 // Did a king change color?
204 const kp
= this.kingPos
[color
];
205 if (this.getColor(kp
[0], kp
[1]) != color
)
206 return color
== "w" ? "0-1" : "1-0";
207 if (this.atLeastOneMove())