2

I am trying to write a Prolog application were an archer will move into an adjacent square that is safe on a 4 x 4 grid.

For example the archer is in the square column 1 row 4, he can move up or right if it does not contain the monster recorded as M. So if column 2 row 4 had the Monster (M), the archer(A) could not move there but if column 1 row 3 is empty(E) he can move into this. The archer can check if the adjacent squares are safe but not any further away than that.

Here is what I got so far:

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

I am struggling to make the archer check if an adjacent square is safe and if it is, then move into that square.

How can this be done?

false
  • 10,264
  • 13
  • 101
  • 209
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • What do you mean by *safe*? Is it a square not containing the monster, or a square not adjacent to the monster? And does that count diagonally? – lurker May 04 '16 at 16:20
  • Yes a square not containing the monster and not diagonally – Craig Gallagher May 04 '16 at 16:22
  • Then you would examine each square that looks like `square(X, Y, L)` where `X` and `Y` are either (but not both) only 1 away from the archer's location, and `L` does not contain `m`. There would be at most one unsafe square if a safe square is anything without the monster and there's just one monster. So a predicate `safe_square(Ax, Ay, X, Y)` could be written using this logic that, given the archer's position, gives possible `X` and `Y` location for the next move. – lurker May 04 '16 at 16:24
  • And then would I say move_archer(X, L) or move_archer(Y, L) to make it move to that square? – Craig Gallagher May 04 '16 at 16:30
  • You can define it how you like, but I'd probably have `move_archer(X, Y)`. You don't need `L`. That's the list of things currently at the new location already. `move_archer(X, Y)` would properly re-assert the new location with the archer, and remove the archer from their current location. – lurker May 04 '16 at 16:36

0 Answers0