Skip to content

Instantly share code, notes, and snippets.

@simonh1000
Last active December 22, 2019 21:47
Show Gist options
  • Save simonh1000/607c6a58806dc872f606 to your computer and use it in GitHub Desktop.
Save simonh1000/607c6a58806dc872f606 to your computer and use it in GitHub Desktop.
Prolog implementation of Mastermind game
%% Provide with a Goal and it will iterate towards it.
guess(Goal, Res) :-
guessIterate(Goal, [], Res).
guessIterate(Goal, Guesses, Res) :-
makeGuess(Guess),
check_against_guesses(Guess, Guesses),
score(Guess, Goal, ThisScore),
(
%% if completely correct,...
ThisScore == [4, 0]
->
Res = Guess
;
format("~w ~w\n", [Guess, ThisScore]),
guessIterate(Goal, [guess(Guess, ThisScore) | Guesses], Res)
).
/*
If this Guess is correct, it will pedict the right results for previous guesses
*/
check_against_guesses(_, []).
check_against_guesses(Guess, [guess(Code, Score) | Guesses_tail]) :-
score(Code, Guess, Score),
check_against_guesses(Guess, Guesses_tail).
makeGuess(Guess) :-
Colours = [red, blue, green, yellow, orange, purple, pink, crimson],
length(Guess, 4),
maplist(list_member(Colours), Guess).
list_member(Ls, M) :- member(M, Ls).
/*
score takes a guess and the goal and returns [#Black, #White], where
Black means correct colour and position;
White means correct colour but wrong positions.
It first looks for exact matches, and replaces matched elements in the gloa with 'black'
Then look for inexaxct matches and replace wiht white.
Then count up Blacks and Whites in final results
It is not possible for a guess of e.g. [...red, red...] to match twice with a
single 'red' in the Goal
*/
score(Guess, Goal, Res) :-
pass1(Guess, Goal, [], Pass1Res),
pass2(Guess, Pass1Res, Pass2Res),
count_up(Pass2Res, Res).
pass1(_, [], Acc, Acc_reversed) :-
reverse(Acc, Acc_reversed).
pass1([Guess_head | Guess_tail], [Goal_head | Goal_tail], Acc, Res) :-
(
Guess_head == Goal_head
->
pass1(Guess_tail, Goal_tail, [black | Acc], Res)
;
pass1(Guess_tail, Goal_tail, [Goal_head | Acc], Res)
).
%% check each Guess element with Goal and change Goal element to 'white' if there is a match
pass2([], Goal, Goal_reversed) :-
reverse(Goal, Goal_reversed).
pass2([Guess_head | Guess_tail], Goal, Res) :-
pass2Helper(Guess_head, Goal, [], ModifiedGoal),
pass2(Guess_tail, ModifiedGoal, Res).
pass2Helper(_, [], Acc, Acc_reversed) :-
reverse(Acc, Acc_reversed).
pass2Helper(Guess, [Goal_head | Goal_tail], Acc, Res) :-
(
Guess == Goal_head
->
reverse(Goal_tail, Goal_tail_reversed),
append(Goal_tail_reversed, [white | Acc], Res)
;
pass2Helper(Guess, Goal_tail, [Goal_head | Acc], Res)
).
count_up(Result_of_scoring, [Blacks, Whites]) :-
count_colour(white, Result_of_scoring, Whites),
count_colour(black, Result_of_scoring, Blacks).
count_colour(Colour, Input, Count) :-
include( =(Colour), Input, Out),
length(Out, Count).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment