Last active
April 13, 2016 01:55
-
-
Save shduff/654d40a02e36a6faa3703d54b4fc36e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title> | |
mastermind | |
</title> | |
</head> | |
<body> | |
<script> | |
// chose some random browser recognized colors to standin as our color options | |
var colors = ['lightgray', 'lightblue', 'lightgreen', 'pink', 'lightyellow', 'lightpurple']; | |
// created a dictionary to represent the code made by the codemaker for this round. change out the values to test different codes. | |
var code = { | |
'pos1':colors[0], | |
'pos2':colors[3], | |
'pos3':colors[3], | |
'pos4':colors[5] | |
} | |
// created a dictionary to represent the code being guessed by the codebreaker this turn. change out the values to test different codes. | |
var test = { | |
'pos1':colors[5], | |
'pos2':colors[0], | |
'pos3':colors[3], | |
'pos4':colors[4] | |
} | |
// grabbing a few useful bits of information in global variable for later use | |
var pegPositions = Object.keys(code); | |
var numberOfPegs = pegPositions.length; | |
// this function aims to count how many exact matches (e.g. correct color in the correct position) were guessed. | |
var countingExactMatches = function(codeDict, testDict) { | |
// it does this first by creating a counter and setting it equal to 0 | |
var exactMatchCount = 0; | |
// then, it iterates through each peg position (pos1, pos2...) | |
pegPositions.forEach( function(element) { | |
// and checks to see if the color assigned to a position is the same in the test and code dictionaries | |
if (codeDict[element] == testDict[element]) { | |
// if it is, then it increases our counter by one | |
exactMatchCount++; | |
} | |
}); | |
// and, in the end, it returns the total count of exact matches | |
return exactMatchCount; | |
} | |
// this function is meant to count all the fuzzy matches between the test and code dictionaries (e.g. correct colors placed in the wrong positions) | |
var countingFuzzyMatches = function(codeDict, testDict) { | |
// first, we are going to create a counter and set it to 0, this counter will addcount up all the colors from our test code which occur in the original code. this count will include exact matches as well as fuzzy matches | |
var allMatchingColorsCount = 0; | |
// next, we'll create some blank dictionaries | |
var codeCountDict = {}; | |
var testCountDict = {}; | |
// mapping each of our colors to 0 to start | |
colors.forEach(function(element) { | |
codeCountDict[element] = 0; | |
testCountDict[element] = 0; | |
}); | |
// now that we have that infrastructure, we will actually count how many times each of the colors occurs in the original code ("codeDict") | |
pegPositions.forEach(function(element) { | |
var codeColor = codeDict[element]; | |
codeCountDict[codeColor]++; | |
// as well as in the code we are testing ("testDict") | |
var testColor = testDict[element]; | |
testCountDict[testColor]++; | |
}); | |
// at this point, we have two dictionaries, "codeCountDict" and "testCountDict" which contain a count of how many times each color is used in each code. | |
// now we are going to iterate through all of our colors and check how many times | |
colors.forEach(function(element) { | |
if (testCountDict[element] <= codeCountDict[element]) { | |
// if the color occurs in "testDict" an equal or fewer number of times as compared with "codeDict", then we add the count present in testCountDict, as that is the more conservative count | |
allMatchingColorsCount += testCountDict[element]; | |
} else { | |
// if it occurs *more* times in "testDict" than in "codeDict", however, then we return the number from codeCountDict, as that is the more conservative count | |
allMatchingColorsCount += codeCountDict[element]; | |
} | |
}) | |
// now, we have a count (in "allMatchingColorsCount") of *all* matches, but we want to separate out only the fuzzy matches. we'll do this by subtracting the exact matches from the total matches | |
var fuzzyMatchCount = allMatchingColorsCount - countingExactMatches(code, test) | |
// and finally, we return the fuzzy matches! | |
return fuzzyMatchCount; | |
} | |
// this function is the final executable that will tell us how many exact matches, fuzzy matches, and not-at-all matches we have between our "codeDict" and "testDict" | |
var checkMyCode = function(codeDict, testDict) { | |
// we calculate exact matches using the function we wrote earlier | |
var exactMatchesCount = countingExactMatches(codeDict, testDict); | |
// we calculate fuzzy marches using the function we wrote earlier | |
var fuzzyMatchesCount = countingFuzzyMatches(codeDict, testDict); | |
// and we calculate the not-at-all matches by subtracting the matches we've found from the total number of pegs the game was played with | |
var notAtAllMatchesCount = numberOfPegs - ( exactMatchesCount + fuzzyMatchesCount ); | |
// then, we package these values into a nice little dictionary | |
var userReport = { | |
'exactMatches' : exactMatchesCount, | |
'fuzzyMatches' : fuzzyMatchesCount, | |
'notAtAllMatches' : notAtAllMatchesCount | |
}; | |
// and then we return our dictionary of values (and print them out so we can see them) | |
console.log(userReport); | |
return userReport; | |
} | |
// by actually running that final function, we can see the results of this script in the browser console | |
checkMyCode(code, test); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment