我对Prolog很陌生,我正在尝试编写一个简单的方法来确定棋盘上的骑士是否可以跳到另一个方格,或者输出一个骑士可以跳到给定方格的所有方格。对于此方法,假设必须始终实例化第一个参数。给定两个参数,它可以正常工作,但我不确定为什么它不会只输出一个。
% validSquare/2
% validSquare(X1/Y1, X2/Y2) iff the coordinate is a valid position on an 8x8 chessboard.
validSquare(X1/Y1, X2/Y2) :-
X1 >= 1, X1 =< 8,
Y1 >= 1, Y1 =< 8,
X2 >= 1, X2 =< 8,
Y2 >= 1, Y2 =< 8.
% jump/2
% jump(Square1, Square2) iff a knight could jump to the coordinate
% Square1/Square2 on a chessboard.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 + 1,
Y2 is Y1 + 2.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 + 2,
Y2 is Y1 + 1.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 + 1,
Y2 is Y1 - 2.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 + 2,
Y2 is Y1 - 1.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 - 1,
Y2 is Y1 - 2.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 - 2,
Y2 is Y1 - 1.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 - 1,
Y2 is Y1 + 2.
jump(X1/Y1, X2/Y2) :-
validSquare(X1/Y1, X2/Y2),
X2 is X1 - 2,
Y2 is Y1 + 1.
就像我说的,我对 Prolog 很陌生,所以我不确定我应该如何格式化查询。这个查询是假的。
?- jump(1/1, X2/Y2).
ERROR: >=/2: Arguments are not sufficiently instantiated
谢谢你的帮助。