### Question:
RandomArtifactGenerator implements ArtifactGenerator { @Override public Apple generateApple(Hero snake, Apple apple, Stone stone, Walls walls, int boardSize) { Point pt; boolean noSoGoodPlace; if (checkForMaxLength(snake, walls, boardSize)){ return new Apple(-1, -1); } int c = 0; do { pt = PointImpl.random(dice, boardSize); boolean onSnake = snake.itsMe(pt); boolean onStone = stone.itsMe(pt); boolean onWall = walls.itsMe(pt); boolean onApple = apple != null && apple.itsMe(pt); boolean whenStandstill = isStandstill(pt, stone, boardSize); boolean outOfBoard = pt.isOutOf(boardSize); noSoGoodPlace = outOfBoard || onSnake || onStone || whenStandstill || onWall || onApple; } while (noSoGoodPlace && c++ < MAX); if (c >= MAX) { return new Apple(-1, -1); } return new Apple(pt); } RandomArtifactGenerator(Dice dice); @Override Stone generateStone(Hero snake, Apple apple, Walls walls, int boardSize); @Override Apple generateApple(Hero snake, Apple apple, Stone stone, Walls walls, int boardSize); static final int MAX; }### Answer:
@Test public void testMaxApplePosition() { generator = new RandomArtifactGenerator(dice); initBoardMock(); initFullWallsMock(); snake = new Hero(3, 1); initSnake(); stone = new Stone(1, 1); apple = new Apple(4, 1); int x = 1; boolean growX = true; for (int y = 1; y < BOARD_SIZE; y++) { while (x <= BOARD_SIZE) { Point xy = pt(x, y); if (stone.itsMe(xy) || snake.itsMe(xy) || walls.itsMe(x, y)) { if (growX) { x++; } else { x--; } continue; } snake.grow(); snake.move(x, y); if (growX) { x++; } else { x--; } if (x == BOARD_SIZE) { growX = false; x--; break; } if (x == 0) { growX = true; x++; break; } if (x == BOARD_SIZE - 1 && y == BOARD_SIZE - 1) { break; } } } Apple newApple = generator.generateApple(snake, apple, stone, walls, BOARD_SIZE); assertEquals(new Apple(-1, -1), newApple); }