Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
:- dynamic colour/1. :- dynamic pegCount/1. :- dynamic turnCount/1. :- dynamic solution/1. :- dynamic guess/1. all_colours([]). all_colours([H|T]) :- colour(H), all_colours(T). get_guess(GuessNo,List) :- turnCount(Turns), write('Guess '),write(GuessNo),write(' of '),write(Turns),write(':'),nl, read(Input), atom_chars(Input,List), pegCount(Pegs), once( length(List,Pegs); ( write('Wrong number of pegs'),nl, fail ) ), once( once(all_colours(List)); ( write('Not all pegs are valid colours'),nl, fail ) ), once( \+guess(List); ( write('Guess has been made before'),nl, fail ) ), assertz(guess(List)). get_guess(GuessNo,List) :- write('Invalid Guess - Try Again'),nl, once(get_guess(GuessNo,List)). get_colours :- write('How many colours do you wish to play with (default is 6, enter from 2 to 10)?'),nl, read(Colours), integer(Colours), Colours >= 2, Colours =< 10, get_colours(Colours). get_colours :- write('Please enter an integer between 2 and 10'),nl, get_colours. get_peg_count :- write('How many pegs do you wish to play with (default is 4, enter from 2 to 8)?'),nl, read(Pegs), integer(Pegs), Pegs >= 2, Pegs =< 8, assertz(pegCount(Pegs)). get_peg_count :- write('Please enter an integer between 2 and 8'),nl, get_peg_count. get_turn_count :- write('How many turns do you wish to play with (default is 12, enter from 2 to 100)?'),nl, read(Turns), integer(Turns), Turns >= 2, Turns =< 100, assertz(turnCount(Turns)). get_turn_count :- write('Please enter an integer between 2 and 100'),nl, get_turn_count. get_colours(ColourCount) :- write('Please enter a character to represent each colour...'),nl, get_colours(1,ColourCount). get_colours(Current,Total) :- Current > Total. get_colours(Current,Total) :- write(Current),write('/'),write(Total),nl, read(Input), once( ( atom_chars(Input,[Colour]), \+colour(Colour), assertz(colour(Colour)), New is Current + 1, get_colours(New,Total) ) ; ( write('Please enter a single character that has not been used before'),nl, get_colours(Current,Total) ) ). get_solution(0,[]). get_solution(Count,[Head|Tail]) :- Next is Count - 1, get_solution(Next,Tail), findall(Colour,colour(Colour),Selection), random_member(Head,Selection). get_solution :- pegCount(Pegs), once(get_solution(Pegs,Solution)), assertz(solution(Solution)). compare_guess(Guess,(Blacks,Whites)) :- solution(Solution), once(get_white_count(Guess,Solution,Matches)), once(get_black_count(Guess,Solution,Blacks)), Whites is Matches - Blacks. get_white_count([],_,0). get_white_count([Head|Tail],Solution,Whites) :- select(Head,Solution,Remainder), get_white_count(Tail,Remainder,Partial), Whites is Partial + 1. get_white_count([_|Tail],Solution,Whites) :- get_white_count(Tail,Solution,Whites). get_black_count([],[],0). get_black_count([Head|Tail],[Head|STail],Blacks) :- get_black_count(Tail,STail,Partial), Blacks is Partial + 1. get_black_count([_|Tail],[_|STail],Blacks) :- get_black_count(Tail,STail,Blacks). start :- once(get_peg_count), once(get_turn_count), once(get_colours), once(get_solution), once(take_turn(0)). take_turn(Turn) :- turnCount(Turn), solution(Solution), write('I\'m sorry, please try again'),nl, write('The solution was '),write(Solution),nl. take_turn(Turn) :- NewTurn is Turn + 1, pegCount(Pegs), get_guess(NewTurn,Guess), ( ( compare_guess(Guess,(Pegs,0)), write('Congratulations!'),nl ); ( compare_guess(Guess,(Blacks,Whites)), write(Blacks),write(' colour'),(Blacks \= 1 -> write('s');true),write(' in the right place and '),write(Whites),write(' colour'),(Whites \= 1 -> write('s');true),write(' in the wrong place'),nl, take_turn(NewTurn) ) ).