コード例 #1
0
        private void AddShip(ShipType shipType, int shipSize, GridPoint startingPoint, bool isHorizontalFree, bool isVerticalFree)
        {
            bool horizontalDirection;

            if (isHorizontalFree && isVerticalFree)
            {
                horizontalDirection = new Random().Next(0, 2) == 0;
            }
            else if (isHorizontalFree)
            {
                horizontalDirection = true;
            }
            else
            {
                horizontalDirection = false;
            }

            var shipBody = new List <IGridPoint>();

            for (var i = 0; i < shipSize; i++)
            {
                int x, y;

                x = horizontalDirection ? startingPoint.X : startingPoint.X + i;
                y = horizontalDirection ? startingPoint.Y + i : startingPoint.Y;

                shipBody.Add(new GridPoint(x, y));
            }

            var ship = new Ship(shipType, shipBody);

            _grid.AddShip(ship);
        }