| 39 | public static int MAX_COLORS = 4; |
| 40 | |
| 41 | public static Result estimate(String guess, String solution) { |
| 42 | if (guess.length() != solution.length()) return null; |
| 43 | Result res = new Result(); |
| 44 | int[] frequencies = new int[MAX_COLORS]; |
| 45 | |
| 46 | /* Compute hits and built frequency table */ |
| 47 | for (int i = 0; i < guess.length(); i++) { |
| 48 | if (guess.charAt(i) == solution.charAt(i)) { |
| 49 | res.hits++; |
| 50 | } else { |
| 51 | /* Only increment the frequency table (which will be used for pseudo-hits) if |
| 52 | * it's not a hit. If it's a hit, the slot has already been "used." */ |
| 53 | int code = code(solution.charAt(i)); |
| 54 | if (code >= 0) { |
| 55 | frequencies[code]++; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* Compute pseudo-hits */ |
| 61 | for (int i = 0; i < guess.length(); i++) { |
| 62 | int code = code(guess.charAt(i)); |
| 63 | if (code >= 0 && frequencies[code] > 0 && guess.charAt(i) != solution.charAt(i)) { |
| 64 | res.pseudoHits++; |
| 65 | frequencies[code]--; |
| 66 | } |
| 67 | } |
| 68 | return res; |
| 69 | } |
| 70 | |
| 71 | /************************** TEST CODE **********************************/ |
| 72 | |