1 import { ChessRules
, PiPo
, Move
} from "@/base_rules";
3 export class BenedictRules
extends ChessRules
{
5 static get HasEnpassant() {
9 static get PawnSpecs() {
21 // Find possible captures from a square
22 // follow steps from x,y until something is met.
23 findCaptures([x
, y
]) {
24 const color
= this.getColor(x
, y
);
25 const piece
= this.getPiece(x
, y
);
29 ? [V
.QUEEN
,V
.KING
].includes(piece
)
30 ? V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])
41 const oneStep
= [V
.KNIGHT
,V
.PAWN
,V
.KING
].includes(piece
);
42 outerLoop: for (let loop
= 0; loop
< steps
.length
; loop
++) {
43 const step
= steps
[loop
];
46 while (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
) {
47 if (oneStep
) continue outerLoop
;
53 this.getColor(i
, j
) == V
.GetOppCol(color
)
62 // TODO: appear/vanish description of a move is too verbose for Benedict.
63 // => Would need a new "flipped" array, to be passed in Game.vue...
64 getPotentialMovesFrom([x
, y
]) {
65 const color
= this.turn
;
66 const oppCol
= V
.GetOppCol(color
);
67 // Get all moves from x,y without captures:
68 let moves
= super.getPotentialMovesFrom([x
, y
]);
73 V
.PlayOnBoard(this.board
, m
);
74 // If castling, m.appear has 2 elements.
75 // In this case, consider the attacks of moving units only.
76 // (Sometimes the king or rook doesn't move).
77 for (let i
= 0; i
< m
.appear
.length
; i
++) {
78 const a
= m
.appear
[i
];
79 if (m
.vanish
[i
].x
!= a
.x
|| m
.vanish
[i
].y
!= a
.y
) {
80 const flipped
= this.findCaptures([a
.x
, a
.y
]);
81 flipped
.forEach(sq
=> {
82 const piece
= this.getPiece(sq
[0],sq
[1]);
83 const pipoA
= new PiPo({
89 const pipoV
= new PiPo({
95 newAppear
.push(pipoA
);
96 newVanish
.push(pipoV
);
100 Array
.prototype.push
.apply(m
.appear
, newAppear
);
101 Array
.prototype.push
.apply(m
.vanish
, newVanish
);
102 V
.UndoOnBoard(this.board
, m
);
107 // Moves cannot flip our king's color, so all are valid
112 // Since it's used just for the king, and there are no captures:
113 isAttacked(sq
, color
) {
117 // No notion of check here:
122 // Stop at the first move found
124 const color
= this.turn
;
125 for (let i
= 0; i
< V
.size
.x
; i
++) {
126 for (let j
= 0; j
< V
.size
.y
; j
++) {
127 if (this.board
[i
][j
] != V
.EMPTY
&& this.getColor(i
, j
) == color
) {
128 if (this.getPotentialMovesFrom([i
, j
]).length
> 0) return true;
136 const color
= this.turn
;
137 // Did a king change color?
138 const kp
= this.kingPos
[color
];
139 if (this.getColor(kp
[0], kp
[1]) != color
)
140 return color
== "w" ? "0-1" : "1-0";
141 if (this.atLeastOneMove()) return "*";
147 // Just remove flips:
149 appear: [move.appear
[0]],
150 vanish: [move.vanish
[0]],
154 return super.getNotation(basicMove
);