- let m = [this.whiteMove, move];
- for (let i of [0, 1]) {
- if (!!m[i].illegal) {
- // Either an anticipated capture of something which didn't move
- // (or not to the right square), or a push through blocus.
- if (
- (
- // Push attempt
- m[i].start.y == m[i].end.y &&
- (m[1-i].start.x != m[i].end.x || m[1-i].start.y != m[i].end.y)
- )
- ||
- (
- // Capture attempt
- Math.abs(m[i].start.y - m[i].end.y) == 1 &&
- (m[1-i].end.x != m[i].end.x || m[1-i].end.y != m[i].end.y)
- )
- ) {
- // Just discard the move, and add a penalty point
- this.penaltyFlags[m[i].vanish[0].c]++;
- m[i] = null;
- }
- }
+ let m1 = this.whiteMove;
+ let m2 = move;
+ const movingLikeCapture = (m) => {
+ const shift = (m.vanish[0].c == 'w' ? -1 : 1);
+ return (
+ m.start.x + shift == m.end.x &&
+ Math.abs(m.end.y - m.start.y) == 1
+ );
+ };
+ const isPossible = (m, other) => {
+ return (
+ (
+ m.vanish[0].p == V.KNIGHT &&
+ (m.vanish.length == 1 || m.vanish[1].c != m.vanish[0].c)
+ )
+ ||
+ (
+ // Promotion attempt
+ m.end.x == (m.vanish[0].c == "w" ? 0 : V.size.x - 1) &&
+ other.vanish.length == 2 &&
+ other.vanish[1].p == V.KNIGHT &&
+ other.vanish[1].c == m.vanish[0].c
+ )
+ ||
+ (
+ // Moving attempt
+ !movingLikeCapture(m) &&
+ other.start.x == m.end.x &&
+ other.start.y == m.end.y
+ )
+ ||
+ (
+ // Capture attempt
+ movingLikeCapture(m) &&
+ other.end.x == m.end.x &&
+ other.end.y == m.end.y
+ )
+ );
+ };
+ if (!!m1.illegal && !isPossible(m1, m2)) {
+ // Either an anticipated capture of something which didn't move
+ // (or not to the right square), or a push through blocus.
+ // ==> Just discard the move, and add a penalty point
+ this.penaltyFlags[m1.vanish[0].c]++;
+ m1.isNull = true;