public Game(Table table, Color[] secret) { this.table = table; this.secretRow = new Row(secret); this.nrOfColumns = secretRow.nrOfColumns(); }
/** * 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); } }
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(expected = IllegalArgumentException.class) public void throwsIAEForInvalidMatchParameters(){ Row row = new Row(new Color[NR_COLUMNS]); row.setMatch(NR_COLUMNS,1); } }
@Test public void canAddNewGuess() { createTestGame(); game.addNewGuess(new Row(secret)); }
@Test public void allColorsAndPositionsMatch() { Color[] colors = firstColors(); Guess guess = new Guess(colors); Row row = new Row(guess, NR_COLUMNS, 0); Assert.assertTrue(row.matches(guess)); }
@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)); } } }
@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)); }
@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)); }
@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 allButOneColorsAndPositionsMatch() { ColorManager manager = new ColorManager(NR_COLORS, Color::new); Color[] colors = firstColors(); Row row = new Row(new Guess(colors), NR_COLUMNS - 1, 0); stepTheLastColor(colors); Assert.assertTrue(row.matches(new Guess(colors))); }
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; }