简体   繁体   English

PROLOG 赋值问题

[英]PROLOG Asignation problems

I have to solve a problem involving the WWESupercard game.我必须解决一个涉及 WWESupercard 游戏的问题。 The idea is to to take 2 cards and make them fight, comparing each stat alone and every combination, making 10 comparations in total.这个想法是拿 2 张牌让他们战斗,单独比较每个统计数据和每个组合,总共进行 10 次比较。 A fighter wins if it gets more than 50% of the fights (6/10).如果一名战士获得超过 50% 的战斗 (6/10),则它获胜。

The problem I have is that the "is" statement is not working for me.我遇到的问题是“是”语句对我不起作用。

This are some of the "solutions" I've tried so far, using only the base stats and no combinations for faster testing, none of them are working as I intend:这是我迄今为止尝试过的一些“解决方案”,仅使用基本统计数据而不使用组合来进行更快的测试,但没有一个能按我的预期工作:

leGana(X,Y) :- T is 0, 
              ((A is 0, (pwr(X,Y) -> A1 is A+1));
               (B is 0, (tgh(X,Y) -> B1 is B+1));
               (C is 0, (spd(X,Y) -> C1 is C+1));
               (D is 0, (cha(X,Y) -> D1 is D+1))),
          T1 is T+A1+B1+C1+D1, T1 > 2.

leGana(X,Y) :- T is 0, 
                ((pwr(X,Y) -> T is T+1);
                 (tgh(X,Y) -> T is T+1);
                 (spd(X,Y) -> T is T+1);
                 (cha(X,Y) -> T is T+1)),
            T1 > 2.

I've tried other ways, but I keep getting the same 2 error, most of the times:我尝试了其他方法,但大多数时候我总是遇到同样的 2 个错误:

  • It fails on "T is T+1" saying "0 is 0+1" and failing它在“T is T+1”上失败说“0 is 0+1”并且失败
  • On example 1, it does the "A1 is A+1", getting "1 is 0+1", but then it jumps directly to the last line of the code, getting "T1 is 0+1+_1543+_1546..."在示例 1 中,它执行“A1 is A+1”,得到“1 is 0+1”,但随后它直接跳到代码的最后一行,得到“T1 is 0+1+_1543+_1546.. ”

Also, sorry if the way I wrote the code is not correct, it's my first time asking for help here, so any advice is welcome.另外,如果我写代码的方式不正确,这是我第一次在这里寻求帮助,所以欢迎任何建议。

Prolog is a declarative language, which means that a variable can only have a single value. Prolog 是一种声明式语言,这意味着一个变量只能有一个值。

If you do this:如果你这样做:

X is 1, X is 2.

it will fail because you're trying to say that X has both the value 1 and the value 2 , which is impossible... that is is is not assignment but a kind of equality that evaluates the right-hand side.它会失败,因为你试图说X具有值1和值2 ,这是不可能的......那is赋值而是一种评估右侧的相等性。

Similarly相似地

X = 1, X = 2.

will fail.将失败。

If you want to do the equivalent of Python's如果你想做相当于 Python 的

def f(x):
   result = 1
   if x == 0:
      result += 1
   return result

then you need to do something like this:那么你需要做这样的事情:

f(X, Result) :-
    Result1 = 1,
    (  X = 0
    -> Result is Result1 + 1
    ;  Result = Result1
    ).

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

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