Ejemplo n.º 1
0
        /// <summary>
        /// Returns a value of indicating whether the AI set the ship.
        /// </summary>
        /// <param name="shipType"></param>
        /// <returns>bool</returns>
        private bool SetShip(ShipType shipType)
        {
            try
            {
                Direction shipDirection = Direction.Horizontal;
                int       shipLength    = 0;
                int       horizontalCoordinateStartCell = 0;
                int       verticalCoordinateStartCell   = 0;

                switch (shipType)
                {
                case ShipType.One:
                    shipLength = 1;
                    break;

                case ShipType.Two:
                    shipLength = 2;
                    break;

                case ShipType.Three:
                    shipLength = 3;
                    break;

                case ShipType.Four:
                    shipLength = 4;
                    break;
                }
                var t = (new Random().Next(2));
                shipDirection = ((t == 1) ? Direction.Horizontal : Direction.Vertical);

                horizontalCoordinateStartCell = new Random().Next(10);
                verticalCoordinateStartCell   = new Random().Next(10);

                BaseShip baseShip = new BaseShip(shipDirection, shipLength, verticalCoordinateStartCell, horizontalCoordinateStartCell);
                if (HomeField.IsPossibleToSetShip(baseShip))
                {
                    HomeField.SetShip(baseShip);

                    Ships.Add(baseShip);

                    return(true);
                }
                return(false);
            }
            catch (Exception e)
            {
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns a value of indicating whether the AI set the ship.
        /// </summary>
        /// <param name="shipType"></param>
        /// <returns>bool</returns>
        private bool SetShip(ShipType shipType)
        {
            try
            {
                Direction shipDirection = Direction.Horizontal;
                int       shipLength    = 0;
                int       horizontalCoordinateStartCell = 0;
                int       verticalCoordinateStartCell   = 0;

                switch (shipType)
                {
                case ShipType.One:
                    UI.Message("Установка одно-палубного");
                    shipLength = 1;
                    break;

                case ShipType.Two:
                    UI.Message("Установка двух-палубного");
                    shipLength = 2;
                    break;

                case ShipType.Three:
                    UI.Message("Установка трех-палубного");
                    shipLength = 3;
                    break;

                case ShipType.Four:
                    UI.Message("Установка четырех-палубного");
                    shipLength = 4;
                    break;
                }

                UI.Message("Введите направление: 1 - по горизонтали; 2 - по вертикали");
                shipDirection = ((Convert.ToInt32(UI.ReadLine().First().ToString()) == 1) ? Direction.Horizontal : Direction.Vertical);

                UI.Message("Введите координату начальной клетке: сначала букву, через пробел цифру '1 A'");
                string[] startCellLineFromConsole = UI.ReadLine().Trim().Split(' ');
                horizontalCoordinateStartCell = (Convert.ToInt32(startCellLineFromConsole.First().ToString()) - 1);

                switch (startCellLineFromConsole.Last()[0])
                {
                case 'А': verticalCoordinateStartCell = 0; break;

                case 'Б': verticalCoordinateStartCell = 1; break;

                case 'В': verticalCoordinateStartCell = 2; break;

                case 'Г': verticalCoordinateStartCell = 3; break;

                case 'Д': verticalCoordinateStartCell = 4; break;

                case 'Е': verticalCoordinateStartCell = 5; break;

                case 'Ж': verticalCoordinateStartCell = 6; break;

                case 'З': verticalCoordinateStartCell = 7; break;

                case 'И': verticalCoordinateStartCell = 8; break;

                case 'К': verticalCoordinateStartCell = 9; break;

                default:
                    break;
                }

                BaseShip baseShip = new BaseShip(shipDirection, shipLength, verticalCoordinateStartCell, horizontalCoordinateStartCell);
                if (HomeField.IsPossibleToSetShip(baseShip))
                {
                    HomeField.SetShip(baseShip);

                    Ships.Add(baseShip);

                    switch (shipType)
                    {
                    case ShipType.One:
                        UI.Message("Однопалубный корабль установлен!");
                        break;

                    case ShipType.Two:
                        UI.Message("Двупалубный корабль установлен!");
                        break;

                    case ShipType.Three:
                        UI.Message("Трех-палубный корабль установлен!");
                        break;

                    case ShipType.Four:
                        UI.Message("Четырех-палубный корабль установлен!");
                        break;
                    }
                    return(true);
                }
                else
                {
                    UI.ErrorMessage("Корабль невозможно установить в эти клетки", new Exception());
                }
                return(false);
            }
            catch (Exception e)
            {
                UI.ErrorMessage("При установке корабля произошла ошибка, придется повторить попытку", e);
            }
            return(false);
        }