public Game(Table table, Guess secret) { this.table = table; this.secret = secret; this.nrOfColumns = this.secret.nrOfColumns(); }
public int nrOfColumns() { return guess.nrOfColumns(); }
private void assertCompatibility(Guess guess) { if (nrOfColumns() != guess.nrOfColumns()) { throw new IllegalArgumentException("Can not compare different lenght guesses"); } }
/** * Count the number of colors that are present on the guess * but not on the pos where they are in the other guess. * If the same color is on multiple pos it is counte * for each pos once. For example the secret is * <pre> * RGRB * </pre> * and the guess is * <pre> * YRPR * </pre> * then this method will return 2. * * @param guess is the actual guess that we evaluate * @return the number of good colors not in pos */ public int nrOfPartialMatches(Guess guess) { assertCompatibility(guess); int count = 0; for (int i = 0; i < nrOfColumns(); i++) { for (int j = 0; j < nrOfColumns(); j++) { if (i != j && guess.getColor(i) == this.getColor(j)) { count++; } } } return count; }
/** * Count the number of colors that are correct and are in pos. * * @param guess is the actual guess that we evaluate * @return the number of colors that match in pos */ public int nrOfFullMatches(Guess guess) { assertCompatibility(guess); int count = 0; for (int i = 0; i < nrOfColumns(); i++) { if (guess.getColor(i) == this.getColor(i)) { count++; } } return count; }
/** * Calculate the next guess and return a new Guess object. * The guesses are ordered in the order of the colors as * specified by the color manager. * * @param manager that specifies the order of the colors * an can return the next color after one color. * @return the guess that comes after this guess. */ public Guess nextGuess(ColorManager manager) { final Color[] colors = Arrays.copyOf(this.colors, nrOfColumns()); int i = 0; boolean guessFound = false; while (i < colors.length && !guessFound) { if (manager.thereIsNextColor(getColor(i))) { colors[i] = manager.nextColor(colors[i]); guessFound = true; } else { colors[i] = manager.firstColor(); i++; } } if (guessFound) { return new Guess(colors); } else { return Guess.none; } }