简体   繁体   English

Prolog If Then Else失败,成员谓词

[英]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. 从用户那里获取一个号码(ArmyNo)。
  2. Take an X coordinate 取X坐标
  3. Take an Y coordinate. 取Y坐标。

Then I have a list that is named TempBoard and it looks like this: 然后,我有一个名为TempBoard的列表,看起来像这样:

([
    (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. 在将此(X,Y,w,ArmyNO)添加到列表之前,我首先要检查它是否已经存在。

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

          %%
(X, Y, w, ArmyNo)

cannot be unified with any member of your example list because w doesn't unify with - . 无法与示例列表的任何成员统一,因为w不能与-统一。 You may have meant W . 您可能是说W

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM