1 import ChessRules
from "/base_rules.js";
3 export default class RefusalRules
extends ChessRules
{
7 select: C
.Options
.select
,
11 variable: "refuseany",
24 genRandInitFen(seed
) {
25 return super.genRandInitFen(seed
).slice(0, -1) + ',"lastmove":"null"}';
30 super.getFen().slice(0, -1) + ',"lastmove":"' +
31 JSON
.stringify(this.lastMove
) + '"}');
34 setOtherVariables(fenParsed
) {
35 super.setOtherVariables(fenParsed
);
36 this.lastMove
= JSON
.parse(fenParsed
.lastmove
);
38 // Fill with empty values to avoid checking lastMove != null
40 start: {x: -1, y: -1}, end: {x: -1, y: -1}, vanish: [{c: ''}]
46 if (super.canIplay(x
, y
))
48 // Check if playing last move, reversed:
49 const lm
= this.lastMove
;
50 return (!lm
.noRef
&& x
== lm
.end
.x
&& y
== lm
.end
.y
);
53 getPotentialMovesFrom([x
, y
]) {
54 const moveColor
= this.getColor(x
, y
);
55 if (moveColor
!= this.turn
) {
56 let revLm
= JSON
.parse(JSON
.stringify(this.lastMove
));
57 [revLm
.appear
, revLm
.vanish
] = [revLm
.vanish
, revLm
.appear
];
58 [revLm
.start
, revLm
.end
] = [revLm
.end
, revLm
.start
];
59 if (!this.options
["refuseany"]) {
60 // After refusing this move, can my opponent play a different move?
61 this.playOnBoard(revLm
);
63 outerLoop: for (let i
=0; i
<this.size
.x
; i
++) {
64 for (let j
=0; j
<this.size
.y
; j
++) {
65 if (this.getColor(i
, j
) == moveColor
) {
66 const potentialOppMoves
= super.getPotentialMovesFrom([i
, j
]);
68 super.filterValid(potentialOppMoves
, moveColor
).length
;
74 this.undoOnBoard(revLm
);
78 // Also reverse segments in Cylinder mode:
79 if (this.options
["cylinder"])
80 revLm
.segments
= revLm
.segments
.map(seg
=> [seg
[1], seg
[0]]);
82 delete revLm
["segments"];
84 revLm
.noRef
= true; //cannot refuse a refusal move :)
87 return super.getPotentialMovesFrom([x
, y
]);
92 return super.getEpSquare(move);
97 const color
= this.turn
;
98 const lm
= this.lastMove
;
99 let rMoves
= moves
.filter(m
=> {
101 !lm
.refusal
|| //it's my first move attempt on this turn
102 m
.start
.x
!= lm
.end
.x
|| m
.start
.y
!= lm
.end
.y
||
103 m
.end
.x
!= lm
.start
.x
|| m
.end
.y
!= lm
.start
.y
||
104 // Doing the same move again: maybe pawn promotion?
105 (m
.vanish
[0].p
== 'p' && m
.appear
[0].p
!= lm
.appear
[0].p
)
108 return super.filterValid(rMoves
);
113 // My previous move was already refused?
114 move.noRef
= this.lastMove
.vanish
[0].c
== this.turn
;
118 this.lastMove
= move;
119 super.postPlay(move);
123 if (!this.lastMove
.noRef
)
125 return super.atLeastOneMove();