Exemple #1
0
        public bool PutShip(Placement placement)
        {
            if (!(placement.Vessel is Vessel))
                throw new ArgumentException("Expecting ship to be of type Vessel, but it was something else (possible cheat).", "placement");

            return _board.PlacePlayerShip(_player, placement);
        }
        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++;
            }
        }
Exemple #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));
            }
        }
Exemple #4
0
        private Coordinate ShipEndPoint(Placement placement)
        {
            Coordinate end;
            if (placement.Orientation == Orientation.Horizontal)
                end = new Coordinate(placement.X + (placement.Vessel.Length - 1), placement.Y);
            else //if (placement.Orientation == Orientation.Vertical)
                end = new Coordinate(placement.X, placement.Y - (placement.Vessel.Length - 1));  // TODO: This should be changed.

            return end;
        }
Exemple #5
0
        private bool IsLegalBoardPlacement(Placement placement)
        {
            Coordinate start, end, boardTopLeft, boardBottomRight;

            start = new Coordinate(placement);
            end = ShipEndPoint(placement);

            boardTopLeft = new Coordinate(1, YMax);
            boardBottomRight = new Coordinate(XMax, 1);

            if (CoordinateInSquare(start, boardTopLeft, boardBottomRight) &&
                CoordinateInSquare(end, boardTopLeft, boardBottomRight))
                return true;

            return false;
        }
Exemple #6
0
        private bool IsInTheWayOf(Placement placedShip, Placement placement)
        {
            Coordinate shipTopLeft, shipBottomRight, endPoint, tmpSegment;

            // TODO: This should be BottomLeft and TopRight if going away from origo.
            shipTopLeft = new Coordinate(placedShip.X - 1, placedShip.Y + 1);
            endPoint = ShipEndPoint(placedShip);
            shipBottomRight = new Coordinate(endPoint.X + 1, endPoint.Y - 1);

            for (int i = 0; i <= placement.Vessel.Length; i++)
            {
                if (placement.Orientation == Orientation.Horizontal)
                    tmpSegment = new Coordinate(placement.X + i, placement.Y);
                else //if (placement.Orientation == Orientation.Vertical)
                    tmpSegment = new Coordinate(placement.X, placement.Y - i);

                if (CoordinateInSquare(tmpSegment, shipTopLeft, shipBottomRight))
                    return true;
            }

            return false;
        }
Exemple #7
0
        internal bool PlacePlayerShip(Player _player, Placement placement)
        {
            if (_gameStarted)
                throw new Exception(String.Format("Player {0} tried to place a ship after the game started. Bad player.", _player));

            List<Placement> playerFleet = _playerShips[_player];

            if (IsLegalBoardPlacement(placement))
            {
                foreach (Placement placedShip in playerFleet)
                {
                    if (IsInTheWayOf(placedShip, placement))
                        return false;
                }
            }
            else
                return false;

            playerFleet.Add(placement);
            return true;
        }