Reputation: 163
I am programming a board game and I want to check if a move is possible. To do that, I need to check if the position player is moving to is empty or has a possible value. However, I don't know how can I do that without returning true or false.
Example: Imagine you have this matrix, which represents a board:
[ [vv,vv,vv,vv,p1]
[vv,aa,vv,vv,vv]
[vv,vv,aa,vv,vv]
[vv,p2,aa,vv,vv] ]
I want to move p2 to aa, which is not empty but it is a possible move. How can I check if it's a possible move? I have a procedure to get the element at a specific position, and I am trying to implement the procedure that checks if position I am moving at is a possible one.
Current code:
elementAt(Line, 1, Y, Element):- nth0(Y, Line, Element).
elementAt([Line|Tail], X, Y, Element) :-
Y > 1,
Y1 is Y-1,
elementAt(Tail, X, Y, Element).
checkPosition(Board, X, Y):-
elementAt(Board, X, Y, Element),
Now, at checkPosition, I want to check if Element equals to vv or aa. How can I do that?
Upvotes: 0
Views: 938
Reputation: 329
% It is true, if
% 1. X is the first element of the listoflists
% 2. X is not first element of the listoflists,
% however, it is a member of the first list in listoflists
% 3. X is a member of any other element in the listoflists
memberlist(X, [X|Xs]).
memberlist(X, [Y|Ys]) :- memberlist(X, Y).
memberlist(X, [Y|Ys]) :- memberlist(X, Ys).
I know this is posted 4 years later, hope it will be helpful to whomever runs into this answer.
Upvotes: 0