Make sure you start by running the Mastermind Template program. This plays the game, but just does random guesses. Your job is to make it “smart”. You will do this by making only GOOD guesses. The place to start is to make a function that returns the feedback if given 2 codes, in terms of a’s and z’s. Check this starter function:
http://ada.evergreen.edu/~vanetta/mmmatchcount.html
Adapt this code so you can send every code, one at a time from your nested for loops. For each possible code, check to see how this code systematicly produced and your first guess match for the input from the user (f1) and the code returned from your function. If it is a match, exit your loops and this will become a good guess. For guess 3, it will be almost the same, except the systematically produced code must match guess 1 with f1 AND guess 2 and f2. The 4th guess will have 3 checks, and so on.
Steps: 1. Run the code link above for returning the code from 2 guesses. Paste into Notepad. Study
2. Change this function so that instead of counting how many matches there are in the 2 passed in codes, it makes a string (called, say, val) that returns an ‘a’ for every match. Save this as version 2
3. When you find a match, change the code.charAt at that position to a, say ‘x’, and the guess.charAt to a , say ‘q’ so you do not find other matches later. Save as version 3.
4. Now try to produce the ‘z’ codes for val. Start by checking the first letter of code (use charAt(0) ) agains every position in the guess string. If you find a match, add a ‘z’ to val.
5. Now, use 2 for loops to check every position for code against every position of guess. Append ‘z’ to val for every match. When you find a match, change the charAt that postion in code to an x and a q for guess so you dont count twice.
6. Check your function over and over to make SURE it works.
7. If you are unable to get this to work over the weekend, make sure you can show me what you HAVE done, and have a very targeted question on what you tried and what you cannot do.
8. Once you have this function working, work on Move2().. Now it just makes a random guess and returns this guess. You want to change it so that it make a GOOD guess. It is a good guess if the guess gives you the same return (f1) as you get when you compare it to your first guess (g1). You can do this by calling the function above and then comparing what is returned to f1. If it is a match, exit your 4 loops (as in the Starter code for May 8) and return this string as your guess. Otherwise, you loop will make the next guess
EXTRA:
Your program should get the code in between 4 and 6 guesses. Once this works, here are some extensions:
1. Use loops and arrays so you only need one function for a good guess
2. Make a better user interface using buttons and colors
3. Upgrade your code so you have 6 digits in your code and 8 colors
4. See me for further extensions