12
\$\begingroup\$

Background and Rules

There is a variant of chess called atomic chess, which follows essentially the same rules as normal chess, except that pieces explodes other pieces around them when captured. In addition to checkmate, you can also win the game by blowing up the king.

When a piece captures another piece, all adjacent pieces (even diagonally adjacent) are destroyed, along with the pieces that were originally part of the capturing.

An exception to this rule are pawns. Pawns can be destroyed by directly capturing them, but they cannot be destroyed by the explosion of a nearby capture. Capturing with a pawn or capturing a pawn with another piece (even a pawn) will still result in an explosion.

A special case is the en passant capture, which allows a pawn that is pushed 3 ranks from its original position to capture an adjacent pawn immediately after that pawn has moved two squares in one move from its original position. In this case, the explosion is centered around where the capture would have been if the captured pawn had only moved one square instead of two.

For example, consider the following position (N represents a knight, capital letters represent white pieces, lowercase represent black pieces, ~ represents an empty space):

White to move

rnbqkb~r
pppppppp
~~~~~~~~
~~~N~~~~
~~~~n~~~
~~~~~~~~
PPPPPPPP
R~BQKBNR

Picture of the board (white to move):

If white then goes to take the pawn on c7 (the black pawn, 3rd from the left) with their knight on d5, both the white knight and the black pawn are destroyed, along with the b8 knight, the c8 bishop, and the queen. Notice how the b7 pawn and the d7 pawn are still alive, even though they were right next to the capture. Recall that they cannot be destroyed by the explosion of a nearby capture.

The resulting position after the capture is as follows:

Black to move

r~~~kb~r
pp~ppppp
~~~~~~~~
~~~~~~~~
~~~~n~~~
~~~~~~~~
PPPPPPPP
R~BQKBNR

Picture of the board (black to move):

With those rules out of the way, we can now talk about legal and illegal moves.

Legal and Illegal Moves

Just like how a piece covering a check (pinned piece) cannot move (you cannot make a move that results in your king being in check), similar things also exist in the atomic chess variant.

Because atomic chess follows all normal chess rules (but with a few differences, more details below), many illegal moves in normal chess also carry over into atomic chess. For example, moving a piece out of a pin is also illegal in atomic chess.

An illegal move unique to atomic chess is that you cannot capture around any piece that is pinned, because that results in your king being in check. Here's an example:

White to move

rnb~k~nr
pppppppp
~~~~~~~~
q~~~B~~~
~~~b~~~~
~~N~PN~~
PPP~~PPP
R~~QKB~R

Picture of the board (white to move):

Even though a pawn, a bishop, a knight, and a queen are all attacking the black bishop on d4, it cannot be taken because the resulting the explosion would also destroy the white knight on c3, which would expose the white king to a check by the black queen.

A legal move in atomic chess that is illegal in regular chess is moving the king next the opponent's king. The reasoning behind allowing this is that taking the opponent's king with your king would blow up both kings, so technically the opponent's king would not be threatening your king at all, which means it is safe to move your king next to the opponent's king.

That also leads to another illegal move: taking any piece with a king. This is because when a piece captures another piece, the capturing piece is also destroyed. Just like it's illegal to make a move such that the king is in check, it's also illegal to make the king suicide by blowing up itself. This also means that a king cannot capture an opponent king.

Another illegal move is taking a piece that is adjacent to both kings. Overall, any move that results in both kings being blown up in the same move is illegal. Taking a piece adjacent to both kings will result in exactly that. For example, take a look at the following position:

White to move

~~~~~~~~
~~kb~~~~
~~K~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~R~~~~

Picture of the board (white to move):

In this position, the white rook cannot take the black bishop because the resulting explosion would destroy both kings.

This also has the side effect that a king can move directly into check if it is adjacent to the opponent king. This is because the piece giving the check can't "take" the king or else it would blow up both kings, which is not allowed.

Looking back at the position above, even though the white king seems to be in check by the black bishop, that is actually not the case, because the white king is adjacent to the black king.

A legal move that is unique to atomic chess is that you can blow up the opponent's king even if your king is in check (or seemingly a checkmate!). For example, consider the following position:

Black to move

~~~~~R~k
~~~~~~pp
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~~~~~~
~~~~r~PP
~~~~~~~K

Picture of the board (Black to move):

Even though white seems to have checkmated black, it is actually legal for black to take the white pawn and blow up the white king.

Task

Your task is, given a board state (always \$8\times8\$) and a valid move, determine whether or not the move is legal in atomic chess. For the sake of this challenge, you can assume that a move is valid when the piece that is moving can make that move if there were only that piece on the board. So rooks won't be moving like knights, for example. You also won't have to deal with castling (short or long) as an input. You can also assume that it is white to move and that the board state is reachable in a real game (this prevents a board state like the black king already being in check, which is impossible if it's white's turn).

Test Cases

One of these test cases is from a real game while all the other test cases were created specifically for this challenge; can you figure out which one?

FEN, move (algebraic notation)
---------------------------------
Illegal:
rnb1k1nr/pp1p1ppp/2p5/q3B3/3b4/2N1PN2/PPP2PPP/R2QKB1R w KQkq - 3 7, Bxd4
rnb1k1nr/pp1p1ppp/2p5/q3B3/3b4/2N1PN2/PPP2PPP/R2QKB1R w KQkq - 3 7, Nd5
8/2kb4/2K5/8/8/8/8/3R4 w - - 20 30, Rd7
8/2kb4/2K5/8/8/8/8/3R4 w - - 20 30, Rd8
8/2kb4/2K5/8/8/8/8/3R4 w - - 20 30, Kb5
7k/4R1pp/8/8/8/8/6PP/5r1K w - - 14 24, Kg1
rnb1kbnr/1p2p3/pqp2ppp/2NpP3/8/5P2/PPPP1KPP/R1BQ1BNR w kq - 0 8, exd6 e.p.
5k1r/ppp3pp/5p2/2b5/4P3/4BP2/PP1r2PP/RN1Q1RK1 w - - 5 14, Qxd2

Legal:
8/2kb4/2K5/8/8/8/8/3R4 w - - 20 30, Rd6
7k/4R1pp/8/8/8/8/6PP/5r1K w - - 14 24, Rxg7
rnbqkb1r/pppppppp/8/3N4/4n3/8/PPPPPPPP/R1BQKBNR w KQkq - 4 3, Nxc7
8/8/4k3/4q3/5K2/8/8/8 w - - 11 26, Kf5
2rqk3/2q3K1/2qqq3/8/8/8/8/r7 w - - 26 53, Kf7

If you want to see the board states on an actual chessboard, you can use the analysis tool on lichess.org and paste in the FEN for each board.


This is , so the shortest code in bytes wins!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ This is challenging, atomic rules are very difficult to implement correctly. Even Fairy-Stockfish got it wrong once before. \$\endgroup\$
    – Sisyphus
    Nov 24, 2022 at 4:31
  • 1
    \$\begingroup\$ @Sisyphus Huh, I didn't think about castling at all! Maybe I should ban castling as an input, I don't have a clear idea of how it works in atomic. \$\endgroup\$
    – Aiden Chow
    Nov 24, 2022 at 4:39
  • 1
    \$\begingroup\$ I may not have fully covered everything as well, but this was every case that I could think of. \$\endgroup\$
    – Aiden Chow
    Nov 24, 2022 at 4:41
  • \$\begingroup\$ In the first example isn't black under check? How can it be white's turn? \$\endgroup\$
    – l4m2
    Dec 13, 2022 at 5:50
  • \$\begingroup\$ Seems check is defined via capture, exploding king is still allowed, which is quite weird \$\endgroup\$
    – l4m2
    Dec 13, 2022 at 5:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.