Example #1
0
        private static List <Position> ValidatePlayerPositions(List <Ship> playerShips, IPositionParser positionParser, IPositionValidator positionValidator, IShipManager shipManager, List <Position> shipPositions, Ship ship, Battlefield battlefield)
        {
            Position  position;
            Direction direction;

            for (int i = 0; i < shipPositions.Count; i++)
            {
                while (positionValidator.IsValidPosition(shipPositions[i], battlefield.ColumnSize, battlefield.RowSize, playerShips) == false)
                {
                    Console.WriteLine();
                    do
                    {
                        Console.Write(" Position is not validated, try again >");
                        position = positionParser.Parse(Console.ReadLine());
                    } while (position == null);

                    do
                    {
                        direction = PlaceManager.GetDirection();
                    } while (direction == Direction.None);

                    shipPositions = shipManager.WholeShipPositions(position, ship.Size, direction);

                    i = -1;
                    break;
                }
            }
            return(shipPositions);
        }
Example #2
0
        public void PlaceComputerShips(List <Ship> computerShips, IRandomManager randomManager, IShipManager shipManager, IPositionValidator positionValidator, Battlefield battlefield)
        {
            foreach (var ship in computerShips)
            {
                var randomPosition  = randomManager.RandomPosition(battlefield.ColumnSize, battlefield.RowSize);
                var randomDirection = randomManager.RandomDirection();
                var shipPositions   = shipManager.WholeShipPositions(randomPosition, ship.Size, randomDirection);

                for (int i = 0; i < shipPositions.Count; i++)
                {
                    while (positionValidator.IsValidPosition(shipPositions[i], battlefield.ColumnSize, battlefield.RowSize, computerShips) == false)
                    {
                        randomPosition  = randomManager.RandomPosition(battlefield.ColumnSize, battlefield.RowSize);
                        randomDirection = randomManager.RandomDirection();
                        shipPositions   = shipManager.WholeShipPositions(randomPosition, ship.Size, randomDirection);
                        i = -1;
                        break;
                    }
                }

                ship.Positions.AddRange(shipPositions);
            }
        }