% x_won_fred(L) is true if L is a winning position for x in fred's
% representation
x_won_fred(L) <- member([x,x,x],L).
x_won_fred([[x,_,_],[x,_,_],[x,_,_]]).
x_won_fred([[_,x,_],[_,x,_],[_,x,_]]).
x_won_fred([[_,_,x],[_,_,x],[_,_,x]]).
x_won_fred([[x,_,_],[_,x,_],[_,_,x]]).
x_won_fred([[_,_,x],[_,x,_],[x,_,_]]).

% Example queries:
% ask x_won_fred([[x,o,o],[b,x,b],[x,b,o]]).
% ask x_won_fred([[x,o,o],[x,x,b],[x,b,o]]).

% x_won_jane(P) is true if P is a winning position for x in jane's
% representation
x_won_jane(ttt(Xpos,_)) <-
   member(pos(1,Row),Xpos) &
   member(pos(2,Row),Xpos) &
   member(pos(3,Row),Xpos).
x_won_jane(ttt(Xpos,_)) <-
   member(pos(Col,1),Xpos) &
   member(pos(Col,2),Xpos) &
   member(pos(Col,3),Xpos).
x_won_jane(ttt(Xpos,_)) <-
   member(pos(1,1),Xpos) &
   member(pos(2,2),Xpos) &
   member(pos(3,3),Xpos).
x_won_jane(ttt(Xpos,_)) <-
   member(pos(3,1),Xpos) &
   member(pos(2,2),Xpos) &
   member(pos(1,3),Xpos).

% Example queries:
% ask x_won_jane(ttt([pos(1,3), pos(2,2), pos(1,1)], 
%                     [pos(2,3), pos(3,3), pos(3,1)])).
% ask x_won_jane(ttt([pos(1,3), pos(2,2), pos(1,1), pos(1,2)], 
%                     [pos(2,3), pos(3,3), pos(3,1)])).

% x_won_harold(L) is true if L is a win in Harold's representation.
x_won_harold(L) <-
   position(x,L,P1) &
   position(x,L,P2) &
   P1 \= P2 &
   position(x,L,P3) &
   P1 \= P2 &
   P2 \= P3 &
   15 is P1 + P2 + P3.

position(C,[C|_],1).
position(C,[_|Y],P1) <-
   position(C,Y,P) &
   P1 is P+1.

% Example Queries:
% ask x_won_harold([b, o, b, o, x, x, o, x, b]).
% ask x_won_harold([x, o, b, o, x, x, o, x, b]).


% ALTERNATE SOLUTION FOR HAROLD
x_won_harold_2(L) <-
   find_pos_sums(3,x,1,15,L).

% find_pos_sums(N,What,Pos,Sum,L) is true if you can find N Whats in list L
% whose positions add up to sum, where Pos is the current position. 
find_pos_sums(1,W,Sum,Sum,[W|_]).
find_pos_sums(N,W,Pos,Sum,[W|R]) <-
   N>1 &
   Pos1 is Pos+1 &
   N1 is N-1 &
   Sum1 is Sum - Pos &
   find_pos_sums(N1,W,Pos1,Sum1,R).
find_pos_sums(N,W,Pos,Sum,[_|R]) <-
   Pos1 is Pos+1 &
   find_pos_sums(N,W,Pos1,Sum,R).

% Example Queries:
% ask x_won_harold_2([b, o, b, o, x, x, o, x, b]).
% ask x_won_harold_2([x, o, b, o, x, x, o, x, b]).

% x_won_jennifer(P) us true if P is a win for x in Jennifer's representation
x_won_jennifer(magic(Xpos,_)) <-
   member(P1,Xpos) &
   member(P2,Xpos) &
   P1 \= P2 &
   member(P3,Xpos) &
   P1 \= P2 &
   P2 \= P3 &
   15 is P1 + P2 + P3.

% member(E,L) is true if E is a member of list L
member(X,[X|_]).
member(X,[_|Y]) <-
   member(X,Y).

% Example queries:
% ask x_won_jennifer(magic([6, 5, 8], [7,2, 4])).
% ask x_won_jennifer(magic([1,6, 5, 8], [7,2, 4])).


% ALTERNATE SOLUTION FOR JENNIFER
x_won_jennifer_2(magic(Xpos,_)) <-
   find_sums(3,15,Xpos).

% find_sums(N,S,L) is true if you can find N numbers that sum to S in
% list L
find_sums(1,S,[S|_]).
find_sums(N,S,[M|R]) <-
   N>0 &
   S>M &
   N1 is N-1 &
   S1 is S-M &
   find_sums(N1,S1,R).
find_sums(N,S,[_|R]) <-
   find_sums(N,S,R).

% Example queries:
% ask x_won_jennifer_2(magic([6, 5, 8], [7,2, 4])).
% ask x_won_jennifer_2(magic([1,6, 5, 8], [7,2, 4])).
