/// <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); }
/// <summary> /// Sets all ships on the field. /// </summary> public void SetAllShips() { HomeField.SetPatternField(); /* if (!IsShipSettedAndIterationsNotOverflow(ShipType.Four)) * { * SetAllShips(); * return; * } * PrintHomeField(); * for (int i = 0; i < 2; i++) * { * if (!IsShipSettedAndIterationsNotOverflow(ShipType.Three)) * { * SetAllShips(); * return; * } * } * PrintHomeField(); * for (int i = 0; i < 3; i++) * { * if (!IsShipSettedAndIterationsNotOverflow(ShipType.Two)) * { * SetAllShips(); * return; * } * } * PrintHomeField(); * for (int i = 0; i < 2; i++) * { * if (!IsShipSettedAndIterationsNotOverflow(ShipType.Four)) * { * SetAllShips(); * return; * } * } * PrintHomeField(); */ }
/// <summary> /// Sets all ships on the field. /// </summary> public void SetAllShips() { UI.ImportantMessage("Начался процесс утсановки кораблей!"); UI.Message("Заполнить поле автоматически, вручную или шаблоном? А - 'Автоматически'; Р - 'Вручную'; Ш - 'Шаблон'"); string filled = "Ш"; filled = UI.ReadLine(); if (String.IsNullOrEmpty(filled)) { filled = "Ш"; } if (filled.Trim().First().ToString() == "А") { AIPlayer aiplayer = new AIPlayer(); aiplayer.SetHomeField((BaseField)HomeField); aiplayer.SetAllShips(); UI.OKMessage("Корабли утсановлены атвоматически!"); return; } if (filled.Trim().First().ToString() == "Ш") { HomeField.SetPatternField(); UI.OKMessage("Корабли утсановлены шаблоном!"); return; } UI.OKMessage("Корабли утсанавливаются вручную!"); PrintHomeField(); bool isSettedShipFour = false; while (!isSettedShipFour) { isSettedShipFour = SetShip(ShipType.Four); } PrintHomeField(); bool isSettedShipThree = false; while (!isSettedShipThree) { isSettedShipThree = SetShip(ShipType.Three); } PrintHomeField(); isSettedShipThree = false; while (!isSettedShipThree) { isSettedShipThree = SetShip(ShipType.Three); } PrintHomeField(); bool isSettedShipTwo = false; while (!isSettedShipTwo) { isSettedShipTwo = SetShip(ShipType.Two); } PrintHomeField(); isSettedShipTwo = false; while (!isSettedShipTwo) { isSettedShipTwo = SetShip(ShipType.Two); } PrintHomeField(); isSettedShipTwo = false; while (!isSettedShipTwo) { isSettedShipTwo = SetShip(ShipType.Two); } PrintHomeField(); bool isSettedShipOne = false; while (!isSettedShipOne) { isSettedShipOne = SetShip(ShipType.One); } PrintHomeField(); isSettedShipOne = false; while (!isSettedShipOne) { isSettedShipOne = SetShip(ShipType.One); } PrintHomeField(); isSettedShipOne = false; while (!isSettedShipOne) { isSettedShipOne = SetShip(ShipType.One); } PrintHomeField(); isSettedShipOne = false; while (!isSettedShipOne) { isSettedShipOne = SetShip(ShipType.One); } PrintHomeField(); }
/// <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); }
/// <summary> /// Returns a value of the indicating whether determine ship is setted and amount of iterations are limited and not overflow. /// </summary> /// <param name="shipType"></param> /// <param name="recommendedAmountIterations"></param> /// <returns>bool</returns> private bool IsShipSettedAndIterationsNotOverflow(ShipType shipType, int recommendedAmountIterations = 0) { int maxIterations = 0; int standartMaxIterations = 0; bool isSettedShip = false; switch (shipType) { case ShipType.One: standartMaxIterations = 200; break; case ShipType.Two: standartMaxIterations = 200; break; case ShipType.Three: standartMaxIterations = 200; break; case ShipType.Four: standartMaxIterations = 200; break; } switch (shipType) { case ShipType.One: maxIterations = (recommendedAmountIterations > 50 ? recommendedAmountIterations : standartMaxIterations); while (!isSettedShip) { isSettedShip = SetShip(ShipType.One); if (maxIterations < 0) { HomeField.ClearField(); return(false); } else { maxIterations--; } } break; case ShipType.Two: maxIterations = (recommendedAmountIterations > 50 ? recommendedAmountIterations : standartMaxIterations); while (!isSettedShip) { isSettedShip = SetShip(ShipType.Two); if (maxIterations < 0) { HomeField.ClearField(); return(false); } else { maxIterations--; } } break; case ShipType.Three: maxIterations = (recommendedAmountIterations > 50 ? recommendedAmountIterations : standartMaxIterations); while (!isSettedShip) { isSettedShip = SetShip(ShipType.Three); if (maxIterations < 0) { HomeField.ClearField(); return(false); } else { maxIterations--; } } break; case ShipType.Four: maxIterations = (recommendedAmountIterations > 50 ? recommendedAmountIterations : standartMaxIterations); while (!isSettedShip) { isSettedShip = SetShip(ShipType.Four); if (maxIterations < 0) { HomeField.ClearField(); return(false); } else { maxIterations--; } } break; } return(true); }