Skip to content

Instantly share code, notes, and snippets.

@mkf
Last active Dec 21, 2017
Embed
What would you like to do?
nth([],_,_) :- !, fail.
nth([X],0,X).
nth([_],0,_) :- !, fail.
nth([X|_],0,X).
nth([_|_],0,_) :- !, fail.
nth([_|Rest],Index,X) :- NewIndex is Index-1, nth(Rest,NewIndex,X), !.
onpos(B,[I],X) :- nth(B,I,X).
onpos(B,[I|R],X) :- nth(B,R,A), !, onpos(A,R,X), !.
%onpos(Board,[Rank, File],X) :- nth(Board,Rank,Elements), !, % if we have the nth rank already, there is no other nth rank in Board
% nth(Elements, File, X), !.
replaced([_|XT],[0],W,[W|XT]).
replaced([XH|XT],[N],W,[YH|YT]) :- I is N-1, replaced(XT,[I],W,YT), !.
replaced(X,[I|R],W,Y) :- !.
replaced(Before,Where,What,After) :- onpos(Before,Where,Discarded),
onpos(After,Where,What),
%boardsequalexcept(Before,After,Where), !.
replaced(After,Where,Discarded,Before), !.
swapped(X,[W,W],_,X).
swapped(X,FT,W,Y) :- swapped(Y,FT,W,X).
swapped(X,[F,T],W,Y) :- swapped(X,[T,F],W,Y).
swapped(Before,[One,Another],After) :- swapped(After,[Another,One],Before), !.
swapped(Before,[One,Another],After) :- onpos(Before,From,Our), onpos(Before,To,Another),
replaced(Before,From,Another,After), replaced(Before,To,Our,After), !.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment