private bool IsUpEdge(Cell cell) { return cell.Row == 0; }
//private bool CheckLinked(Cell from, Cell to) //{ // // Если на одной строке и столбцы соседние и концы трубы совмещены // if (IsNear(from, to) && IsCompatible(from, to)) // { // return true; // } // // Если на одном столбце и строки соседние и концы трубы совмещены // if (IsNear(from, to) && IsCompatible(from, to)) // { // return true; // } // return false; //} private bool IsNear(Cell c1, Cell c2) { if ((c1.Row == c2.Row) && (Math.Abs(c1.Col - c2.Col) == 1)) return true; if ((c1.Col == c2.Col) && (Math.Abs(c1.Row - c2.Row) == 1)) return true; return false; }
private bool IsRightEdge(Cell cell) { return cell.Col == Width - 1; }
private bool IsLeftEdge(Cell cell) { return cell.Col == 0; }
private bool IsFree(Cell cell) { return cell.Value == EMPTY; }
private bool IsDownEdge(Cell cell) { return cell.Row == Height - 1; }
private void InitField() { FinalPipes = new List<Cell>(); PipeLength = 0; _field = new Cell[Height, Width]; for (int row = 0; row < Height; row++) { for (int col = 0; col < Width; col++) { _field[row, col] = new Cell(row, col, EMPTY); } } NextCell = _field[0, 0]; }
public bool IsCompatible(Cell cell1, Cell cell2) { if (cell1.Value == 0) // Проверка для ! { if (cell2.Value == 0) { if (cell1.Col == cell2.Col && Math.Abs(cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2)) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } if (!IsDownEdge(cell2)) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 2 || cell2.Value == 3) { if (cell1.Col == cell2.Col && (cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 2) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsLeftEdge(cell2) && cell2.Value == 3) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } } else if (cell2.Value == 4 || cell2.Value == 5) { if (cell1.Col == cell2.Col && (cell2.Row - cell1.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 4) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsLeftEdge(cell2) && cell2.Value == 5) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } } } else if (cell1.Value == 1) // Проверка для - { if (cell2.Value == 1) { if (cell1.Row == cell2.Row && Math.Abs(cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if ( !IsRightEdge(cell2) ) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if ( !IsLeftEdge(cell2) ) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } } else if (cell2.Value == 2 || cell2.Value == 4) { if (cell1.Row == cell2.Row && (cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2) && cell2.Value == 4) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } if (!IsDownEdge(cell2) && cell2.Value == 2) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 3 || cell2.Value == 5) { if (cell1.Row == cell2.Row && (cell2.Col - cell1.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2) && cell2.Value == 5) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } if (!IsDownEdge(cell2) && cell2.Value == 3) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } } else if (cell1.Value == 2) // Проверка для Г { if (cell2.Value == 5) { if (cell1.Col == cell2.Col && (cell2.Row - cell1.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsLeftEdge(cell2)) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } if (cell1.Row == cell2.Row && (cell2.Col - cell1.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2)) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 0 || cell2.Value == 4) { if (cell1.Col == cell2.Col && (cell2.Row - cell1.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 4) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsDownEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 1 || cell2.Value == 3) { if (cell1.Row == cell2.Row && (cell2.Col - cell1.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 1) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsDownEdge(cell2) && cell2.Value == 3) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } } else if (cell1.Value == 3) // Проверка для 7 { if (cell2.Value == 4) { if (cell1.Col == cell2.Col && (cell2.Row - cell1.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2)) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } #endregion return true; } if (cell1.Row == cell2.Row && (cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2)) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 1 || cell2.Value == 2) { if (cell1.Row == cell2.Row && (cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsLeftEdge(cell2) && cell2.Value == 1) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } if (!IsDownEdge(cell2) && cell2.Value == 2) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 0 || cell2.Value == 5) { if (cell1.Col == cell2.Col && (cell2.Row - cell1.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsLeftEdge(cell2) && cell2.Value == 5) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } if (!IsDownEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } } else if (cell1.Value == 4) // Проверка для L { if (cell2.Value == 3) { if (cell1.Col == cell2.Col && (cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsLeftEdge(cell2)) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } if (cell1.Row == cell2.Row && (cell2.Col - cell1.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsDownEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 0 || cell2.Value == 2) { if (cell1.Col == cell2.Col && (cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 2) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsUpEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 1 || cell2.Value == 5) { if (cell1.Row == cell2.Row && (cell2.Col - cell1.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2) && cell2.Value == 1) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } if (!IsUpEdge(cell2) && cell2.Value == 5) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } #endregion return true; } } } else if (cell1.Value == 5) // Проверка для j { if (cell2.Value == 2) { if (cell1.Col == cell2.Col && (cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsRightEdge(cell2)) { if (IsFree(_field[cell2.Row, cell2.Col + 1])) { NextCell = _field[cell2.Row, cell2.Col + 1]; } } #endregion return true; } if (cell1.Row == cell2.Row && (cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsDownEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row + 1, cell2.Col])) { NextCell = _field[cell2.Row + 1, cell2.Col]; } } #endregion return true; } } else if (cell2.Value == 0 || cell2.Value == 3) { if (cell1.Col == cell2.Col && (cell1.Row - cell2.Row) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2) && cell2.Value == 0) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } if (!IsLeftEdge(cell2) && cell2.Value == 3) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } } else if (cell2.Value == 1 || cell2.Value == 4) { if (cell1.Row == cell2.Row && (cell1.Col - cell2.Col) == 1) { #region Выбор следующей ячейки NextCell = null; if (!IsUpEdge(cell2) && cell2.Value == 4) { if (IsFree(_field[cell2.Row - 1, cell2.Col])) { NextCell = _field[cell2.Row - 1, cell2.Col]; } } if (!IsLeftEdge(cell2) && cell2.Value == 1) { if (IsFree(_field[cell2.Row, cell2.Col - 1])) { NextCell = _field[cell2.Row, cell2.Col - 1]; } } #endregion return true; } } } NextCell = null; return false; }