Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created March 19, 2013 02:18
Show Gist options
  • Save charlespunk/5193204 to your computer and use it in GitHub Desktop.
Save charlespunk/5193204 to your computer and use it in GitHub Desktop.
The game of Master Mind.
public Result estinate(char[] guess, char[] solution){
int hit = 0;
int pesudoHit = 0;
int[] frequencies = new int[MAX_COLOR];
/* compute the hit number and build frequencies table for pesudoHit usage */
for(int i = 0; i < guess.length; i++){
if(guess[i] == solution[i]) hit++;
else frequencies[code(solution[i])]++;
}
/* compute pesudoHit number */
for(int i = 0; i < guess.length; i++){
int code = code(guess[i]);
if(guess[i] != solution[i] && code >= 0 && frequencies[code] > 0){
pesudoHit++;
frequencies[code]--;
}
}
return Result(hit, pesudoHit);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment