% N queens
queens(N,L):- N>10, !, big_queens(N,L).
queens(N,L):- N>2,
	length(L,N),
	fd_domain(L,1,N),
	gui_show_values,
        gui_button(safe(L)),
        L=[X1|L2],
	last(L2,XN),
        gui_button(X1#<XN),
        gui_button(gui_trace_labeling(L)),
        gui_button((gui_trace_labeling(L),gui_show('+'),fail)).
	%gui_bagof_buttons(fd_labeling(X),member(X,L)). % bug bagof with FD variables in GNU Prolog <1.2.15

big_queens(N,L):-
	(N>127 -> fd_set_vector_max(N) ; true),
	length(L,N),
	gui_show_no_domain,
	fd_domain(L,1,N),
        gui_button(safe(L)),
        gui_button((fd_labeling(L,[variable_method(ff),value_method(middle)]),gui_show_values)).

 
safe([]).
safe([X| Y]) :-
        noattack(X, Y),
        safe(Y).
 
noattack(X, Xs) :-
        noattack(X, Xs, 1).
 
noattack(_, [], _) :- !.
noattack(X, [Y | Ys], Nb) :-
        X#\=Y,
        X#\=Y+Nb,
        X#\=Y-Nb,
        Nb1 is Nb+1,
        noattack(X, Ys, Nb1).  




