Algorithm 如何在prolog中移动到安全的相邻广场?
我正在尝试编写一个Prolog应用程序,如果一个弓箭手将移动到一个相邻的正方形,该正方形在4x4网格上是安全的 例如,弓箭手在第4行第1列的正方形中,如果没有记录为M的怪物,他可以向上或向右移动。因此,如果第2列第4行有怪物(M),弓箭手(A)不能移动到那里,但是如果第1列第3行是空的(E),他可以移动到这里。弓箭手可以检查相邻的方格是否安全,但不能再远了 以下是我到目前为止得到的信息:Algorithm 如何在prolog中移动到安全的相邻广场?,algorithm,prolog,wumpus-world,Algorithm,Prolog,Wumpus World,我正在尝试编写一个Prolog应用程序,如果一个弓箭手将移动到一个相邻的正方形,该正方形在4x4网格上是安全的 例如,弓箭手在第4行第1列的正方形中,如果没有记录为M的怪物,他可以向上或向右移动。因此,如果第2列第4行有怪物(M),弓箭手(A)不能移动到那里,但是如果第1列第3行是空的(E),他可以移动到这里。弓箭手可以检查相邻的方格是否安全,但不能再远了 以下是我到目前为止得到的信息: :- dynamic square/3. :- dynamic show/1. createBoard(N
:- dynamic square/3.
:- dynamic show/1.
createBoard(N) :-
retractall(show(_)),
assert(show([[1,1]])),
retractall(square(_,_,_)),
createBoard(N,N).
testBoard :-
retractall(square(_,_,_)),
retractall(show(_)),
assert(show([[4,1]])),
asserta(square(1,1,[e])),
asserta(square(1,2,[e])),
asserta(square(1,3,[s])),
asserta(square(1,4,[e])),
asserta(square(2,1,[e])),
asserta(square(2,2,[b,s])),
asserta(square(2,3,[m])),
asserta(square(2,4,[g,s])),
asserta(square(3,1,[b])),
asserta(square(3,2,[p])),
asserta(square(3,3,[g,b,s])),
asserta(square(3,4,[x])),
asserta(square(4,1,[a,e])),
asserta(square(4,2,[b])),
asserta(square(4,3,[e])),
asserta(square(4,4,[g])).
% you can place a piece on a board of N size if you can generate a row
% and column value and you can place that piece on an square empty square
place(Piece,N) :-
Row1 is random(N),
Col1 is random(N),
place(Piece,Row1,Col1,N).
% you can place a pit if the square is empty at the specified
% position
place(p,Row,Col,_) :-
square(Row,Col,[e]),
retract(square(Row,Col,[e])),
assert(square(Row,Col,[p])).
% Find an Item at a position R,C by
% examining the contents of the R,C, location
find(R,C,Item) :-
square(R,C,Contents),
member(Item,Contents).
我正在努力让弓箭手检查附近的一个广场是否安全,如果安全,然后进入该广场
如何做到这一点?您所说的安全是什么意思?它是一个不包含怪物的正方形,还是一个与怪物不相邻的正方形?是的,一个不包含怪物的正方形,并且不是对角的。然后你会检查每个看起来像
square(X,Y,L)
的正方形,其中X
和Y
中的任何一个(但不是两个)离弓箭手的位置只有1,而L
不包含m
。如果一个安全广场没有怪物,而只有一个怪物,那么它最多只能有一个不安全的广场。因此,一个谓词safe\u square(Ax,Ay,X,Y)
可以用这个逻辑来编写,给定射手的位置,为下一步的移动提供可能的X
和Y
位置。然后我会说move\u archer(X,L)或move\u archer(Y,L)使它移动到那个正方形吗?你可以随意定义它,但我可能需要move\u archer(X,Y)
。您不需要L
。这是目前在新地点的物品清单move_archer(X,Y)
将正确地用弓箭手重新声明新位置,并将弓箭手从当前位置移除。