public Game(Table table, Color[] secret) { this.table = table; this.secretRow = new Row(secret); this.nrOfColumns = secretRow.nrOfColumns(); }
/** * A guess matches if all rows in the table matches the guess. * * @param guess to match against the rows * @return true if all rows match */ private boolean guessMatch(Color[] guess) { for (Row row : table.rows) { if (!row.guessMatches(guess)) { return false; } } return true; }
/** * A guess matches if all rows in the table match the guess. * * @param guess to match against the rows * @return true if all rows match */ public boolean guessMatch(Guess guess) { for (Row row : table.rows) { if (!row.matches(guess)) { return false; } } return true; }
public boolean guessMatches(Color[] guess) { return nrMatchingColors(guess) == matchedColors && nrMatchingPositions(guess) == matchedPositions; }
public void addNewGuess(Row row) { if (isFinished()) { throw new IllegalArgumentException("You can not guess on a finished game."); } final int positionMatch = secretRow.nrMatchingPositions(row.positions); final int colorMatch = secretRow.nrMatchingColors(row.positions); row.setMatch(positionMatch, colorMatch); table.addRow(row); if (itWasAWinningGuess(positionMatch)) { finished = true; } }
@Test public void allColorsAndPositionsMatch() { ColorManager manager = new ColorManager(NR_COLORS); Color[] positions = new Color[NR_COLUMNS]; int i = 0; for (Color color = manager.firstColor(); i < positions.length; color = manager.nextColor(color)) { positions[i++] = color; } Row row = new Row(positions); row.setMatch(NR_COLUMNS, 0); Assert.assertTrue(row.guessMatches(positions)); }
/** * Create a new Row object that contains a guess that matches all guesses and the * responses to them that are on the table. * * @return the new Row to be added to the table along with the feedback afterwards. */ public Row guess() { Color[] guess = nextGuess(); while (guess != none && guessDoesNotMatch(guess)) { guess = nextGuess(); } if (guess == none) { return Row.none; } else { return new Row(guess); } }
protected Row(Row cloneFrom) { this(cloneFrom.positions); setMatch(cloneFrom.matchedPositions, cloneFrom.matchedColors); }
@Test public void twoColorsWrongPositionOtherGoodPosition() { ColorManager manager = new ColorManager(NR_COLORS); Color[] positions = new Color[NR_COLUMNS]; int i = 0; for (Color color = manager.firstColor(); i < positions.length; color = manager.nextColor(color)) { positions[i++] = color; } Row row = new Row(positions); Color swap = positions[0]; positions[0] = positions[1]; positions[1] = swap; row.setMatch(NR_COLUMNS - 2, 2); Assert.assertTrue(row.guessMatches(positions)); }
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; }
@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))); } }
@Test public void allButOneColorsAndPositionsMatch() { ColorManager manager = new ColorManager(NR_COLORS); Color[] positions = new Color[NR_COLUMNS]; int i = 0; for (Color color = manager.firstColor(); i < positions.length; color = manager.nextColor(color)) { positions[i++] = color; } Row row = new Row(positions); positions[NR_COLUMNS - 1] = manager.nextColor(positions[NR_COLUMNS - 1]); row.setMatch(NR_COLUMNS - 1, 0); Assert.assertTrue(row.guessMatches(positions)); }
@Test public void canAddNewGuess() { createTestGame(); game.addNewGuess(new Row(secret)); }
@Test public void testSimpleGame() { Table table = new Table(NR_COLUMNS, manager); Guess secret = createSecret(); System.out.println(PrettyPrintRow.pprint(new Row(secret, 4, 0))); System.out.println(); Game game = new Game(table, secret); Guesser guesser = new UniqueGuesser(table); while (!game.isFinished()) { Guess guess = guesser.guess(); if (guess == Guess.none) { Assert.fail(); } Row row = game.addNewGuess(guess); System.out.println(PrettyPrintRow.pprint(row)); } } }
@Test public void testSimpleGame() { Table table = new Table(nrColumns, manager); Color[] secret = createSecret(); System.out.println(PrettyPrintRow.pprint(new Row(secret))); System.out.println(); Game game = new Game(table, secret); Guesser guesser = new UniqueGuesser(table); while (!game.isFinished()) { Row guess = guesser.guess(); if (guess == Row.none) { Assert.fail(); } game.addNewGuess(guess); System.out.println(PrettyPrintRow.pprint(guess)); } } }
public String playParallel() { Table table = new Table(NR_COLUMNS, manager); Guess secret = createSecret(); println(PrettyPrintRow.pprint(new Row(secret, NR_COLUMNS, 0))); println(); Game game = new Game(table, secret); final IntervalGuesser[] guessers = createGuessers(table); startAsynchronousGuessers(guessers); final Guesser finalCheckGuesser = new UniqueGuesser(table); int serial = 1; try { while (!game.isFinished()) { final Guess guess = guessQueue.take(); if (finalCheckGuesser.guessMatch(guess)) { if (guess == Guess.none) { Assert.fail(); } Row row = game.addNewGuess(guess); print("" + serial + ". "); serial++; println(PrettyPrintRow.pprint(row)); } } } catch (InterruptedException ie) { } finally { stopAsynchronousGuessers(guessers); } return out; }