I have a simple predicate in prolog that when MaxScore >0, it gives yes, otherwise is no. So if MaxScore is 0, then obviously the result is No. But this doesn’t happen. Look closer at this: 
Or corresponding code:
aiPlay(PlayerNum, Board, Pos, BotType) :-
aiTryAll(Board, PlayerNum, ScoreList, 0),
scoreListGetPos(ScoreList, 1, Pos, MaxScore),
write('MaxScore: '), write(MaxScore), nl,
MaxScore > 0.
And corresponding output:
| ? myQuery(Pos).
MaxScore: 0
MaxScore: 0
MaxScore: 1
Pos = 6 ?
yes
| ?
I am getting insane with this. Anyone have a clue about what is happening?
There isn’t really a problem; consider this code:
Prolog will first unify
Xwith0(from the firstfoo(0)), will print the output and then fail since0 > 0is false. Then prolog will backtrack and pick the secondfoo(0); again the same. Finally,Xwill be unified with1andbarwill returntrue.Naturall, side-effects like printing cannot be reversed/backtracked, so in the terminal you will see:
How you solve it depends on what you want to do; if you want to print just
Max Core = 1then you can do the printing after you check ifMaxScore>0. Generally though, it’s better to do the printing (if it’s required) at the very end, and not in a predicate deep in the program.Oh, by the way, it’s better to post the actual code and the output instead of a screenshot; it makes it easier to copy-paste-debug your code. Also, it is advised to give a runnable example: half of the predicates you use in
aiPlay/4are undefined so I’m just guessing that the problem is what I mentioned above :p