/// <summary> /// Helper method for arrangement of boats of enemy /// </summary> /// <param name="p_boat">The Boat to be arranged</param> /// <param name="p_x">X-coordinate of the first cell of boat</param> /// <param name="p_y">Y-coordinate of the first cell of boat</param> /// <param name="p_IsHorisontal">Desired position of the boat</param> /// <returns>Returns true if the coordinates were set for each cell of the boat.</returns> private bool SetCoordinates(Boat p_boat, int p_x, int p_y, bool p_IsHorisontal) { bool IsSuccess = true; Cell _startCell; //Cell[,] Cells = Cells[0].ParentBattleField.Cells; //check the free space //TODO: improve the search of the free space! try { _startCell = Cells[p_x, p_y]; if (p_IsHorisontal) { for (int l = 0; l < p_boat.Cells.Count; l++) { if (!(Cells[_startCell.x + l, _startCell.y].Style == CellStyle.Empty) || Cells[_startCell.x + l, _startCell.y].IsSafeZone) { IsSuccess = false; } } } else//vertical { for (int l = 0; l < p_boat.Cells.Count; l++) { if (!(Cells[_startCell.x, _startCell.y + l].Style == CellStyle.Empty) || Cells[_startCell.x, _startCell.y + l].IsSafeZone) { IsSuccess = false; } } } } catch (Exception) { IsSuccess = false; } //set the coordinates if (IsSuccess) { if (p_IsHorisontal) { for (int l = 0; l < p_boat.Cells.Count; l++) { Cell _cell = Cells[p_x + l, p_y]; _cell.Style = CellStyle.HealthyCell; _cell.ParentBoat = p_boat; p_boat.Cells[l] = _cell; } } else//vertical { for (int l = 0; l < p_boat.Cells.Count; l++) { Cell _cell = Cells[p_x, p_y + l]; _cell.Style = CellStyle.HealthyCell; _cell.ParentBoat = p_boat; p_boat.Cells[l] = _cell; } } this.ConnectBoatToBF(p_boat); } //surround the boat with a safe zone SurroundBoat(p_boat, CellStyle.Empty, true); return(IsSuccess); }