# Solving mastermind with R

**R snippets**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In my last post I have shown a solution to classical sorting problem in R. So I thought that this time it would be nice to generate a strategy for playing Mastermind using R.

It was shown by D.E. Knuth that Mastermind code can be broken in at most five guesses. The algorithm is to always choose guess that *minimizes the maximum number of remaining possibilities*. Here is the R code that implements it.

The game is played using six colors and codes have length four. First part of the code prepares the data for calculations:

**<-**letters

**[**1

**:**6

**]**

**<-**as.matrix

**(**expand.grid

**(**set, set, set, set

**))**

**<-**

**function**

**(**hidden, guess

**)**

**{**

**<-**

**function**

**(**pattern

**)**

**{**

**(**set,

**function**

**(**x

**)**

**{**sum

**(**x

**==**pattern

**)**

**})**

**}**

**<-**sum

**(**full

**[**hidden,

**]**

**==**full

**[**guess,

**])**

**<-**sum

**(**pmin

**(**count

**(**full

**[**hidden,

**])**,

**(**full

**[**guess,

**])))**

**–**black

**(**black, white, sep

**=**“;”

**)**

**}**

**<-**mapply

**(**guessFit, rep

**(**1

**:**nrow

**(**full

**)**, nrow

**(**full

**))**,

**(**1

**:**nrow

**(**full

**)**, each

**=**nrow

**(**full

**)))**

**(**all.fit

**)**

**<-**c

**(**nrow

**(**full

**)**, nrow

**(**full

**))**

We want to prepare matrix all.fit of all possible guess-hidden code combinations in advance in order to avoid calling guessFit in the main algorithm (WARNING: it takes ~5 minutes on my laptop). Having generated it we can reference codes using their position (row number) in full matrix.

Now let us move on to the main function:

**<-**

**function**

**(**possible, indent

**=**1

**)**

**{**

**if**

**(**length

**(**possible

**)**

**==**1

**)**

**{**

**if**

**(**indent

**>**worst

**)**

**{**

**<<-**indent

**}**

**(**full

**[**possible,

**]**, “| *\n”

**)**

**(**1

**)**

**}**

**if**

**(**indent

**==**1

**)**

**{**

**(**“1: “

**)**

**}**

**<-**sapply

**(**1

**:**nrow

**(**full

**)**,

**function**

**(**guess

**)**

**{**

**(**table

**(**all.fit

**[**guess, possible

**]))**

**})**

**<-**which.min

**(**splits

**)**

**<-**split

**(**possible, sapply

**(**possible, guessFit,

**=**which.min

**(**splits

**)))**

**(**full

**[**best.guess,

**]**, “|”, length

**(**possible

**)**, “\n”

**)**

**for**

**(**i

**in**1

**:**length

**(**out.split

**))**

**{**

**if**

**(**names

**(**out.split

**)[**i

**]**

**!=**paste

**(**ncol

**(**full

**)**, 0, sep

**=**“;”

**))**

**{**

**(**indent

**+**1,“:”, rep

**(**” “, indent

**)**,

**(**out.split

**)[**i

**]**, “|”, sep

**=**“”

**)**

**(**out.split

**[[**i

**]]**, indent

**+**1

**)**

**}**

**}**

**}**

It recursively constructs the decision tree solving the game and outputs it using cat. At each level of the tree first number of the question asked is printed, next the chosen guess and finally either number of remaining options or a star * indicating a hit. Additionally in variable worst we keep the number of questions that have to be asked in the worst case.

Finally we run the prepared code:

**(**“rules.txt”

**)**# save output to a file

**<-**0

**(**1

**:**nrow

**(**full

**))**# this is slow: 2 minutes

**(**“\nQuestions in worst case:”, worst, “\n”

**)**

**()**

I redirect output to a file because the resulting tree is quite big (1710 lines) and we can actually see that the game can be solved in five questions in a worst case.

Finally – the code was prepared to make it easy to experiment with the code by changing number of colors and pegs only by changing variables set and full.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.