Skip to content

Mastermind Tutorial

SpeshulK edited this page Dec 9, 2013 · 3 revisions

Mastermind Description

Mastermind is a childhood game I played as a kid in which you must guess a sequence of four colors set up by the another player, otherwise known as the "Mastermind". More information for this game can be found on [http://en.wikipedia.org/wiki/Mastermind_(board_game)](the Wikipedia page.)

Now, how would we implement this game in Java? It is actually quite simple. And Even more simple, if you use recursion!

For starters, we need to set up how the game will work. For our version of the game, we will be creating an answer key of four numbers, ranging from the digits 0-9. That means our possible answer will look something like this:

8033
1234
9999

And so on and so forth. We have a range of 1000 different combinations.

Now, to create the original numeral string, it is as simple as calling Math.random() four times.

#####String createGame() We will need to create a loop that runs four times that:

  • call Math.random() and saves it to a variable(double)
  • compare the variable using if statements that holds all possible numeric answers(digits 0-9)(Hint: Math.random() returns a decimal number between 0 and 1.)
  • save the answer to a space in the array(array size equal to the number of digits)
  • The answer should be saved as an array of Strings. This will be important for comparisons.

And that's how to create the match. This should be done in its own method.

Before we go any farther, there are a few global variables we should instantiate. We need:

  • Something to hold the number of turns the user takes to guess
  • A CharSequence array that holds your answer, used for comparisons. (In our game, the array size will be four.)
  • A String to hold the answer (set to empty)
  • A String to hold the hint that is printed after the guess(set to empty)
  • A boolean to tell the player has guessed correctly.

Now, to play the game we must check our answer against the series of numbers. You will need only one parameter in your helper method(that is, the method the main method will call), and that is the guess that is put into the method.

#####String check For the recursive method, there are four parameters you should use. They are:

  • The string the player has guessed
  • An array of CharSequences that hold the answer numbers
  • A String that is your completed answer
  • An int that represents the place in the String/CharSequence you're checking

Our base cases will be when the player's string matches the answer string, as well as when the int that represents the place in the CharSequence matches the length of the charSequence. If the game is over, add to the number of turns and print the number of turns taken. In the other case, add to the number of turns and return the hint.

Now, for the comparisons:

if a substring of the player's guess(found with the int) matches the number in the same spot in the array
   concatenate an affirmative "correct number and correct placement" token to the hint
else if the player's guess contains the number in the array at place int
   concatenate an affirmative "correct number and incorrect placement" token to the hint

A few hints:

  • a while loop will keep the game running until the game is over
  • Scanner is, of course, what you use to take in the answer
  • You can use numbers or colors, if you want to be closer to the physical game.

Clone this wiki locally