1 import { ChessRules
, PiPo
} from "@/base_rules";
3 export class Atomic1Rules
extends ChessRules
{
5 getPotentialMovesFrom([x
, y
]) {
6 let moves
= super.getPotentialMovesFrom([x
, y
]);
8 if (this.getPiece(x
, y
) == V
.PAWN
) {
9 // Promotions by captures can be reduced to only one deterministic
10 // move (because of the explosion).
11 moves
= moves
.filter(m
=> {
13 m
.vanish
.length
== 1 ||
14 [V
.PAWN
, V
.QUEEN
].includes(m
.appear
[0].p
)
21 // NOTE: if vanish.length==2 and appear.length==2, this is castle
22 if (m
.vanish
.length
> 1 && m
.appear
.length
<= 1) {
23 // Explosion! (TODO?: drop moves which explode our king here)
34 for (let step
of steps
) {
35 let x
= m
.end
.x
+ step
[0];
36 let y
= m
.end
.y
+ step
[1];
39 this.board
[x
][y
] != V
.EMPTY
&&
40 this.getPiece(x
, y
) != V
.PAWN
44 p: this.getPiece(x
, y
),
45 c: this.getColor(x
, y
),
52 m
.end
= { x: m
.appear
[0].x
, y: m
.appear
[0].y
};
53 m
.appear
.pop(); //Nothin appears in this case
60 getPotentialKingMoves([x
, y
]) {
61 // King cannot capture:
63 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
64 for (let step
of steps
) {
65 const i
= x
+ step
[0];
66 const j
= y
+ step
[1];
67 if (V
.OnBoard(i
, j
) && this.board
[i
][j
] == V
.EMPTY
)
68 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
70 return moves
.concat(this.getCastleMoves([x
, y
]));
73 isAttacked(sq
, color
) {
75 this.getPiece(sq
[0], sq
[1]) == V
.KING
&&
76 this.isAttackedByKing(sq
, color
)
78 // A king next to the enemy king is immune to attacks
82 this.isAttackedByPawn(sq
, color
) ||
83 this.isAttackedByRook(sq
, color
) ||
84 this.isAttackedByKnight(sq
, color
) ||
85 this.isAttackedByBishop(sq
, color
) ||
86 this.isAttackedByQueen(sq
, color
)
87 // No "attackedByKing": it cannot take
93 // NOTE: (harmless) condition on movesCount for Atomic2
94 if (move.appear
.length
== 0 && this.movesCount
>= 2) {
96 const firstRank
= { w: 7, b: 0 };
97 for (let c
of ["w", "b"]) {
98 // Did we explode king of color c ? (TODO: remove move earlier)
100 Math
.abs(this.kingPos
[c
][0] - move.end
.x
) <= 1 &&
101 Math
.abs(this.kingPos
[c
][1] - move.end
.y
) <= 1
103 this.kingPos
[c
] = [-1, -1];
104 this.castleFlags
[c
] = [8, 8];
107 // Now check if init rook(s) exploded
108 if (Math
.abs(move.end
.x
- firstRank
[c
]) <= 1) {
109 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][0]) <= 1)
110 this.castleFlags
[c
][0] = 8;
111 if (Math
.abs(move.end
.y
- this.castleFlags
[c
][1]) <= 1)
112 this.castleFlags
[c
][1] = 8;
120 super.postUndo(move);
122 const oppCol
= V
.GetOppCol(c
);
123 // NOTE: condition on movesCount for Atomic2
125 this.movesCount
>= 1 &&
126 [this.kingPos
[c
][0], this.kingPos
[oppCol
][0]].some(e
=> e
< 0)
128 // There is a chance that last move blowed some king away..
129 for (let psq
of move.vanish
) {
131 this.kingPos
[psq
.c
== c
? c : oppCol
] = [psq
.x
, psq
.y
];
137 const oppCol
= V
.GetOppCol(color
);
139 // If our king disappeared, move is not valid
140 if (this.kingPos
[color
][0] < 0) res
= true;
141 // If opponent king disappeared, move is valid
142 else if (this.kingPos
[oppCol
][0] < 0) res
= false;
143 // Otherwise, if we remain under check, move is not valid
144 else res
= this.isAttacked(this.kingPos
[color
], oppCol
);
149 const color
= this.turn
;
152 this.kingPos
[color
][0] >= 0 && //king might have exploded
153 this.isAttacked(this.kingPos
[color
], V
.GetOppCol(color
))
155 res
= [JSON
.parse(JSON
.stringify(this.kingPos
[color
]))];
161 const color
= this.turn
;
162 const kp
= this.kingPos
[color
];
165 return color
== "w" ? "0-1" : "1-0";
166 if (this.atLeastOneMove()) return "*";
167 if (!this.isAttacked(kp
, V
.GetOppCol(color
))) return "1/2";
168 return color
== "w" ? "0-1" : "1-0"; //checkmate