word([a,b,i,l,i,t,y]).
word([a,b,o,v,e]).
word([a,g,e,n,t]).
word([b,e,n,e,f,i,t]).
word([b,e,s,t]).
word([b,e,t,t,e,r]).
word([b,e,t,w,e,e,n]).
word([b,e,y,o,n,d]).
word([c,e,l,l]).
word([c,e,n,t,e,r]).
word([c,e,n,t,r,a,l]).
word([c,e,n,t,u,r,y]).
word([c,e,r,t,a,i,n]).
word([d,e,a,t,h]).
word([d,e,b,a,t,e]).
word([d,e,c,a,d,e]).
word([d,e,c,i,d,e]).
word([d,e,c,i,s,i,o,n]).
word([d,e,e,p]).
word([e,a,c,h]).
word([e,a,r,l,y]).
word([e,a,s,t]).
word([e,a,s,y]).
word([e,a,t]).
word([z,e,b,a]).
word([z,e,t,a]).






play :- write("hangMan: The Jig is up, Time to play! What's your word?"), nl,read_word_list(Input), exit_check(Input).

play1(State):- write("guessing"),nl,letterIsPossible(Y,State),random_member(X,Y),write(" I pick the letter "),write(X),UsedLetters = [X],write(" Is it in the word?"),read_word_list(Ans),playing1(State,X,UsedLetters,Ans).

playing1(State,Letter,UsedLetters,Ans):- (Ans == [y,e,s]) -> nl,write("Whats its spot in the word? enter a number"),read_word_list(Position),trimState(State,Position,_,1,Letter,UsedLetters);!,trimState2(State,NState,1,Letter,UsedLetters),nl,write("Ouch, ima guess agian"),play2(NState,UsedLetters).


positionCheck(State,OgLetter,UsedLetters):- nl,write("Is there another spot with this letter ?"),read_word_list(Ans),exit_check1(Ans,Ans1),(Ans1 == [y,e,s]) -> nl,write("Whats its spot in the word? enter a number"),read_word_list(Position),trimState(State,Position,_,1,OgLetter,UsedLetters);!,solveCheck(State) -> play2(State,UsedLetters);!,fail.

solveCheck(State):-length(State,Length),(1==Length)->
write("Is your word "),write(State),write("?"),read_word_list(Ans),exit_check1(Ans,Ans1),(Ans1 == [y,e,s]) ->
write("would you like to play agian?"),read_word_list(Ans2),exit_check1(Ans2,Ans3),exit_check2(Ans3,Ans4),(Ans4 == [y,e,s])->
      play;!,true;!,abort.

play2(State,UsedLetters):- nl,write("guessing"),letterIsPossible(Y,State),subtract(Y,UsedLetters,L),random_member(X,L),nl,write(" I pick the letter "),write(X),write(" Is it in the word? "),read_word_list(Ans),exit_check1(Ans,Ans1),playing2(State,X,L,Ans1).

playing2(State,Letter,UsedLetters,Ans):- (Ans == [y,e,s]) -> nl,write("Whats its spot in the word? enter a number"),read_word_list(Position),trimState(State,Position,_,1,Letter,UsedLetters);!,trimState2(State,NState,1,Letter,UsedLetters),nl,write("Ouch, ima guess agian"),play2(NState,UsedLetters).

makeList(Input):-length(Input,Length),word(X),length(X,LengthX),
    State = [],(Length==LengthX,(\+member(X,State))) -> (append(State,[X],NState), mL2(Input,NState)); !, fail.

mL2(Input,State):- length(Input,Length),word(X),length(X,LengthX),(Length== LengthX,(\+member(X,State))) -> (append(State,[X],NState), mL2(Input,NState)); !, play1(State).

exit_check2(Input,Output) :-
    (Input == [n,o]) -> abort;
    !,Output = Input.

exit_check1(Input,Output) :-
    (Input == [e,x,i,t]) -> abort;
    !,Output = Input.

exit_check(Input) :-
    (Input \== [e,x,i,t]) -> dic_Check(Input);
    !, abort.

dic_Check(Input) :- word(X),
    (Input == X) -> makeList(Input);
    !,write("your word isn't in the Dictionary, try agian"),play.

read_word_list(Ws) :-
    read_line_to_codes(user_input, Cs),atom_codes(A, Cs),atom_chars(A,Ws).


trimState2(State,Ns,Index,Letter,UsedLetters):- length(State,X),\+(Index=<X)-> Temp = [],append(Temp,State,Ns);!,nth1(Index,State,Elm,Rest),member(Letter,Elm)->(N is Index+1),trimState2(Rest,Ns,N,Letter,UsedLetters);!,(N is Index+1),trimState2(State,Ns,N,Letter,UsedLetters) .

trimState(S,Position,Ns,IndexinS,OgLetter,UsedLetters):- (nth1(1,Position,X),atom_number(X,P),nth1(IndexinS,S,Elm),nth1(P,Elm,Letter),(Letter == OgLetter)) ->(NIndexinS is IndexinS+1),append(Ns,[Elm],NNs),trimState(S,Position,NNs,NIndexinS,OgLetter,UsedLetters) ;!,length(S,Length),(IndexinS<Length),(1\=Length)->(NIndexinS is IndexinS+1),trimState(S,Position,Ns,NIndexinS,OgLetter,UsedLetters);!,positionCheck(Ns,OgLetter,UsedLetters). %%finish the figuring out trimming of the state

letterIsPossible(Y, State) :-
  flatten(State,X),sort(X,Y) .
