/*
** Version of factorial with an if-then-else
*/
factorial1(N, F) :-
    ( N > 0 ->
         N1 is N-1, factorial1(N1, F1), F is F1 * N
    ;
         N =:= 0, F = 1
    ).

/*
** Tail recursive version of factorial with an if-then-else
*/
/* factorial(+N, -F) */
factorial2(N, F) :- factorial(N, 1, F).

factorial(N, Fin, Fout) :-
    ( N > 0 ->
         N1 is N-1, Fnext is Fin*N, factorial(N1, Fnext, Fout)
    ;
         N =:= 0, Fout = Fin
    ).


%%======================================================
%% Solution to the Eight Queens problem
%%======================================================

/* eightqueens(?Queens) */
eightqueens(Queens) :-
    Queens = [1/_, 2/_, 3/_, 4/_, 5/_, 6/_, 7/_, 8/_],
    qsafe(Queens).

qsafe([]).
qsafe([X/Y|Qs]) :-
    qsafe(Qs),
    % member(X, [1,2,3,4,5,6,7,8]),
    member(Y, [1,2,3,4,5,6,7,8]),
    no_attack(Qs, X/Y).

no_attack([], _).
no_attack([X1/Y1|Qs], X/Y) :-
    % X =\= X1,
    Y =\= Y1,
    abs(Y1-Y) =\= abs(X1-X),
    no_attack(Qs, X/Y).


%%===========================================================================
%% Solution to the zebra puzzle (https://en.wikipedia.org/wiki/Zebra_Puzzle)
%%===========================================================================

/* house(Nationality, Color, Pet, Smoke, Drink) */

drinks_water(Nationality) :- zebra(Houses), member(house(Nationality, _, _, _, water), Houses).
owns_zebra(Nationality) :- zebra(Houses), member(house(Nationality, _, zebra, _, _), Houses).

zebra(Houses) :-
    Houses = [house(norwegian, _, _, _, _), house(_, blue, _, _, _), house(_, _, _, _, milk), _, _],
    member(house(englishman, red, _, _, _), Houses),
    member(house(spaniard, _, dog, _, _), Houses),
    member(house(_, green, _, _, coffee), Houses),
    member(house(ukranian, _, _, _, tea), Houses),
    rightof(house(_, green, _, _, _), house(_, ivory, _, _, _), Houses),
    member(house(_, _, snails, old_gold, _), Houses),
    % member(house(_, yellow, _, kools, _), Houses),
    nextto(house(_, _, _, chesterfields, _), house(_, _, fox, _, _), Houses),
    nextto(house(_, yellow, _, kools, _), house(_, _, horse, _, _), Houses),
    member(house(_, _, _, lucky_strike, orange_juice), Houses),
    member(house(japanese, _, _, parliaments, _), Houses).

rightof(R, L, [L,R,_,_,_]).
rightof(R, L, [_,L,R,_,_]).
rightof(R, L, [_,_,L,R,_]).
rightof(R, L, [_,_,_,L,R]).

nextto(H1, H2, Houses) :- rightof(H1, H2, Houses).
nextto(H1, H2, Houses) :- rightof(H2, H1, Houses).


%%===========================================================================
%% Winning Tic-Tac-Toe configuration
%%===========================================================================

winner(L) :- L = [_, _, _], triple(L, T), is_winning_triple(T).

triple(L, T) :- member(T, L).				% gets the rows
triple(L, T) :- transpose(L, LT), member(T, LT).	% gets the columns
triple([[A,_,_], [_,E,_], [_,_,I]], [A,E,I]).
triple([[_,_,C], [_,E,_], [G,_,_]], [C,E,G]).

transpose([[A,B,C],[D,E,F],[G,H,I]], [[A,D,G],[B,E,H],[C,F,I]]).

is_winning_triple([x,x,x]).
is_winning_triple([b,x,x]).
is_winning_triple([x,b,x]).
is_winning_triple([x,x,b]).

