Is there any specific way to solve Mastermind?
Apart from the first step that is pure chance, is there any way to continue based on the colors that you think are correct?
Wikipedia has the nice section on optimal Mastermind strategies:
MathWorld's page on Mastermind also gives a nice synopsis and mention a few more strategies:
In summary, there is a trade-off to make between the average length and the maximum length of the game. (length is expressed in the number of code guesses)
I play Mastermind with numbers instead of colours, because I first learned it in the second grade as Bagel Pico Fermi which uses numbers. For the rest of my answer, I will refer to red pegs as "bagels", and white pegs as "picos" (and holes without pegs as "fermis").
The system I tend to use is suboptimal but very easy to follow. It goes as follows:
An example of this algorithm at work might be as follows:
This isn't as good as the algorithm that a computer would use, but it's very simple and systematic, and very easy to get the hang of once you understand what you're doing.
A simple strategy which is good and computationally much faster than Knuth's is the following (I have programmed both)
Create the list 1111,...,6666 of all candidate secret codes
Start with 0011.
Repeat the following 2 steps:
1) After you got the answer (number of red and number of white pegs) eliminate from the list of candidates all codes that would not have produced the same answer if they were the secret code.
2) Pick the first element in the list and use it as new guess.
This requires in general no more than 5 guesses.
This is the Swaszek (1999-2000) strategy that was mentioned in another answer.
It may not be the fastest technique, but I generally take the trivial solution of starting with a row with all one color. This tells me how many of that color exist in the solution.
If none, I simply move on to another color. If one or more match, I leave that many of the color and move on to the next color for the remaining spaces. Using this technique it will determine the exact set of colors within 6 moves, but not the order.
While I am working on that technique, I also start working on determining position by swapping positions to rule out possible positions for each color. This is where the technique I use gets a bit more complicated.
I look carefully at the previous combinations and select combinations that will eliminate positions for certain colors based on whatever output I get, for example, placing colors I know are not present (or already know the position of) to blank portions of the board. For each color I lock in, the number of possible guesses is reduced on future guesses.
If possible, I will also try solving more than one peg simultaneously by leaving one in place and moving the other. If I don't lose a black peg, then I know that the one I didn't move was correct, if I gain a black peg, I know that both are correct. If I lose a black peg, I know that the one I moved was correct.
Using this technique, I can reliably win pretty much any mastermind game, though I do sometimes use most of my guesses, so it isn't the most efficient solution out there.
I made a custom strategy for solving with the original rules - 6 colors and 4 pegs code length with 10 guesses allowed. My solution is guaranteed to solve in 9 guesses.
Suppose the colors are red, green, yellow, orange, purple, and violet. Start by putting 1 color in the first 2 holes then another color in the second 2 holes.
Example - RRGG
Now make another guess with 2 colors that haven't been used the next guess must follow the same pattern. So after 3 guesses we will have used all the colors. So in my solution the first 3 guesses would go something like this:
guess 1 - YYOO
Up until now we have ignored the clue pegs - the first 3 guesses always use the same pattern. Now here's where things get a little complicated.
Our goal is to figure out what colors the first 2 pegs of the code are and what color the other 2 pegs are. I call these the front and back pegs respectively. After that we can settle the final positions in at most 3 guesses. So after making the guesses above take the guess marked with the most clue pegs (if it is a tie you can choose) and replace the back pegs with a color you know is NOT in the code then make this your new guess. Now there are 2 ways find a color not in the code. If one of the first 3 guesses has no clue pegs then none of those colors are in the code. The second method requires making a guess. If the first 3 guesses all have clue pegs choose the guess marked only by one clue peg - this is always possible! Now choose a color in that guess and simply repeat it. Now note the clue peg if there is none that color is not in the code otherwise if you still get a clue peg then the other color is not in the code. Now you can continue. Since this sounds more complicated then it is I will show examples from playing against the computer. The colors the computer uses are: blue,green,orange,purple,red,yellow. Bu stands for blue.
guess 1 - BuBuGG; no clue pegs
guess 1 - BuBuGG; 0 black 1 white
guess 5 - OOGG; 0 black 0 white - Since guess 2 has the most clue pegs we modify it by putting green in the last 2 holes. Green is used because we know it is not in the code. From this we can see that orange is not in the code either. If we look back at guess 2 we can alse there are 2 purple pegs and that both must go in the front 2 holes. So now the code is: PPBux Looking at guess 3 the final color must be red. The clue tells 1 peg is out of position. Since the code has only 1 spot left in the back the missing peg must be red. So now we have: PPBuR which is used as our next guess.
guess 6 - PPBuR solved - if we had gotten 2B 2W it would have been PPRBu
guess 1 BuBuGG; 2 black 0 white
Important! In the end game there are different ways to settle the final positions of the pegs. Once you know what pegs go in the front and back there is a pattern you can follow:
PRGY; 2 black 0 white
Basically if you get 4 white swap the front 2 pegs then the back 2 pegs as shown above. If you get 2 black swap the pair in the back and that will either solve or give you 4 white.
I think it is possible to solve all puzzles (of four unique colors) at five tries. The best way to start is picking red, green, blue, yellow. Then simply stick to those colors, starting with red... say you get three white markers after the first round, simply keep red, green, blue, but move all of them one space to the right, and fill in the last space (space #1) with the first of the remaining colors, brown. Continue doing this, systematically, and you will hit the right code within five tries.