/// <summary> /// Проверка на соседство с клеткой /// </summary> /// <param name="cell">Указатель на клетку</param> /// <returns>true - соседи</returns> public bool IsNeighbour(Cell cell) { if (cell == cellDown || cell == cellUp || cell == cellRight || cell == cellLeft) return true; return false; }
/// <summary> /// Инициализация /// </summary> private void Init() { // Парсит имя клетки, получает из него координаты. Затем ищет и запоминает соседние клетки try { int index1 = name.IndexOf('('); int index2 = name.IndexOf(')'); int index3 = name.LastIndexOf('('); int index4 = name.LastIndexOf(')'); int length1 = index2 - 1 - index1; int length2 = index4 - 1 - index3; if (length1 <= 0 || length2 <= 0) return; _positionX = Convert.ToInt32(name.Substring(index1 + 1, length1)); _positionY = Convert.ToInt32(name.Substring(index3 + 1, length2)); _correctCell = true; } catch { _correctCell = false; Debug.LogWarning("Incorrect cell"); return; } cellUp = GetCell(_positionX, _positionY + 1); cellDown = GetCell(_positionX, _positionY - 1); cellRight = GetCell(_positionX + 1, _positionY); cellLeft = GetCell(_positionX - 1, _positionY); }
/// <summary> /// Сравнивает объекты в трех клетках /// </summary> /// <param name="cell1">Клетка 1</param> /// <param name="cell2">Клетка 2</param> /// <param name="cell3">Клетка 3</param> /// <param name="list">Список, в который будут добавлены эти три клетке при равенстве объектов в них</param> /// <returns>true - клетки содержат объекты одного типа</returns> private bool CompareType(Cell cell1, Cell cell2, Cell cell3, ref List<Cell> list) { if (cell1 == null || cell2 == null || cell3 == null) return false; if (cell1.type == cell2.type && cell1.type == cell3.type) { if (list != null) { list.Add(cell1); list.Add(cell2); list.Add(cell3); } return true; } return false; }
/// <summary> /// Сравнивает объекты в трех клетках /// </summary> /// <param name="cell1">Клетка 1</param> /// <param name="cell2">Клетка 2</param> /// <param name="cell3">Клетка 3</param> /// <returns>true - клетки содержат объекты одного типа</returns> private bool CompareType(Cell cell1, Cell cell2, Cell cell3) { if (cell1 == null || cell2 == null || cell3 == null) return false; if (cell1.type == cell2.type && cell1.type == cell3.type) return true; return false; }
/// <summary> /// Проверяет клетку и соседние клетки на условие "три в ряд" /// При этом меняет текущую клетку и переданную в качестве параметра местами. /// Необходимо для проверки, можно ли поменять клетки местами, и при этом выполнить условие "три в ряд" /// </summary> /// <param name="cell">Клетка, которая будет менять местами с текущей</param> /// <returns></returns> public bool ThreeInARowAfterMove(Cell cell) { if (cell == null) return false; bool b = false; ObjectType t = this.type; this.type = cell.type; cell.type = t; if (CompareType(this, cellUp, cellDown) || CompareType(this, cellRight, cellLeft)) b = true; if (cellUp != null) if (CompareType(this, cellUp, cellUp.cellUp)) b = true; if (cellDown != null) if (CompareType(this, cellDown, cellDown.cellDown)) b = true; if (cellRight != null) if (CompareType(this, cellRight, cellRight.cellRight)) b = true; if (cellLeft != null) if (CompareType(this, cellLeft, cellLeft.cellLeft)) b = true; t = this.type; this.type = cell.type; cell.type = t; return b; }