1) (Previously proposed by Q. Dinh) add new conditions to the second clause so that it is only matched when the last position is reached:
furthest(Dir,State,Pos,Color,FurthestPos):-
getDisc(Pos,State,Color),
nextPos(Dir,Pos,NextPos),
furthest(Dir,State,NextPos,Color,FurthestPos).
furthest(Dir,State,Pos,Color,Pos):-
getDisc(Pos,State,Color),
nextPos(Dir,Pos,NextPos),
getDisc(NextPos,State,NextColor),
NextColor \= Color.
2) (If you are familiar with the "!" operator) add a cut "!" at the end of the first clause. This prevents prolog from backtracking after matching the first clause so it won't attempt to match the second clause:
furthest(Dir,State,Pos,Color,FurthestPos):-
getDisc(Pos,State,Color),
nextPos(Dir,Pos,NextPos),
furthest(Dir,State,NextPos,Color,FurthestPos),!.
furthest(Dir,State,Pos,Color,Pos):-
getDisc(Pos,State,Color).
Sorry for the confusion. The file othello.P
has now been corrected.