/// <summary> /// Помечает корабль на поле, как разрушенный и выделяет вокруг /// </summary> /// <param name="ship">Уничтоженный корабль</param> private void DestroyShip(Ship ship) { var length = ship.Decks.Length - 1; var horizontalDirection = ship.GetDirection(); for (var row = ship.Decks[0].Location.Y - 1; row <= ship.Decks[0].Location.Y + 1 + (horizontalDirection ? 0 : length); row++) { for (var column = ship.Decks[0].Location.X - 1; column <= ship.Decks[0].Location.X + 1 + (horizontalDirection ? length : 0); column++) { if (!FieldGenerator.CellInField(Field.GetLength(1), Field.GetLength(0), column, row)) { continue; } switch (Field[row, column]) { case CellType.DamagedShip: Field[row, column] = CellType.DestroyedShip; break; case CellType.Water: case CellType.FogOverWater: Field[row, column] = CellType.EmptyWater; break; } } } }
/// <summary> /// Начало новой игры /// </summary> /// <returns>true, если удалось создать и заполнить поля для игроков</returns> public bool NewGame() { _player = FieldGenerator.TryGenerateField(AHorizontalCells, AVerticalCells, _fiveDecks, _fourDecks, _threeDecks, _doubleDecks, _oneDeckeds, PlayerType.Player, 10, 10); _pc = FieldGenerator.TryGenerateField(AHorizontalCells, AVerticalCells, _fiveDecks, _fourDecks, _threeDecks, _doubleDecks, _oneDeckeds, PlayerType.Pc, 10, 10); if (_player == null || _pc == null) return false; _player.NewGame(this); _pc.NewGame(this); FieldGenerator.HideField(_pc.Field); _aiMove = false; Refresh(); return true; }
/// <summary> /// Проверяет, свободна ли клетка на поле в шаге от выбранной в указанном направлении /// </summary> /// <param name="field">Игровое поле</param> /// <param name="selectedPoint">Выбранная точка для проверки</param> /// <param name="step">Шаг</param> /// <param name="direction">Направление</param> /// <returns>true, если клетка в границах поля и ни чем не занята</returns> private bool CellFree(CellType[,] field, Point selectedPoint, int step, Direction direction) { var horizontalCells = field.GetLength(1); var verticalCells = field.GetLength(0); var x = selectedPoint.X + (direction == Direction.Right ? step : (direction == Direction.Left ? -step : 0)); var y = selectedPoint.Y + (direction == Direction.Bot ? step : (direction == Direction.Top ? -step : 0)); return (FieldGenerator.CellInField(horizontalCells, verticalCells, x, y) && field[y, x] != CellType.DamagedShip && field[y, x] != CellType.DestroyedShip && field[y, x] != CellType.EmptyWater); }