/** * get the next guess, without checking any matching * * @return the next guess */ protected Guess nextGuess() { Guess currentGuess = nextGuess; nextGuess = currentGuess.nextGuess(manager); return currentGuess; }
/** * 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; }
public boolean matches(Guess guess) { return this.guess.nrOfPartialMatches(guess) == partial && this.guess.nrOfFullMatches(guess) == full; }
private Guess nextIntervalStart(Color[] colors) { final int index = colors.length - 1; int step = NR_COLORS / nrThreads; if (step == 0) { step = 1; } while (step > 0) { if (manager.thereIsNextColor(colors[index])) { colors[index] = manager.nextColor(colors[index]); step--; } else { return Guess.none; } } Guess guess = new Guess(colors); while (!guess.isUnique()) { guess = guess.nextGuess(manager); } return guess; } }
@Override protected Guess firstGuess() { Color[] colors = new Color[table.nrOfColumns()]; int i = 0; for (Color color = table.manager.firstColor(); i < colors.length; ) { colors[i++] = color; } return new Guess(colors); }
/** * 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; } }
@Override protected Guess nextGuess() { Guess guess = super.nextGuess(); while (!guess.isUnique()) { guess = super.nextGuess(); } return guess; }
public Game(Table table, Guess secret) { this.table = table; this.secret = secret; this.nrOfColumns = this.secret.nrOfColumns(); }
public Color pos(int i) { return guess.getColor(i); }
public String toString() { return guess.toString() + " " + full + "/" + partial; }
@Override protected Guess nextGuess() { Guess guess; guess = super.nextGuess(); if (guess.equals(end)) { guess = Guess.none; } lastGuess = guess; return guess; }
private Guess firstIntervalStart(Color[] colors) { for (int i = 0; i < colors.length; i++) { colors[i] = manager.firstColor(); } return new Guess(colors); }
public int nrOfColumns() { return guess.nrOfColumns(); }
@Override protected Guess firstGuess() { final Color[] colors = new Color[table.nrOfColumns()]; int i = colors.length - 1; for (Color color = manager.firstColor(); i >= 0; color = manager.nextColor(color)) { colors[i--] = color; } return new Guess(colors); }
/** * 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; }
public Row addNewGuess(Guess guess) { assertNotFinished(); final int full = secret.nrOfFullMatches(guess); final int partial = secret.nrOfPartialMatches(guess); final Row row = new Row(guess,full,partial); table.addRow(row); if (itWasAWinningGuess(full)) { finished = true; } return row; }
private void assertCompatibility(Guess guess) { if (nrOfColumns() != guess.nrOfColumns()) { throw new IllegalArgumentException("Can not compare different lenght guesses"); } }
@Test public void twoColorsWrongPositionOtherGoodPosition() { ColorManager manager = new ColorManager(NR_COLORS, Color::new); Color[] colors = firstColors(); Row row = new Row(new Guess(colors), NR_COLUMNS - 2, 2); Color swap = colors[0]; colors[0] = colors[1]; colors[1] = swap; Assert.assertTrue(row.matches(new Guess(colors))); } }
private Guess createSecret() { Color[] colors = new Color[NR_COLUMNS]; int count = 0; Color color = manager.firstColor(); while (count < NR_COLORS - NR_COLUMNS) { color = manager.nextColor(color); count++; } for (int i = 0; i < NR_COLUMNS; i++) { colors[i] = color; color = manager.nextColor(color); } return new Guess(colors); }