Exemple #1
0
        public bool Neighbour(Ship p)
        {
            if (p.Direction     == Direction.Vertical &&
                this.Direction  == Direction.Vertical &&
                Math.Abs(p.StartPoint.X - this.StartPoint.X) == 1 &&
                Math.Min(p.EndPoint.Y, this.EndPoint.Y)+1 - Math.Max(p.StartPoint.Y, this.StartPoint.Y) > 0)
                return true;

            if (p.Direction     == Direction.Horizontal &&
                this.Direction  == Direction.Horizontal &&
                Math.Abs(p.StartPoint.Y - this.StartPoint.Y) == 1 &&
                Math.Min(p.EndPoint.X, this.EndPoint.X)+1 - Math.Max(p.StartPoint.X, this.StartPoint.X) > 0)
                return true;

            var ver = this.Direction == Direction.Vertical ? this : p;
            var hor = this.Direction == Direction.Vertical ? p    : this;

            if (ver.StartPoint.Y <= hor.StartPoint.Y && hor.StartPoint.Y <= ver.EndPoint.Y &&
                Math.Min(Math.Abs(ver.StartPoint.X - hor.StartPoint.X), Math.Abs(ver.StartPoint.X - hor.EndPoint.X)) == 1
                 ||
                hor.StartPoint.X <= ver.StartPoint.X && ver.StartPoint.X <= hor.EndPoint.X &&
                Math.Min(Math.Abs(hor.StartPoint.Y - ver.StartPoint.Y), Math.Abs(hor.StartPoint.Y - ver.EndPoint.Y)) == 1)
                return true;

            return false;
        }
        public List<Ship> PlaceShips(List<int> sizes, int boardHeight, int boardWidth)
        {
            Height = boardHeight;
            Width = boardWidth;

            //var ship = new Ship
            //{
            //    Direction = Direction.Vertical,
            //    Length = 3,
            //    Location = new Point(0, 0)
            //};

            //var newShip = new Ship
            //{
            //    Direction = Direction.Vertical,
            //    Length = 3,
            //    Location = new Point(0, 3)
            //};

            //Console.WriteLine(ship.Neighbour(newShip));

            var ships = new List<Ship>();

            for (int i = 0; i < sizes.Count; i++)
            {
                var size = sizes[i];

                // Create new ship
                var newShip = new Ship
                {
                    Direction = Helpers.Random.Next(0, 1 + 1) == 0 ? Direction.Horizontal : Direction.Vertical,
                    Length = size,
                    Location = Helpers.RandomPoint(boardHeight, boardWidth)
                };

                // Check that it doesn't overlap with any other
                bool ok = ships.All(ship => !ship.Overlaps(newShip));
                ok = ok && ships.All(ship => !ship.Neighbour(newShip));

                // Do not advance to next ship,
                // repeat the same one next iteration
                if (!ok ||
                    newShip.EndPoint.X > boardWidth ||
                    newShip.EndPoint.Y >= boardHeight)
                    i--;
                else
                    ships.Add(newShip);
            }

            return ships;
        }
Exemple #3
0
 public bool Overlaps(Ship p)
 {
     return Helpers.Intersect(StartPoint, EndPoint, p.StartPoint, p.EndPoint);
 }