From UBC Wiki

Authors: Irene Chen, Stephanie Lam, Michael Spearman

What is the problem?

Mastermind is a classic two-player game. Six types of pegs are available in this game: white, black, blue, yellow, green, and red. One player acts as the codemaker, while the other takes the role of the codebreaker. The codemaker picks four pegs of any color, lines them up in a particular order, and keeps the order of the colored pegs as a secret code. The codemaker may pick more than one peg of the same color. The codebreaker's objective is to guess the secret code (in its correct order) in as few guesses as possible.

For each of the codebreaker's guesses, the codemaker will tell the codebreaker how many pegs of the correct color the codebreaker guessed in the correct spots as well as how many pegs of the correct color the codebreaker guessed in incorrect spots. For example, if the codemaker selects "Black Yellow Black Yellow" as the secret code, and the codebreaker guesses "Black Black White Yellow," the codebreaker will be told that two pegs are correct colors in the correct spots and one peg is the correct color but in the wrong spot.

We propose making a Mastermind solver in Prolog.

What is the something extra?

Our implementation of the Mastermind solver will have two main functionalities:

  • Given two combinations of pegs with one combination representing the secret code and the other representing a guess, output how many correctly-colored pegs are in correct spots and how many corectly-colored pegs are in incorrect spots.
  • Given one combination of pegs, output how many combinations of pegs the program tried before finding the correct combination. Or, if a number is also given as a parameter, output whether the program can find the secret code within the given number of guesses.

What did we learn from doing this?

We learned how to use difference lists to pass parameters around and how to use lists to hold parameters. This is especially apparent in our evaluate(Guess,Actual,Response) function: we use many "accumulator" variables (such as TR1 and TR2 for counting pegs) to pass information between different helper functions. In addition to this, we also learned to leverage Prolog's declarative programming style to solve problems using brute force techniques.

Logic programming doesn't seem suitable in the case of determining how many tries it would take to find the correct answer given that we used brute force rather than logic. The number of tries was simply how far down the list the given pattern was and it did not change based on the responses of the previous guesses.