示例#1
0
        public bool Equals(ShipCoordinate other)
        {
            if (object.ReferenceEquals(other, null))
            {
                return(false);
            }

            return(X == other.X && Y == other.Y);
        }
示例#2
0
        public static Tuple <bool, ShipCoordinate> TryIncrementVerticalPosition(ShipCoordinate position)
        {
            ShipCoordinate newPosition;

            if (ShipCoordinate.TryConstruct(position.X, position.Y + 1, out newPosition))
            {
                return(new Tuple <bool, ShipCoordinate>(true, newPosition));
            }
            else
            {
                return(new Tuple <bool, ShipCoordinate>(false, ShipCoordinate.Zero));
            }
        }
示例#3
0
        public bool TryHit(ShipCoordinate coordinate)
        {
            ShipCell hitCell = Cells.FirstOrDefault(item => item.Coordinate == coordinate);

            if (hitCell != null)
            {
                hitCell.Hit = true;
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#4
0
        public static bool TryConstruct(int x, int y, out ShipCoordinate position)
        {
            position = null;
            if (x < 0 || x >= BoardWidth)
            {
                return(false);
            }
            position = null;
            if (y < 0 || y >= BoardHeight)
            {
                return(false);
            }

            position = new ShipCoordinate(x, y);
            return(true);
        }
示例#5
0
        public static Tuple <bool, Ship> TryConstructShipWithCoordinate(
            ShipClass shipClass,
            ShipOrientation orientation,
            ShipCoordinate headCoordinate
            )
        {
            var cellsResult = ShipAllocationLogic.TryGetCellsOnBoard(headCoordinate, shipClass.Length, orientation);

            if (cellsResult.Item1)
            {
                return(new Tuple <bool, Ship>(true, new Ship(shipClass, orientation, headCoordinate, cellsResult.Item2)));
            }
            else
            {
                return(new Tuple <bool, Ship>(false, null));
            }
        }
示例#6
0
        public static Ship ConstructShipWithCoordinate(
            ShipClass shipClass,
            ShipOrientation orientation,
            ShipCoordinate headCoordinate
            )
        {
            var result = TryConstructShipWithCoordinate(shipClass, orientation, headCoordinate);

            if (result.Item1)
            {
                return(result.Item2);
            }
            else
            {
                throw new ArgumentException("Not good ship class/orientation/headCoordinate.");
            }
        }
 public ShotResult Shot(ShipCoordinate coordinate)
 {
     if (shipList.Any(ship => ship.TryHit(coordinate)))
     {
         if (shipList.All(ship => ship.IsDestroyed))
         {
             return(ShotResult.GameWin);
         }
         else
         {
             return(ShotResult.Hit);
         }
     }
     else
     {
         return(ShotResult.Miss);
     }
 }
示例#8
0
        public Ship(
            ShipClass shipClass,
            ShipOrientation orientation,
            ShipCoordinate headCoordinate,
            List <ShipCoordinate> cells
            )
        {
            ShipClass      = shipClass ?? throw new ArgumentNullException(nameof(shipClass));
            Orientation    = orientation;
            HeadCoordinate = headCoordinate ?? throw new ArgumentNullException(nameof(headCoordinate));

            if (!ShipAllocationLogic.ValidateShipBarier(headCoordinate, shipClass.Length, orientation))
            {
                throw new ArgumentException("Ship out of the board.");
            }

            Cells = cells.Select(item => new ShipCell(item)).ToList();
        }
示例#9
0
        public static bool ValidateShipBarier(ShipCoordinate position, int length, ShipOrientation orientation)
        {
            var            getShipDirectionPositionUpdate = GetShipDirectionPositionUpdate(orientation);
            ShipCoordinate currentPosition = position;

            for (int i = 1; i < length; i++)
            {
                var update = getShipDirectionPositionUpdate(currentPosition);
                if (update.Item1)
                {
                    currentPosition = update.Item2;
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
示例#10
0
        /// <summary>
        /// Returns ship cells if it doesn't go out of the board.
        /// </summary>
        public static Tuple <bool, List <ShipCoordinate> > TryGetCellsOnBoard(ShipCoordinate headPosition, int length, ShipOrientation orientation)
        {
            var            positionUpdate  = GetShipDirectionPositionUpdate(orientation);
            ShipCoordinate currentPosition = headPosition;
            var            resultList      = new List <ShipCoordinate> {
                currentPosition
            };

            for (int tailIndex = 1; tailIndex < length; tailIndex++)
            {
                var update = positionUpdate(currentPosition);
                if (update.Item1)
                {
                    currentPosition = update.Item2;
                    resultList.Add(update.Item2);
                }
                else
                {
                    return(new Tuple <bool, List <ShipCoordinate> >(false, new List <ShipCoordinate>()));
                }
            }
            return(new Tuple <bool, List <ShipCoordinate> >(true, resultList));
        }