示例#1
0
        private static Battleship CreateGameBattleship(int battleshipSize, int boardSize)
        {
            var coordinates     = new Coordinate[battleshipSize];
            var isHorizontalPos = _randGenerator.Next(boardSize - 1) % 2 == 0;

            var xCoordinate = _randGenerator.Next(boardSize - 1);
            var yCoordinate = _randGenerator.Next(boardSize - 1);

            if (xCoordinate + battleshipSize > boardSize)
            {
                xCoordinate = GetSingleCoordinateWithinBoardGame(battleshipSize, boardSize);
            }

            if (yCoordinate + battleshipSize > boardSize)
            {
                yCoordinate = GetSingleCoordinateWithinBoardGame(battleshipSize, boardSize);
            }

            if (isHorizontalPos)
            {
                for (int i = 0; i < battleshipSize; i++)
                {
                    coordinates[i] = new Coordinate(xCoordinate, yCoordinate);
                    xCoordinate++;
                }
            }
            else
            {
                for (int i = 0; i < battleshipSize; i++)
                {
                    coordinates[i] = new Coordinate(xCoordinate, yCoordinate);
                    yCoordinate++;
                }
            }

            return(BattleshipBuilder.BuildBattleShip(coordinates));
        }