Dite Gashi
Dite Gashi

Reputation: 128

Prolog If Then Else fail with member predicate

Hello guys I am working on a prolog game and I need to write a piece of code that will:

  1. Take a number (ArmyNo) from the user.
  2. Take an X coordinate
  3. Take an Y coordinate.

Then I have a list that is named TempBoard and it looks like this:

([
    (1,1,-,-),(1,2,-,-),(1,3,-,-),(1,4,-,-),
    (2,1,-,-),(2,2,-,-),(2,3,-,-),(2,4,-,-),
    (3,1,-,-),(3,2,-,-),(3,3,-,-),(3,4,-,-),
    (4,1,-,-),(4,2,-,-),(4,3,-,-),(4,4,-,-)
]).

before I add this (X,Y,w,ArmyNO) to the list I first want to check it if it is already there.

I attempted to do that by using this code but it does not seem to work properly:

%#######Got the number####
        repeat,
    %Get Cordinates X & Y.
    writelist( [TempBoard,'select coordinates for the horizontal axis 1 to 4 to place          your soldier Mr. Human',nl]),
    read(X),
    writelist(['select coordinates for the vertical axis 1 to 4 to place your soldier Mr. Human',nl]),
    read(Y),
    %Check if they are in the list.
          (
              member( (X,Y,w,ArmyNo),TempBoard )  ->
                       (  replace((X,Y,w,ArmyNo),TempBoard,NewBoard) ) ;
                 (
             writelist(['selected positions are not available in the table Mr.Human',nl]) , fail
             )
          ).

          %%

Upvotes: 0

Views: 530

Answers (1)

Fred Foo
Fred Foo

Reputation: 363487

(X, Y, w, ArmyNo)

cannot be unified with any member of your example list because w doesn't unify with -. You may have meant W.

Upvotes: 1

Related Questions