简体   繁体   中英

PROLOG Asignation problems

I have to solve a problem involving the WWESupercard game. The idea is to to take 2 cards and make them fight, comparing each stat alone and every combination, making 10 comparations in total. A fighter wins if it gets more than 50% of the fights (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:

  • It fails on "T is T+1" saying "0 is 0+1" and failing
  • 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..."

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.

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.

Similarly

X = 1, X = 2.

will fail.

If you want to do the equivalent of Python's

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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