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

Prolog将已访问的方块添加到列表中,然后显示该列表,prolog,Prolog,我已经在Prolog应用程序中制作了一个游戏,一个弓箭手将移动到一个相邻的正方形中,该正方形在一个4x4系统的网格上是安全的。用户可以输入他们想要去的坐标,例如,如果我输入safe_square(4,1,3,1)。弓箭手将从4,1移动到3,1。我想做的是通过将弓箭手访问过的任何方块添加到列表中,然后显示该列表来显示这些方块 我的代码如下:创建电路板、打印电路板、填充电路板以及在电路板中移动 % This buffer is for notes you don't want to save. %

我已经在Prolog应用程序中制作了一个游戏,一个弓箭手将移动到一个相邻的正方形中,该正方形在一个4x4系统的网格上是安全的。用户可以输入他们想要去的坐标,例如,如果我输入
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.
%此缓冲区用于您不想保存的便笺。
%如果要创建文件,请使用C-x C-f访问该文件,
%然后在该文件自己的缓冲区中输入文本。
%方形(行、列、瓦尔)
:-动态方块/3。
:-动态显示/1。
createBoard(N):-
收回所有(显示()),
断言(显示([[1,1]]),
收回所有(平方(u,u,u)),
createBoard(N,N)。
测试板:-
收回所有(平方(u,u,u)),
收回所有(显示()),
断言(显示([[4,1]]),
断言(显示([[3,1]]),
断言(显示([[2,1]]),
资产(平方(1,1[e]),
资产(平方(1,2,[e]),
资产(平方(1,3[s]),
资产(平方(1,4[e]),
资产(平方(2,1[e]),
资产(平方(2,2[b,s]),
asserta(正方形(2,3[w]),
资产(平方(2,4[g,s]),
资产(平方(3,1[b]),
asserta(平方(3,2,[p]),
资产(平方(3,3[g,b,s]),
asserta(正方形(3,4[x]),
资产(平方(4,1[a,e]),
asserta(正方形(4,2[b]),
asserta(正方形(4,3[e]),
asserta(平方(4,4[g])。
createBoard(0,41;。
createBoard(行、列):-
createCol(行,列),
下一行是第1行,
createBoard(下一个,Col)。
createCol(第1行):-
assertz(方形(第1排[e])。
createCol(CurrentRow,CurrentCol):-
阿塞尔茨(
正方形(CurrentRow,CurrentCol,[e])
),
NextCol是CurrentCol-1,
createCol(CurrentRow,NextCol)。
最大约束(B):-%
集合(行,正方形(行,列,列),L),%L=[1,2,3,4]
反向(L,[B| |])。%L=[4,3,2,1]=>B=4
印刷板:-
maxBound(绑定),
集合([R,C,Contents],
正方形(R、C、内容物),正方形,
%平方=[[1,1[e]],[1,2[e],…]
打印方块(方块,绑定),nl。
printSquares([],。%)如果我们打印了所有的方块,则停止打印
打印方块(S,0):-%打印新行
maxBound(B),
nl,打印正方形(S,B)。
%打印一个显示的正方形
%平方=[[1,1[e]]|[1,2[e],…]
打印方块([[R,C,Contents]| Rest],ColCount):-
显示(L),
成员([R,C],L),
摘录([p,w],内容,显示),
填充(显示器,3,填充),
写入(填充),
NewColCount是ColCount-1,
打印方块(Rest、NewColCount)。
%打印一个不可见的
打印方块([[u124; Rest],ColCount):-
写入('[---]'),
NewColCount是ColCount-1,
打印方块(Rest、NewColCount)。
提取([],L,L)。
摘录([H | T],L,最终列表):-
删除(L,H,ReducedL),
%如果内容中没有H,则删除失败
提取(T,还原,最终)。
摘录([[u124; T]、L、FinalList):-
摘录(T、L、FinalList)。
padd(左、最大、P):-
长度(L,Len),
paddout(左,最大,长,P)。
paddout(L,Max,Max,L)。
paddout(L,Max,Len,[''P]):-
Len1是Len+1,
paddout(左,最大,第1列,第P列)。
%如果可以生成行和列的值,并且可以将一块放在一个正方形或空正方形上,则可以将该块放在N大小的板上
放置(件,N):-
行1是随机的(N),
Col1是随机的(N),
放置(件,第1行,第1列,第N列)。
%如果正方形在指定位置为空,则可以放置坑
%位置
地点(p、Row、Col和u2;):-
正方形(行,列,[e]),
缩回(方形(行,列,[e]),
断言(正方形(行,列,[p]))。
%在位置R,C处通过
%检查R、C、位置的内容
查找(R、C、项目):-
方形(R、C、内容物),
成员(项目、内容)。
安全广场(Xa,Ya,X,Y):-
maxBound(N),
相邻(Xa,Ya,X,Y,N),
安全(X,Y),
移动(Xa,Ya,X,Y),
收回(显示(L)),
追加([[X,Y]],L,NewL),
断言(show(NewL))。
安全广场(Xa,Ya,X,Y):-
maxBound(N),
相邻(Xa,Ya,X,Y,N),
闪光(X,Y),
移动(Xa,Ya,X,Y),
收回(显示(L)),
追加([[X,Y]],L,NewL),
断言(show(NewL))。
安全广场(Xa,Ya,X,Y):-
maxBound(N),
相邻(Xa,Ya,X,Y,N),
闪光(X,Y),
移动(Xa,Ya,X,Y),
收回(显示(L)),
追加([[X,Y]],L,NewL),
断言(show(NewL))。
安全广场(Xa,Ya,X,Y):-
maxBound(N),
相邻(Xa,Ya,X,Y,N),
空(X,Y),
移动(Xa,Ya,X,Y),
收回(显示(L)),
追加([[X,Y]],L,NewL),
断言(show(NewL))。
安全广场(Xa,Ya,X,Y):-
maxBound(N),
相邻(Xa,Ya,X,Y,N),
黄金(X,Y),
移动(Xa,Ya,X,Y),
收回(显示(L)),
追加([[X,Y]],L,NewL),
断言(show(NewL))。
移动(Xa,Ya,X,Y):-
收回(方形(Xa,Ya,ListFrom)),
删除(ListFrom、a、现有),
缩回(方形(X,Y,ListTo)),
追加([a]、ListTo、NextPositionList),
断言(平方(Xa,Ya,现有)),
断言(平方(X,Y,NextPositionList)),
!.
安全(X,Y):-
正方形(X,Y,L),
\+成员(p,L),
\+成员(w,L)。
黄金(X,Y):-
正方形(X,Y,L),
成员(x,L)。
闪光(X,Y):-
正方形(X,Y,L),
成员(g、L)。
空(X,Y):-
正方形(X,Y,L),
成员(e,L)。
相邻(R,C,Ar,Ac,u):-Ar是R-1,Ar>=1,Ac是C。
相邻(R,C,Ar,Ac,N):-Ar是R+1,Ar==1,Ar是R。
相邻(R,C,Ar,Ac,N):-Ac是C+1,Ac=
我没有看到任何问题。您需要解释高亮显示的含义。问题是我如何高亮显示archer访问过的正方形。如果archer访问过正方形,我只想在网格上高亮显示黄色的正方形。@Lourgeri我不确定您使用的是什么Prolog框架来图形化表示数据。是吗你问的是如何从字面上突出显示一个正方形,还是你真的在问如何识别这个正方形