Prolog将访问的方块添加到列表中,然后显示该列表

时间:2016-05-06 12:45:45

标签: prolog

我在Prolog应用程序中制作了一个游戏,射手将移动到一个相邻的方块,这个方块在一个4 x 4系统的网格上是安全的。用户可以输入他们想要去的坐标,例如,如果我输入safe_square(4,1,3,1)。弓箭手将从 4,1移动到3,1 。我想要做的是显示射手已经访问过的任何方块,方法是将它们添加到列表中,然后显示该列表。

我的代码如下:创建电路板,打印电路板,填充电路板,以及在电路板上移动。

% This buffer is for notes you don't want to save.
% If you want to create a file, visit that file with C-x C-f,
% then enter the text in that file's own buffer.

% square(row,col,val)
:- 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]])),
        assert(show([[3,1]])),
        assert(show([[2,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,[w])),
    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])).



createBoard(0,_).
createBoard(Row,Col) :-
 createCol(Row,Col),
 NextRow is Row -1,
 createBoard(NextRow,Col).

createCol(Row,1) :-
      assertz(square(Row,1,[e])).
createCol(CurrentRow,CurrentCol) :-
        assertz(
    square(CurrentRow,CurrentCol,[e])
            ),
        NextCol is CurrentCol -1,
        createCol(CurrentRow,NextCol).

maxBound(B) :-   %
    setof(Row,square(Row,_,_),L), % L = [1,2,3,4]
    reverse(L,[B|_]). % L = [4,3,2,1] => B = 4


printBoard :-
    maxBound(Bound),
    setof([R,C,Contents],
        square(R,C,Contents),Squares),
           % Squares = [[1,1,[e]],[1,2,[e],....]
    printSquares(Squares,Bound),nl.

printSquares([],_). % stop if we printed all the squares

printSquares(S,0) :-  % Print New Row
    maxBound(B),
    nl,printSquares(S,B).

% Print a square that is revealed
% Squares = [[1,1,[e]] |   [1,2,[e],....]
printSquares([[R,C,Contents]|Rest],ColCount) :-
    show(L),
    member([R,C],L),
    extract([p,w],Contents,Display),
    padd(Display,3,Padded),
    write(Padded),
    NewColCount is ColCount - 1,
    printSquares(Rest,NewColCount).

% Print one that is not visible
printSquares([_|Rest],ColCount) :-
    write('[----]'),
    NewColCount is ColCount - 1,
    printSquares(Rest,NewColCount).

extract([],L,L).
extract([H|T],L,FinalList) :-
    delete(L,H,ReducedL),
    % delete fails if H is not in the contents
    extract(T,ReducedL,FinalList).
extract([_|T],L,FinalList) :-
    extract(T,L,FinalList).

padd(L,Max,P) :-
    length(L,Len),
    paddout(L,Max,Len,P).

paddout(L,Max,Max,L).
paddout(L,Max,Len,[' '|P]) :-
    Len1 is Len + 1,
    paddout(L,Max,Len1,P).



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


safe_square(Xa,Ya,X,Y) :-
    maxBound(N),
    adjacent(Xa,Ya,X,Y,N),
    safe(X,Y),
    move(Xa,Ya,X,Y),
    retract(show(L)),
    append([[X,Y]],L,NewL),
    assert(show(NewL)).


safe_square(Xa,Ya,X,Y) :-
    maxBound(N),
    adjacent(Xa,Ya,X,Y,N),
    glitter(X,Y),
    move(Xa,Ya,X,Y),
    retract(show(L)),
    append([[X,Y]],L,NewL),
    assert(show(NewL)).

safe_square(Xa,Ya,X,Y) :-
    maxBound(N),
    adjacent(Xa,Ya,X,Y,N),
    glitter(X,Y),
    move(Xa,Ya,X,Y),
    retract(show(L)),
    append([[X,Y]],L,NewL),
    assert(show(NewL)).

safe_square(Xa,Ya,X,Y) :-
    maxBound(N),
    adjacent(Xa,Ya,X,Y,N),
    empty(X,Y),
    move(Xa,Ya,X,Y),
    retract(show(L)),
    append([[X,Y]],L,NewL),
    assert(show(NewL)).

safe_square(Xa,Ya,X,Y) :-
    maxBound(N),
    adjacent(Xa,Ya,X,Y,N),
    gold(X,Y),
    move(Xa,Ya,X,Y),
    retract(show(L)),
    append([[X,Y]],L,NewL),
    assert(show(NewL)).





move(Xa,Ya,X,Y) :-
    retract(square(Xa,Ya,ListFrom)),
    delete(ListFrom,a,Existing),
    retract(square(X,Y,ListTo)),
    append([a],ListTo,NextPositionList),
    assert(square(Xa,Ya,Existing)),
    assert(square(X,Y,NextPositionList)),
    !.

safe(X,Y) :-
    square(X,Y,L),
    \+ member(p,L),
    \+ member(w,L).

gold(X,Y) :-
    square(X,Y,L),
    member(x,L).
glitter(X,Y):-
    square(X,Y,L),
    member(g,L).
empty(X,Y):-
    square(X,Y,L),
    member(e,L).


adjacent(R,C,Ar,Ac,_) :- Ar is R -1,  Ar >= 1,  Ac is C.
adjacent(R,C,Ar,Ac,N) :-  Ar is R + 1,  Ar =< N,  Ac is C.
adjacent(R,C,Ar,Ac,_) :-  Ac is C - 1,  Ac >= 1,  Ar is R.
adjacent(R,C,Ar,Ac,N) :-  Ac is C + 1,  Ac =< N, Ar is R.


run:-
    printBoard,
    at(Xa,Ya,X,Y),
    move(Xa,Ya,X,Y),
    get0(_),
    run.

0 个答案:

没有答案