Solving a chess puzzle with Claude and Prolog
(832) 422-8646
Contact
Prolog is the original logic programming language. The name comes from programming in logic. More specifically, the name comes from programmation en logique because the inventor of the language, Philippe Roussel, is French.
Prolog has its advantages and disadvantages. One of the advantages is that the language represents logical problems directly. One of the disadvantages is that the syntax can be quirky. But if an LLM is writing the code, or at least helping to write the code, the syntax doesn’t matter so much.
I wanted to see how well Claude (Sonnet 4.6, medium effort) could solve a chess puzzle by Martin Gardner that I wrote about a little over a year ago. I chose a relatively obscure problem rather than something like the Eight Queens puzzle because an LLM could simply quote one of countless articles on the puzzle.
The puzzle
As I stated in the post last year, the task is to place two rooks, two bishops, and two knights on a 4 by 4 chessboard so that no piece attacks any other.
There are two basic solutions, twelve if you count rotations and reflections as different solutions.
Prolog results
Claude wrote an SWI-Prolog program that I ran with
swipl -g "run, halt" chess_placement.pl
and it gave the following output.
Total solutions (raw): 12<br>Solutions up to D4 symmetry: 2<br>Solutions up to D4 symmetry: 2
Solution 1:<br>B . . N<br>N . . B<br>. R . .<br>. . R .
Solution 2:<br>. B N .<br>. . . R<br>R . . .<br>. N B .
Claude made a nice image of the solutions.
Note that you can make eight variations of the first solution by a combination of rotations and flips. There are only four variations of the second solution because two rotations put you back where you started.
Prolog source
Here’s the Prolog code that Claude generated.
% ============================================================<br>% Non-attacking placement of 2 Rooks, 2 Bishops, 2 Knights<br>% on a 4x4 chessboard<br>% Run with SWI-Prolog:<br>% swipl -g "run, halt" chess_placement.pl<br>% Or interactively:<br>% swipl chess_placement.pl<br>% ?- run.<br>% ?- count_solutions(N).<br>% ?- count_unique(N).<br>% ============================================================
:- use_module(library(lists)).
% ---- Board squares: (Row, Col) with Row, Col in 0..3 ----------------
square(R, C) :- member(R, [0,1,2,3]), member(C, [0,1,2,3]).
% ---- Attack predicates ----------------------------------------------
rook_attacks(R,_,R,_). % same row<br>rook_attacks(_,C,_,C). % same column
bishop_attacks(R1,C1,R2,C2) :-<br>DR is abs(R1-R2), DC is abs(C1-C2),<br>DR =:= DC, DR > 0.
knight_attacks(R1,C1,R2,C2) :-<br>DR is abs(R1-R2), DC is abs(C1-C2),<br>( DR =:= 1, DC =:= 2 ; DR =:= 2, DC =:= 1 ).
% attacks(+Type1, +R1, +C1, +Type2, +R2, +C2)<br>% True if either piece threatens the other's square.<br>attacks(rook, R1,C1, _, R2,C2) :- rook_attacks(R1,C1,R2,C2).<br>attacks(_, R1,C1, rook, R2,C2) :- rook_attacks(R1,C1,R2,C2).<br>attacks(bishop, R1,C1, _, R2,C2) :- bishop_attacks(R1,C1,R2,C2).<br>attacks(_, R1,C1, bishop,R2,C2) :- bishop_attacks(R1,C1,R2,C2).<br>attacks(knight, R1,C1, _, R2,C2) :- knight_attacks(R1,C1,R2,C2).<br>attacks(_, R1,C1, knight,R2,C2) :- knight_attacks(R1,C1,R2,C2).
% ---- All pairs safe -------------------------------------------------
% Check all unordered pairs in a list.<br>no_attack_pair(_, []).<br>no_attack_pair(T1-(R1,C1), [T2-(R2,C2)|Rest]) :-<br>\+ attacks(T1,R1,C1,T2,R2,C2),<br>no_attack_pair(T1-(R1,C1), Rest).
all_pairs_safe([]).<br>all_pairs_safe([P|Rest]) :-<br>no_attack_pair(P, Rest),<br>all_pairs_safe(Rest).
% ---- Generate a placement -------------------------------------------<br>% Placement = [rook-(R1,C1), rook-(R2,C2),<br>% bishop-(R3,C3), bishop-(R4,C4),<br>% knight-(R5,C5), knight-(R6,C6)]<br>% We enforce an ordering within each pair to avoid counting<br>% same-type duplicates (rook1 vs rook2 swapped).<br>% Squares are represented canonically: (R,C) with R*4+C as index.
sq_index(R, C, I) :- I is R*4 + C.
placement(Placement) :-<br>% --- Rooks ---<br>square(R1r, C1r), square(R2r, C2r),<br>sq_index(R1r,C1r,I1r), sq_index(R2r,C2r,I2r),<br>I1r write('R ')<br>; member(bishop-(R,C), Placement) -> write('B ')<br>; member(knight-(R,C), Placement) -> write('N ')<br>; write('. ')<br>).
% ---- Print all unique solutions -------------------------------------
print_unique_solutions :-<br>findall(P, placement(P), Ps),<br>maplist(canonical_placement, Ps, Canonicals),<br>list_to_set(Canonicals, Unique),<br>length(Unique, N),<br>format("~nSolutions up to D4 symmetry: ~w~n~n", [N]),<br>forall(nth1(I, Unique, Sol),<br>( format("Solution ~w:~n", [I]),<br>print_board(Sol) )).
% ---- Top-level entry point ------------------------------------------
run :-<br>count_solutions(Raw),<br>count_unique(Sym),<br>format("~n"),<br>print_unique_solutions,<br>format("Summary: ~w raw solutions, ~w up to D4 symmetry.~n",<br>[Raw, Sym]).
4 thoughts on “Solving a chess puzzle with Claude and Prolog”
Carlo Donzella
11 June 2026 at 14:37
how was the problem described to Claude?
John
11 June 2026 at 15:06
"Consider a 4 by 4 chess board. Is...