Esempio n. 1
0
        public void PlaceShips(IPlayerView playerView, ICollection<IVessel> ships)
        {
            /* This AI places ships in the upper right corner and to the left.
             *
             * E.g.
             *
             * 10  #   #   #   #   #
             * 9   #   #   #   #   #
             * 8       #   #   #   #
             * 7               #   #
             * 6                   #
             * 5
             * 4
             * 3
             * 2
             * 1
             *   1 2 3 4 5 6 7 8 9 10
             */

            Placement place;
            Coordinate coord;
            int xMax = playerView.GetXMax();
            int yMax = playerView.GetYMax();

            int i = 0;
            foreach (IVessel ship in ships)
            {
                coord = new Coordinate(xMax - (2 * i), yMax);
                place = new Placement(ship, coord, Orientation.Vertical);
                playerView.PutShip(place);

                i++;
            }
        }
Esempio n. 2
0
        public void PlaceShips(IPlayerView playerView, ICollection<IVessel> ships)
        {
            InitializeAI(playerView);

              int x, y;
              Orientation orient;
            #if DEBUG
              Visualizer viz = new Visualizer(_xMax, _yMax);
            #endif

              foreach (IVessel ship in ships)
              {
            do
            {
              x = _rand.Next(0, _xMax) + 1;
              y = _rand.Next(0, _yMax) + 1;
              orient = (_rand.NextDouble() < 0.5) ? Orientation.Horizontal : Orientation.Vertical;
            } while (!playerView.PutShip(ship.SailTo(x, y, orient)));
            #if DEBUG
            for (int i = 0; i < ship.Length; i++)
            {
              if (orient == Orientation.Horizontal)
            viz.SetSquare(x + i, y, false);
              else
            viz.SetSquare(x, y - i, false);
            }
            #endif
              }
            #if DEBUG
              Console.WriteLine("Ships:");
              Console.WriteLine(viz.FieldView());
            #endif
        }
Esempio n. 3
0
        public void PlaceShips(IPlayerView playerView, ICollection<IVessel> ships)
        {
            int maxX = playerView.GetXMax();
            int maxY = playerView.GetYMax();

            Placement placement;
            int xPos;
            int yPos;
            Orientation direction;

            foreach (IVessel ship in ships)
            {
                do
                {
                    xPos = _rand.Next(maxX) + 1;
                    yPos = _rand.Next(maxY) + 1;
                    direction = _rand.NextDouble() > 0.5 ? Orientation.Horizontal : Orientation.Vertical;
                    placement = new Placement(ship, xPos, yPos, direction);
                } while (!playerView.PutShip(placement));
            }
        }
Esempio n. 4
0
        private void CreateInternal(IPlayerView world, ICollection<IVessel> enemies)
        {
            Coordinate coordinate;
            int tries = 0;
            Orientation orientation = Orientation.Horizontal;

            foreach (IVessel enemy in enemies.OrderByDescending(v => v.Length))
            {
                Enemies.Add(enemy);
                coordinate = RandomCoordinate;
                orientation = Dice.Next(2) == 1 ? Orientation.Horizontal : Orientation.Vertical;

                if (orientation == Orientation.Horizontal && coordinate.Latitude + enemy.Length > Boundaries.East)
                    coordinate.Latitude -= coordinate.Latitude + enemy.Length - Boundaries.East;

                if (orientation == Orientation.Vertical && coordinate.Longitude - enemy.Length < 1)
                    coordinate.Longitude = enemy.Length;

                if (AvoidEdge)
                    AvoidEdges(coordinate);

                while (!world.PutShip(enemy.SailTo(coordinate.ToInterfaceCoordinate(), orientation)))
                {
                    if (++coordinate.Latitude > Boundaries.East)
                        coordinate.Latitude = Boundaries.West;

                    if (++coordinate.Longitude > Boundaries.North)
                        coordinate.Longitude = Boundaries.South;

                    if (++tries > 10)
                        orientation = orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal;

                    if (tries > 20)
                    {
                        tries = 0;
                        coordinate = RandomCoordinate;
                    }

                    if (AvoidEdge)
                        AvoidEdges(coordinate);
                }
            }
        }