Beispiel #1
0
        public static bool Place(Ship ship, Field field)
        {
            var nearest = FieldGenerator.GetNearestPoints(ship.Points, field.Size);

            if (!nearest.Intersect(field.Ships.SelectMany(x => x.Points)).Any() &&
                !ship.Points.Any(x => x.X >= field.Size || x.X < 0 || x.Y >= field.Size || x.Y < 0))
            {
                field.Ships.Add(ship);
                return(true);
            }

            return(false);
        }
Beispiel #2
0
        public void DamageShip(Ship ship, Field field)
        {
            ship.Hp--;
            if (ship.Hp == 0)
            {
                var aroundShip = FieldGenerator.GetNearestPoints(ship.Points, field.Size);

                var exceptMissed = aroundShip.Except(field.Missed).Except(field.Ships.SelectMany(x => x.Points));

                field.Missed.AddRange(exceptMissed);
            }
            else if (ship.Hp < 0)
            {
                throw new ShipException();
            }
        }
Beispiel #3
0
        public bool NearStep(Field field)
        {
            var ship = field.Ships.Find(x => x.Hp > 0 && x.Hp < x.Size);

            if (ship == null)
            {
                return(RandomStep(field));
            }

            var points = field.Damaged.Intersect(ship.Points).ToList();

            var nearest = FieldGenerator.GetNearestPoints(points, field.Size)
                          .Except(field.Missed).Except(field.Damaged).ToList();

            Random rnd = new Random();

            var x     = rnd.Next(0, nearest.Count);
            var point = nearest[x];

            return(MakeStep(point, field));
        }