Recently, I was introduced to a puzzle game known as Solitaire Chess. I'll summarize the rules here:
- The board is a 4x4 checkerboard.
- All pieces are the same color (no teams) and all pieces can capture any other piece.
- Every move must be a capture. No moving to empty squares.
- There must be exactly one piece remaining at the end.
- All pieces move exactly like they do in chess, with one modification: the pawn can capture in any diagonal direction (which technically makes it a ferz). For the benefit of those who may not know, I have included movement diagrams.
- None of the other rules of chess (such as check, castling, etc.) apply here. It's all about captures.
King (K)
K * . . | * K * . | * * * .
* * . . | * * * . | * K * .
. . . . | . . . . | * * * .
. . . . | . . . . | . . . .
Queen (Q)
Q * * * | * Q * * | * * * .
* * . . | * * * . | * Q * *
* . * . | . * . * | * * * .
* . . * | . * . . | . * . *
Rook (R)
R * * * | * R * * | . * . .
* . . . | . * . . | * R * *
* . . . | . * . . | . * . .
* . . . | . * . . | . * . .
Bishop (B)
B . . . | . B . . | * . * .
. * . . | * . * . | . B . .
. . * . | . . . * | * . * .
. . . * | . . . . | . . . *
Knight (N)
N . . . | . N . . | . . . *
. . * . | . . . * | . N . .
. * . . | * . * . | . . . *
. . . . | . . . . | * . * .
Pawn (P)
P . . . | . P . . | * . * .
. * . . | * . * . | . P . .
. . . . | . . . . | * . * .
. . . . | . . . . | . . . .
Input/output
For reference, the sample puzzle from the Solitaire Chess webpage will be used:
. . . .
. B . .
R P . .
. . . N
The solution is to take the pawn with the knight, then take the knight with the rook, and finally take the bishop with the rook.
Input
Input must be in one of three forms; you are free to pick the one that is most convenient for you.
- A string of characters such as
.....B..RP.....N
, with or without newlines. The character representing a blank space may be any character that isn't one ofKQRBNP
. - A list of lists (or a flattened list) where the elements are either characters or numbers, like so:
[['.', '.', '.', '.'], ['.', 'B', '.', '.'], ['R', 'P', '.', '.'], ['.', '.', '.', 'N']]
or[[0, 0, 0, 0], [0, 4, 0, 0], [3, 6, 0, 0], [0, 0, 0, 5]]
. For the former, the character that represents a blank space may be anything that isn't one ofKQRBNP
. For the latter, I've given pieces the number that corresponds to their rank in my earlier list of moves (1
is a king,4
is a bishop,6
is a pawn, etc.). You are free to change the numbering. - A list of coordinates where each element has the form
[x, y, 'c']
, like so:[[1, 2, 'B'], [0, 1, 'R'], [1, 1, 'P'], [3, 0, 'N']]
.
If you pick one of the list-based input formats, the separators and delimiters may be any reasonable and understandable characters.
Output
Output must be a sequence of moves or a sequence of board states. Some puzzles have more than one solution; you may output one or all of them. If you choose to output a sequence of board states, each board must be in one of the three input formats, with a reasonable separator (such as newlines) between them.
If you choose to output a sequence of moves, they must be expressed as a list of pairs of coordinate pairs, like so: [[[3,0], [1,1]], [[0,1], [1,1]], [[1,1], [1,2]]]
. [0,0]
represents the bottom left corner, and again, separating and delimiting characters may be any reasonable choice.
If a given board cannot be solved, output any falsy value (0
, empty string, etc.). If a given board has fewer than two pieces, behavior is undefined.
Test Cases
Note: outputs are only given as a list of pairs of coordinates since the other formats should be fairly easy to check for correctness (and I didn't feel like typing out all the possible output formats). Also, for the puzzles that have more than one solution, only one possibility is provided.
Input 1:
. . . N
. . . .
. R . .
. . B .
...N.....R....B.
[['.', '.', '.', 'N'], ['.', '.', '.', '.'], ['.', 'R', '.', '.'], ['.', '.', 'B', '.']]
[[0, 0, 0, 5], [0, 0, 0, 0], [0, 3, 0, 0], [0, 0, 4, 0]]
[[3, 3, 'N'], [1, 1, 'R'], [2, 0, 'B']]
Output 1:
[[[2,0], [1,1]], [[1,1], [3,3]]]
Input 2:
. . . .
. B . .
R P . .
. . . N
.....B..RP.....N
[['.', '.', '.', '.'], ['.', 'B', '.', '.'], ['R', 'P', '.', '.'], ['.', '.', '.', 'N']]
[[0, 0, 0, 0], [0, 4, 0, 0], [3, 6, 0, 0], [0, 0, 0, 5]]
[[1, 2, 'B'], [0, 1, 'R'], [1, 1, 'P'], [3, 0, 'N']]
Output 2:
[[[3,0], [1,1]], [[0,1], [1,1]], [[1,1], [1,2]]]
Input 3:
. N R .
B . . .
N . . B
. . P .
.NR.B...N..B..P.
[['.', 'N', 'R', '.'], ['B', '.', '.', '.'], ['N', '.', '.', 'B'], ['.', '.', 'P', '.']]
[[0, 5, 3, 0], [4, 0, 0, 0], [5, 0, 0, 4], [0, 0, 6, 0]]
[[1, 3, 'N'], [2, 3, 'R'], [0, 2, 'B'], [0, 1, 'N'], [3, 1, 'B'], [2, 0, 'P']]
Output 3:
[[[2,0], [3,1]], [[0,1], [1,3]], [[0,2], [1,3]], [[2,3], [1,3]], [[3,1], [1,3]]]
Input 4:
. . . N
. . . R
R B B .
N P P .
...N...RRBB.NPP.
[['.', '.', '.', 'N'], ['.', '.', '.', 'R'], ['R', 'B', 'B', '.'], ['N', 'P', 'P', '.']]
[[0, 0, 0, 5], [0, 0, 0, 3], [3, 4, 4, 0], [5, 6, 6, 0]]
[[3, 3, 'N'], [3, 2, 'R'], [0, 1, 'R'], [1, 1, 'B'], [2, 1, 'B'], [0, 0, 'N'], [1, 0, 'P'], [2, 0, 'P']]
Output 4:
[[[2,1], [3,2]], [[1,1], [3,3]], [[3,2], [1,0]], [[3,3], [0,0]], [[0,1], [0,0]], [[0,0], [1,0]], [[1,0], [2,0]]]
Input 5:
P . . .
. R . .
R . R .
. R . .
P....R..R.R..R..
[['P', '.', '.', '.'], ['.', 'R', '.', '.'], ['R', '.', 'R', '.'], ['.', 'R', '.', '.']]
[[6, 0, 0, 0], [0, 3, 0, 0], [3, 0, 3, 0], [0, 3, 0, 0]]
[[0, 3, 'P'], [1, 2, 'R'], [0, 1, 'R'], [2, 1, 'R'], [1, 0, 'R']]
Output 5:
[[[0,3], [1,2]], [[1,2], [2,1]], [[2,1], [1,0]], [[1,0], [0,1]]]
Input 6:
. P . N
K . . .
. . B .
. . R Q
.P.NK.....B...RQ
[['.', 'P', '.', 'N'], ['K', '.', '.', '.'], ['.', '.', 'B', '.'], ['.', '.', 'R', 'Q']]
[[0, 6, 0, 5], [1, 0, 0, 0], [0, 0, 4, 0], [0, 0, 3, 2]]
[[1, 3, 'P'], [3, 3, 'N'], [0, 2, 'K'], [2, 1, 'B'], [2, 0, 'R'], [3, 0, 'Q']]
Output 6:
[[[3,0], [2,0]], [[2,0], [2,1]], [[3,3], [2,1]], [[2,1], [1,3]], [[0,2], [1,3]]]
[["R", [2, 0], [1, 1]], ["N", [1, 1], [3, 3]]]
\$\endgroup\$