コード例 #1
0
        /// <summary>
        /// The function load the battleship to the battlefield
        /// </summary>
        /// <param name="battlefield"></param>
        /// <returns>Returns new battlfield object with new battlship</returns>
        private IBattleField LoadShipToBattlefield(IBattleField battlefield)
        {
            IBattleField newBattleField = (IBattleField)battlefield.Clone();

            int xOfTheBattlefield = battlefield.GetArray().GetLength(0) - 1;
            int yOfTheBattlefield = battlefield.GetArray().GetLength(1) - 1;

            int?maxXcoordinate = this.CoordinatesOfTheShip.Max(a => a.X);
            int?maxYcoordinate = this.CoordinatesOfTheShip.Max(a => a.Y);

            int?minXcoordinate = this.CoordinatesOfTheShip.Min(a => a.X);
            int?minYcoordinate = this.CoordinatesOfTheShip.Min(a => a.Y);

            foreach (Coordinates mast in this.CoordinatesOfTheShip)
            {
                newBattleField = newBattleField.AddMastOfTheShip(this.id, Convert.ToInt32(mast.X), Convert.ToInt32(mast.Y));
            }

            return(newBattleField);
        }
コード例 #2
0
 /// <summary>
 /// The function is checking is no collision on the map with other ship.
 /// </summary>
 /// <param name="battlefield"></param>
 /// <param name="coordinatesForTheShip"></param>
 /// <returns></returns>
 private bool IsNoColisionOnTHeMap(IBattleField battlefield, List <Coordinates> coordinatesForTheShip)
 {
     foreach (Coordinates coordinates in coordinatesForTheShip)
     {
         FieldState fieldState = battlefield.GetArray()[Convert.ToInt32(coordinates.X), Convert.ToInt32(coordinates.Y)].State;
         if (fieldState != FieldState.Empty)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #3
0
        /// <summary>
        /// This function get coordinates from the user.
        /// </summary>
        /// <returns>Two coordinates x and y</returns>
        public static (int x, int y) GetCoorinatesFromUser(IBattleField battlefield)
        {
            Console.Write("x:");
            int x;

            while (Int32.TryParse(Console.ReadLine(), out x) == false || battlefield.GetArray().GetLength(0) <= x || x < 0)
            {
                Console.WriteLine("The coordinate is wrong. You should type a number from 0 to 9");
                Console.Write("x:");
            }

            Console.Write("y:");
            int y;

            while (Int32.TryParse(Console.ReadLine(), out y) == false || battlefield.GetArray().GetLength(1) <= y || y < 0)
            {
                Console.WriteLine("The coordinate is wrong. You should type a number from 0 to 9");
                Console.Write("y:");
            }

            return(x : x, y : y);
        }
コード例 #4
0
        /// <summary>
        /// The function try to set all coordinates of the masts. It will be possible only if is no collision with other ship on the map
        /// </summary>
        /// <param name="battlefield"></param>
        /// <param name="intitialXPostion"></param>
        /// <param name="initialYPosition"></param>
        /// <param name="directionOfTheShip"></param>
        /// <returns>Return true if opperation was succed and false when isn't</returns>
        public bool TrySetCoordinatesForTheShip(IBattleField battlefield, int intitialXPostion, int initialYPosition, Direction directionOfTheShip)
        {
            bool isNoCollisions = false;

            switch (directionOfTheShip)
            {
            case Direction.Rigth:
                int distanceToRightBorder = battlefield.GetArray().GetLength(0) - intitialXPostion;
                if (distanceToRightBorder >= this.Size)
                {
                    List <Coordinates> TemporaryCoordinatesOfTheShip = new List <Coordinates>();
                    for (int i = 0; i < this.Size; i++)
                    {
                        TemporaryCoordinatesOfTheShip.Add(new Coordinates()
                        {
                            X = intitialXPostion + i, Y = initialYPosition
                        });
                    }
                    if (IsNoColisionOnTHeMap(battlefield, TemporaryCoordinatesOfTheShip))
                    {
                        this.coordinatesOfTheShip = TemporaryCoordinatesOfTheShip;
                        isNoCollisions            = true;
                    }
                    else
                    {
                        isNoCollisions = false;
                    }
                }
                else
                {
                    isNoCollisions = false;
                }
                break;

            case Direction.Down:
                int distanceToBottomBorder = battlefield.GetArray().GetLength(0) - initialYPosition;
                if (distanceToBottomBorder >= this.Size)
                {
                    List <Coordinates> TemporaryCoordinatesOfTheShip = new List <Coordinates>();
                    for (int i = 0; i < this.Size; i++)
                    {
                        TemporaryCoordinatesOfTheShip.Add(new Coordinates()
                        {
                            X = intitialXPostion, Y = initialYPosition + i
                        });
                    }
                    if (IsNoColisionOnTHeMap(battlefield, TemporaryCoordinatesOfTheShip))
                    {
                        this.coordinatesOfTheShip = TemporaryCoordinatesOfTheShip;
                        isNoCollisions            = true;
                    }
                }
                else
                {
                    isNoCollisions = false;
                }
                break;

            case Direction.Left:
                int distanceToLeftBorder = intitialXPostion + 1;
                if (distanceToLeftBorder >= this.Size)
                {
                    List <Coordinates> TemporaryCoordinatesOfTheShip = new List <Coordinates>();
                    for (int i = 0; i < this.Size; i++)
                    {
                        TemporaryCoordinatesOfTheShip.Add(new Coordinates()
                        {
                            X = intitialXPostion - i, Y = initialYPosition
                        });
                    }
                    if (IsNoColisionOnTHeMap(battlefield, TemporaryCoordinatesOfTheShip))
                    {
                        this.coordinatesOfTheShip = TemporaryCoordinatesOfTheShip;
                        isNoCollisions            = true;
                    }
                }
                else
                {
                    isNoCollisions = false;
                }
                break;

            case Direction.Up:
                int distanceToTopBorder = initialYPosition + 1;
                if (distanceToTopBorder >= this.Size)
                {
                    List <Coordinates> TemporaryCoordinatesOfTheShip = new List <Coordinates>();
                    for (int i = 0; i < this.Size; i++)
                    {
                        TemporaryCoordinatesOfTheShip.Add(new Coordinates()
                        {
                            X = intitialXPostion, Y = initialYPosition - i
                        });
                    }
                    if (IsNoColisionOnTHeMap(battlefield, TemporaryCoordinatesOfTheShip))
                    {
                        this.coordinatesOfTheShip = TemporaryCoordinatesOfTheShip;
                        isNoCollisions            = true;
                    }
                }
                else
                {
                    isNoCollisions = false;
                }
                break;
            }

            return(isNoCollisions);
        }