It's a standalone HTML file on GitHub
I included the CSS and JavaScript with the HTML so it would be easier to e-mail the game.
I'm most interested in comments about the solution-checking functions. This is the main function:
var evaluateSolution = function () {
//"In play" is the default state for the game.
var state = stateGame;
//Look for exact matches.
checkColorPositionMatches();
//If every peg matches exactly, the game was won
if (colorPositionMatches == codeLength) {
state = stateWin;
} else {
//Look for color matches among the unmatched pegs.
checkColorMatches();
state = stateLose;
}
//Reset the match state of each codePeg
unmatchCodePegs();
return state;
}
This one checks for exact matches.
var checkColorPositionMatches = function() {
colorPositionMatches = 0;
codePegLoop:
for (var codePegIdx=0;codePegIdx<codePegs.length;codePegIdx++) {
var codePeg = codePegs[codePegIdx];
//evaluate each gamePeg, stopping if we hit an exact match.
gamePegLoop:
for (var gamePegIdx=0;gamePegIdx<gamePegs.length;gamePegIdx++) {
var gamePeg = gamePegs[gamePegIdx];
if ((gamePeg.color == codePeg.color) &&
(gamePeg.position == codePeg.position)) {
gamePeg.colorPositionMatched = true;
codePeg.colorPositionMatched = true;
colorPositionMatches++;
break gamePegLoop;
}
}
}
}
This one checks for color-only matches.
var checkColorMatches = function() {
colorMatches = 0;
codePegLoop:
for (var codePegIdx=0;codePegIdx<codePegs.length;codePegIdx++) {
var codePeg = codePegs[codePegIdx];
//evaluate each unmatched game peg, recording matches as they occur
gamePegLoop:
for (var gamePegIdx=0;gamePegIdx<gamePegs.length;gamePegIdx++) {
var gamePeg = gamePegs[gamePegIdx];
if (gamePeg.color == codePeg.color &&
(gamePeg.colorMatched == false) &&
(codePeg.colorMatched == false) &&
(gamePeg.colorPositionMatched == false) &&
(codePeg.colorPositionMatched == false)) {
gamePeg.colorMatched=true;
codePeg.colorMatched=true;
colorMatches++;
break gamePegLoop;
}
}
}
}