Just a few days in the past I wrote about utilizing Claude to resolve a chess puzzle by writing Prolog code. This morning I attempted a related chess puzzle with ChatGPT. The duty is to position a queen, king, rook, bishop, and knight on a 4 by 4 chessboard so no piece assaults one other.
After all there’s not a variety of market demand for fixing chess puzzles, however there may be demand for fixing real-world issues that quantity to logic puzzles. Experiments with chess puzzles recommend writing Prolog with LLM assist is perhaps nice. The syntax of Prolog generally is a little odd, however the LLM handles that. And since Prolog has been round so lengthy with out altering a lot, you’d anticipate LLMs to be good at producing code in it. [1]
I gave ChatGPT this immediate:
Write a SWI Prolog program to resolve the next chess puzzle. Place a queen, king, rook, bishop, and knight on a 4 by 4 chessboard so no piece assaults one other. Print all options.
It produced a file chess4.pl (under) that I ran with
swipl -q -s chess4.pl -g clear up -t halt
It discovered all 16 options:
Resolution 1: queen=1-2 king=2-4 rook=3-1 bishop=4-3 knight=4-4 . Ok . N . . . B Q . . . . . R . Resolution 2: queen=1-2 king=4-3 rook=2-4 bishop=3-1 knight=4-1 . R . . . . . Ok Q . . . . . B N ...
Right here’s the code.
% chess4.pl
% Place queen, king, rook, bishop, and knight on a 4x4 board
% in order that no piece assaults one other.
:- use_module(library(lists)).
sq.(X-Y) :-
between(1, 4, X),
between(1, 4, Y).
clear up :-
findall(Board, answer(Board), Boards),
print_solutions(Boards, 1),
size(Boards, Depend),
format('Whole options: ~d~n', [Count]).
answer(Board) :-
Items = [queen, king, rook, bishop, knight],
findall(S, sq.(S), Squares),
place_pieces(Items, Squares, Board),
secure(Board).
place_pieces([], _, []).
place_pieces([Piece | Pieces], Squares, [Piece-Square | Board]) :-
choose(Sq., Squares, RemainingSquares),
place_pieces(Items, RemainingSquares, Board).
secure(Board) :-
secure(Board, Board).
secure([], _).
secure([Piece-Square | Rest], Board) :-
forall(
member(OtherPiece-OtherSquare, Relaxation),
non_attacking(Piece-Sq., OtherPiece-OtherSquare, Board)
),
secure(Relaxation, Board).
non_attacking(P1-S1, P2-S2, Board) :-
+ assaults(P1, S1, S2, Board),
+ assaults(P2, S2, S1, Board).
% Assault guidelines
assaults(king, X1-Y1, X2-Y2, _) :-
X1-Y1 = X2-Y2,
DX is abs(X1 - X2),
DY is abs(Y1 - Y2),
DX =< 1,
DY =< 1. assaults(knight, X1-Y1, X2-Y2, _) :- DX is abs(X1 - X2), DY is abs(Y1 - Y2), ( DX =:= 1, DY =:= 2 ; DX =:= 2, DY =:= 1 ). assaults(rook, S1, S2, Board) :- same_rank_or_file(S1, S2), clear_line(S1, S2, Board). assaults(bishop, S1, S2, Board) :- same_diagonal(S1, S2), clear_line(S1, S2, Board). assaults(queen, S1, S2, Board) :- ( same_rank_or_file(S1, S2) ; same_diagonal(S1, S2) ), clear_line(S1, S2, Board). same_rank_or_file(X1-Y1, X2-Y2) :- ( X1 =:= X2 ; Y1 =:= Y2 ). same_diagonal(X1-Y1, X2-Y2) :- DX is abs(X1 - X2), DY is abs(Y1 - Y2), DX =:= DY. % For sliding items, ensure that no piece blocks the trail. clear_line(X1-Y1, X2-Y2, Board) :- step(X1, X2, SX), step(Y1, Y2, SY), NX is X1 + SX, NY is Y1 + SY, clear_path(NX-NY, X2-Y2, SX-SY, Board). clear_path(Goal, Goal, _, _) :- !. clear_path(X-Y, Goal, SX-SY, Board) :- + occupied(X-Y, Board), NX is X + SX, NY is Y + SY, clear_path(NX-NY, Goal, SX-SY, Board). step(A, B, 0) :- A =:= B. step(A, B, 1) :- B > A.
step(A, B, -1) :-
B < A.
occupied(Sq., Board) :-
member(_-Sq., Board).
% Printing
print_solutions([], _).
print_solutions([Board | Rest], N) :-
format('Resolution ~d:~n', [N]),
print_coordinates(Board),
print_board(Board),
nl,
N1 is N + 1,
print_solutions(Relaxation, N1).
print_coordinates(Board) :-
member(queen-Q, Board),
member(king-Ok, Board),
member(rook-R, Board),
member(bishop-B, Board),
member(knight-N, Board),
format(' queen=~w king=~w rook=~w bishop=~w knight=~w~n',
[Q, K, R, B, N]).
print_board(Board) :-
forall(
between(1, 4, Row0),
(
Y is 5 - Row0,
forall(
between(1, 4, X),
(
piece_at(X-Y, Board, Image),
format('~w ', [Symbol])
)
),
nl
)
).
piece_at(Sq., Board, Image) :-
member(Piece-Sq., Board),
!,
piece_symbol(Piece, Image).
piece_at(_, _, '.').
piece_symbol(queen, 'Q').
piece_symbol(king, 'Ok').
piece_symbol(rook, 'R').
piece_symbol(bishop, 'B').
piece_symbol(knight, 'N').
[1] I’ve performed round with producing Prolog and Lean this weekend, and I’ve had higher outcomes with Prolog. The issues with Lean haven’t been Lean per se however the Mathlib library. The library is continuously refactored, which is sensible for a younger language, however this makes it tougher to generate and debug code.
