1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class BenedictRules
extends ChessRules
{
4 static get HasEnpassant() {
8 static get PawnSpecs() {
16 // TODO(?): some duplicated code in 2 next functions
17 getSlideNJumpMoves([x
, y
], steps
, oneStep
) {
19 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
20 const step
= steps
[loop
];
23 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
24 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
25 if (oneStep
) continue outerLoop
;
29 // No capture check: handled elsewhere (next method)
34 // Find possible captures from a square
35 // follow steps from x,y until something is met.
36 findCaptures([x
, y
]) {
37 const color
= this.getColor(x
, y
);
38 const piece
= this.getPiece(x
, y
);
42 ? [V
.QUEEN
,V
.KING
].includes(piece
)
43 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
54 const oneStep
= [V
.KNIGHT
,V
.PAWN
,V
.KING
].includes(piece
);
55 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
56 const step
= steps
[loop
];
59 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
60 if (oneStep
) continue outerLoop
;
66 this.getColor(i
, j
) == V
.GetOppCol(color
)
75 // TODO: appear/vanish description of a move is too verbose for Benedict.
76 // => Would need a new "flipped" array, to be passed in Game.vue...
77 getPotentialMovesFrom([x
, y
]) {
78 const color
= this.turn
;
79 const oppCol
= V
.GetOppCol(color
);
80 // Get all moves from x,y without captures:
81 let moves
= super.getPotentialMovesFrom([x
, y
]);
86 V
.PlayOnBoard(this.board
, m
);
87 // If castling, m.appear has 2 elements.
88 // In this case, consider the attacks of moving units only.
89 // (Sometimes the king or rook doesn't move).
90 for (let i
= 0; i
< m
.appear
.length
; i
++) {
91 const a
= m
.appear
[i
];
92 if (m
.vanish
[i
].x
!= a
.x
|| m
.vanish
[i
].y
!= a
.y
) {
93 const flipped
= this.findCaptures([a
.x
, a
.y
]);
94 flipped
.forEach(sq
=> {
95 const piece
= this.getPiece(sq
[0],sq
[1]);
96 const pipoA
= new PiPo({
102 const pipoV
= new PiPo({
108 newAppear
.push(pipoA
);
109 newVanish
.push(pipoV
);
113 Array
.prototype.push
.apply(m
.appear
, newAppear
);
114 Array
.prototype.push
.apply(m
.vanish
, newVanish
);
115 V
.UndoOnBoard(this.board
, m
);
120 // Moves cannot flip our king's color, so all are valid
125 // Since it's used just for the king, and there are no captures:
126 isAttacked(sq
, color
) {
130 // No notion of check here:
135 // Stop at the first move found
137 const color
= this.turn
;
138 for (let i
= 0; i
< V
.size
.x
; i
++) {
139 for (let j
= 0; j
< V
.size
.y
; j
++) {
140 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
141 if (this.getPotentialMovesFrom([i
, j
]).length
> 0) return true;
149 const color
= this.turn
;
150 // Did a king change color?
151 const kp
= this.kingPos
[color
];
152 if (this.getColor(kp
[0], kp
[1]) != color
)
153 return color
== "w" ? "0-1" : "1-0";
154 if (this.atLeastOneMove()) return "*";
160 // Just remove flips:
162 appear: [move.appear
[0]],
163 vanish: [move.vanish
[0]],
167 return super.getNotation(basicMove
);