Example #1
0
 public Player()
 {
     Name = "";
     Ships = new Ship[5];
     Ships[0] = ShipCreator.CreateShip(ShipType.Destroyer);
     Ships[1] = ShipCreator.CreateShip(ShipType.Cruiser);
     Ships[2] = ShipCreator.CreateShip(ShipType.Submarine);
     Ships[3] = ShipCreator.CreateShip(ShipType.Battleship);
     Ships[4] = ShipCreator.CreateShip(ShipType.Carrier);
     ShipLocations = new Dictionary<Coordinate, ShipType>();
 }
Example #2
0
 private void AddShipToBoard(Ship newShip)
 {
     _ships[_currentShipIndex] = newShip;
     _currentShipIndex++;
 }
Example #3
0
        private ShipPlacement PlaceShipUp(Coordinate coordinate, Ship newShip)
        {
            // y coordinate gets smaller
            int positionIndex = 0;
            int minY = coordinate.YCoordinate - newShip.BoardPositions.Length;

            for (int i = coordinate.YCoordinate; i > minY; i--)
            {
                var currentCoordinate = new Coordinate(coordinate.XCoordinate, i);

                if (!IsValidCoordinate(currentCoordinate))
                    return ShipPlacement.NotEnoughSpace;

                if (OverlapsAnotherShip(currentCoordinate))
                    return ShipPlacement.Overlap;

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;
            }

            AddShipToBoard(newShip);
            return ShipPlacement.Ok;
        }
Example #4
0
        private ShipPlacement PlaceShipRight(Coordinate coordinate, Ship newShip)
        {
            // x coordinate gets bigger
            int positionIndex = 0;
            int maxX = coordinate.XCoordinate + newShip.BoardPositions.Length;

            for (int i = coordinate.XCoordinate; i < maxX; i++)
            {
                var currentCoordinate = new Coordinate(i, coordinate.YCoordinate);

                if (!IsValidCoordinate(currentCoordinate))
                    return ShipPlacement.NotEnoughSpace;

                if (OverlapsAnotherShip(currentCoordinate))
                    return ShipPlacement.Overlap;

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;
            }

            AddShipToBoard(newShip);
            return ShipPlacement.Ok;
        }
Example #5
0
        private ShipPlacement PlaceShipUp(Coordinate coordinate, Ship newShip)
        {
            // y coordinate gets smaller
            int positionIndex = 0;
            int minY = coordinate.YCoordinate - newShip.BoardPositions.Length;

            for (int i = coordinate.YCoordinate; i > minY; i--)
            {
                var currentCoordinate = new Coordinate(coordinate.XCoordinate, i);

                if (!IsValidCoordinate(currentCoordinate))
                    return ShipPlacement.NotEnoughSpace;

                if (OverlapsAnotherShip(currentCoordinate))
                    return ShipPlacement.Overlap;

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;

            }

            // Mike's changes if placement is ok, add to dictionary
            positionIndex = 0;
            for (int i = coordinate.YCoordinate; i > minY; i--)
            {
                var currentCoordinate = new Coordinate(coordinate.XCoordinate, i);

               newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;

                // Mike's changes - add to dictionary
                ShipHistory.Add(currentCoordinate, newShip.ShipType);
            }
            AddShipToBoard(newShip);
            return ShipPlacement.Ok;
        }
Example #6
0
        private ShipPlacement PlaceShipRight(Coordinate coordinate, Ship newShip)
        {
            // x coordinate gets bigger
            int positionIndex = 0;
            int maxX = coordinate.XCoordinate + newShip.BoardPositions.Length;

            for (int i = coordinate.XCoordinate; i < maxX; i++)
            {
                var currentCoordinate = new Coordinate(i, coordinate.YCoordinate);

                if (!IsValidCoordinate(currentCoordinate))
                    return ShipPlacement.NotEnoughSpace;

                if (OverlapsAnotherShip(currentCoordinate))
                    return ShipPlacement.Overlap;

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;

            }

            // Mike's changes if placement is ok add to dictionary
            positionIndex = 0;
            for (int i = coordinate.XCoordinate; i < maxX; i++)
            {
                var currentCoordinate = new Coordinate(i, coordinate.YCoordinate);

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                positionIndex++;

                // Mike's changes - add to dictionary
                ShipHistory.Add(currentCoordinate, newShip.ShipType);

            }

            AddShipToBoard(newShip);
            return ShipPlacement.Ok;
        }
Example #7
0
        private ShipPlacement PlaceShipUp(Coordinate coordinate, Ship newShip)
        {
            // y coordinate gets smaller
            int positionIndex = 0;
            int minY = coordinate.YCoordinate - newShip.BoardPositions.Length;

            for (int i = coordinate.YCoordinate; i > minY; i--)
            {
                var currentCoordinate = new Coordinate(coordinate.XCoordinate, i);

                if (!IsValidCoordinate(currentCoordinate))
                {
                    foreach (var badvalue in ShipHistory.Where(rv => rv.Value == newShip.ShipType).ToList())
                    {
                        ShipHistory.Remove(badvalue.Key);
                    }
                        return ShipPlacement.NotEnoughSpace;
                }

                if (OverlapsAnotherShip(currentCoordinate))
                {
                    foreach (var badvalue in ShipHistory.Where(rv => rv.Value == newShip.ShipType).ToList())
                    {
                        ShipHistory.Remove(badvalue.Key);
                    }
                        return ShipPlacement.Overlap;
                }

                newShip.BoardPositions[positionIndex] = currentCoordinate;
                ShipHistory.Add(currentCoordinate, newShip.ShipType);
                positionIndex++;
            }

            AddShipToBoard(newShip);
            return ShipPlacement.Ok;
        }