Skip to content

MasterMindSolver: Examples

Damian Monogue edited this page Apr 23, 2021 · 1 revision

Let's see this in action

Prerequisites

I used https://supermastermind.github.io/playonline/game.html as my test for the solver. The code below was written to play against that website and allow me to easily test. As a bonus it would show me how well the code was doing, and allowed me to verify that the number of potentially valid guesses left was correct.

Alias to tell the solver what the answer was for our last guess

This alias will work with the rest of the examples on this page, as they all use Msolver for the object variable.

Alias pattern: ^mr (\d+) (\d+)$

code:

local coloredPins = tonumber(matches[2])
local whitePins = tonumber(matches[3])
Msolver:checkLastSuggestion(coloredPins, whitePins)
echo(f"There are {Msolver.numberRemaining} possibilities left\n")
local guess = Msolver:getValidGuess(true)
display(guess)

Solver for 8 colors, 5 positions

Here I use the template feature so that it will send, for instance

push blue here
push blue here
push green here
push green here

For a guess if you set autoSend to on. If not, then it will return it as a table, { "push blue here", "push blue here", "push green here", "push green here" }

Alias pattern: ^mnew5$ is what I used but it doesn't really matter.

local mms = require("MDK.mastermindsolver")
local items = {
  "blue",
  "green",
  "red",
  "orange",
  "brown",
  "black",
  "white",
  "yellow"
}
Msolver = mms:new({
  places = 5,
  items = items ,
  allowDuplicates = true,
  template = "push |t here",
})
echo(f"There are {Msolver.numberRemaining} possibilities left\n")
local guess = Msolver:getValidGuess(true)
display(guess)

It will display the guess, once you have played the guess you respond with the answer. If it was 2 colored pins and 0 white pins, you would do mr 2 0 , 1 white pin and no colored pins? mr 0 1 etc. Then it will come back and give you the next guess to try, until you've solved the puzzle! Here's screenshots of one I played.

Solver for 6 colors, 4 positions

For this one, I just let it use the standard template, which is "|t", or the item on its own.

Alias pattern: ^mnew4$ is what I used but doesn't really matter

local mms = require("MDK.mastermindsolver")
local items = {
  "blue",
  "green",
  "red",
  "orange",
  "brown",
  "black",
}
Msolver = mms:new({
  places = 4,
  items = items,
  allowDuplicates = true,
})
echo(f"There are {Msolver.numberRemaining} possibilities left\n")

display(Msolver:getValidGuess(true))

And images of a game against the website. As you can see, it isn't always optimized.


\