% code common to star, similistar and starpalindrome collect_stars(Commands,Stars) :- once(collect_stars(Commands,0,0,UnorderedStars)), sort(UnorderedStars,Stars). collect_stars([] ,_,_,[]). collect_stars([up | Commands],I,J,Stars) :- J1 is J + 1, collect_stars(Commands,I,J1,Stars). collect_stars([down | Commands],I,J,Stars) :- J1 is J - 1, collect_stars(Commands,I,J1,Stars). collect_stars([left | Commands],I,J,Stars) :- I1 is I - 1, collect_stars(Commands,I1,J,Stars). collect_stars([right| Commands],I,J,Stars) :- I1 is I + 1, collect_stars(Commands,I1,J,Stars). collect_stars([star | Commands],I,J,[star(I,J)| Stars]) :- collect_stars(Commands,I,J,Stars). max_line([],Max,Max). max_line([star(_,L)|RestStars],MaxSoFar,Max) :- ( L > MaxSoFar -> max_line(RestStars,L,Max) ; max_line(RestStars,MaxSoFar,Max) ).