Apply a predicate to a list of inputs Prolog

120 views Asked by At

What i'm trying to do is:

Given a list of characters to know which list best contradicts it, so image I put on the list [kraken,scorpia,zulrah] so it will check the type of attack of each and would see would be the most effective type of attack for each and with that, I would receive a list of 3 bosses.

% boss(Name, Type) Name = Name of boss, Type = Attack type
boss(kraken,magic).
boss(scorpia,melee).
boss(zulrah,ranged).
boss(cerberus,melee).
boss(abyssal_sire,melee).
boss(obor,melee).
boss(sarachnis,ranged).
boss(skotizo,melee).
boss(venenatis,magic).

superEffective(magic,melee). %magic is more effective against melee
superEffective(ranged,magic). %ranged is more effective against magic
superEffective(melee,ranged). %melee is more effective against ranged
1

There are 1 answers

0
Leonardo Mariga On

First, create a predicate to verify the counter of a single Boss:

verify_con(Boss, ConBoss) :- boss(Boss,MainSkill), 
                             superEffective(MainSkill,ConSkill), 
                             boss(ConBoss,ConSkill), !.

Notice that this predicate will get always the first best counter to the input boss. If you want all the possible combinations, just delete the , ! at the end.

Second, use recursion to iterate over the input list and build the output list. You can use append/3 to append the output array.

verify_con_list([],[]).
verify_con_list([H|T], LIST) :- verify_con(H, ConBoss), 
                                verify_con_list(T, L1), 
                                append([ConBoss],L1, LIST).

If necessary, you can define the append/3 function at the top of your code like this:

append([], X, X).
append([H|T], X, [H|S]) :- append(T, X, S).

Examples

Single output:

?- verify_con(kraken, A).

A = scorpia 

List input:

?- verify_con_list([kraken, scorpia, zulrah], Con).

Con = [scorpia, zulrah, kraken]

Full code:

append( [], X, X).                        
append( [X | Y], Z, [X | W]) :- append( Y, Z, W).  

% boss(Name, Type) Name = Name of boss, Type = Attack type
boss(kraken,magic).
boss(scorpia,melee).
boss(zulrah,ranged).
boss(cerberus,melee).
boss(abyssal_sire,melee).
boss(obor,melee).
boss(sarachnis,ranged).
boss(skotizo,melee).
boss(venenatis,magic).

superEffective(magic,melee). %magic is more effective against melee
superEffective(ranged,magic). %ranged is more effective against magic
superEffective(melee,ranged). %melee is more effective against ranged

verify_con(Boss, ConBoss) :- boss(Boss,MainSkill), 
                             superEffective(MainSkill,ConSkill), 
                             boss(ConBoss,ConSkill), !.

verify_con_list([],[]).
verify_con_list([H|T], LIST) :- verify_con(H, ConBoss), 
                                verify_con_list(T, L1), 
                                append([ConBoss],L1, LIST).



%verify_con(kraken, A).
%verify_con_list([kraken, scorpia, zulrah], Con).