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