如何在prolog中移动到安全的邻近广场?

时间:2016-05-04 16:02:02

标签: algorithm prolog wumpus-world

我正在尝试编写Prolog应用程序,如果射手将移动到 4 x 4网格上安全的相邻广场。

例如,射手在方形第1列第4行中,如果它不包含记录为 M 的怪物,他可以向上或向右移动。因此,如果第2列第4行怪物(M)弓箭手(A)无法移动,但是如果第1列第3行 为空(E)他可以进入此状态。射手可以检查相邻的方格是否安全,但不能远离它。

这是我到目前为止所得到的:

:- 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).

我正在努力让射手检查相邻的广场是否安全,如果是,那么就进入那个广场。

如何做到这一点?

0 个答案:

没有答案