public GridCoordinate GetShipStart(List <List <BattleshipGridCell> > grid, BattleShip ship) { var nextShipStart = GetRandomGridCell(); while (_detectCollisionService.IsNextShipColliding(grid, ship, nextShipStart)) { nextShipStart = GetRandomGridCell(); } return(nextShipStart); }
public void GetShipStart_ShouldPreventCollisionsWithOtherShips_WhenPlacingNextShip() { // arrange var grid = BattleshipGameState.Empty(gridSize).Grid; var firstShipStart = new GridCoordinate(1, 2); grid[firstShipStart.Line][firstShipStart.Column] = BattleshipGridCell.Ship; // place ship on grid grid[firstShipStart.Line + 1][firstShipStart.Column] = BattleshipGridCell.Ship; var nextShip = new BattleShip(2, true); var expected = new GridCoordinate(2, 3); _randomService.NextCell() .Returns(firstShipStart, // we randomly got space that is firstShipStart, // already used by other ships expected); // until we got proper one _detectCollisionService.IsNextShipColliding(grid, nextShip, firstShipStart).Returns(true); _detectCollisionService.IsNextShipColliding(grid, nextShip, expected).Returns(false); // act var result = _servceUnderTest.GetShipStart(grid, nextShip); // assert Assert.AreEqual(expected, result); }