- This implementation of mastermind contain two players i.e.
ComputerPlayerandHumanPlayer - You are the
HumanPlayer - You can choose to be either code breaker or code creator
- If you choose to be code guesser then
ComputerPlayerwill create code and you'll get11tries to guess the code. - If you can guess the secret code in
11tries then you win the game and the game will end with a winning message. - If you can't guess the code in
11tries you lose the game and the game will end with a losing message. - If you choose to be the code creator, then write your code on a piece of paper.
ComputerPlayerwill start guessing your code and everytime Computer makes a guess, look at your code(where you wrote it in step 7) and provide feedback.ComputerPlayerwill work under the assumption that you'll always provide correct feedback.ComputerPlayerwill always win and will be able to guess the code in atmost 5 tries. Provided that the feedback you provide is correct.- It uses Knuth's algorithm to guess it in atmost 5 tries. More details on the algortihm are here
Click to know about How to Play Mastermind
If you want to be the code creator then press 2. Write your code on paper and every time ComputerPlayer make a guess look at your code and provide feedback. Take some time but do provide correct feedback. In the above GIF I created the code ["Red", "Blue", "Green", "Yellow"]. I kept on providing feedback to Computer based on this code and the ComputerPlayer was able to guess the original code in 4 tries.
If you want to be the code breaker then press 1. Provide the guess in the format specified in the above peg.
To Play the mastermind :
- Go to this link.
Clickon the green Fork button in the top upper right corner.Selectblue Fork repl button.- Now
Clickthe green Run button. - You'll be prompted to choose between
code creatororcode breaker - Enter
1if you want to guess the code created by theComputerPlayer - Enter
2if you want theComputerPlayerto guess your secret code - KEEP REPEATING AND HAVE FUN!!
-
Implementing the functionality for
white feedback pegwas difficult. Forred feedback pegit was way easier, just check the corresponding pegs in bothcodeandguessand if they are same then increment a counter variable for it. I ended up usingred peg feedbackdata and methodmax_peg_countto indirectly get get thewhite feedback data. -
Whle implementing Knuth's Mastermind Algorithm to remove codes from the
setof all possible 1296 codes, I was usingeachmethod onsetwhile simultaneously updating mysetwhich led to my possible codes leakage.
-
Always write pseudocode first. It saves time and helps to find fault in your logic. You may have a strong temptation to just write the code directly but refrain from doing so. Write pseudocode and verify it against some examples. Once you have done that you can be sure that whatever error comes now is an implementation problem.
-
Don't think about refactoring and writing new piece of functionality together. Focus on one thing at a time.
-
Always create a functionality outline via functions only then begin to fill those functions. The other way around is too confusing for me. Writing individual functions first and then thinking about how to piece them together to achieve the desired functionality wastes too much time and it doesn't give a good feel of
data flow. -
Tests are awesome. Everytime I wrote a functionality, I feared that it might break something. I had to check if everything was working manually which took too much time. So why didn't I write tests?? I haven't studied them in
Rubyyet but had prior exposure to them inJava.

