public bool TouchFinish(SquareDataViewModel cell, out bool cleared) { int x, y; cleared = false; if (SelectedCells.Count == 0) { return(false); } var state = CreateGridState(); if (cell == null) { var cellIndex = SelectedCells.Last(); x = cellIndex % Definition.Width; y = cellIndex / Definition.Width; cell = Grid[x, y]; } else { FindXY(cell, out x, out y); } Debug.WriteLine($"TouchFinish: {x}, {y}: {cell}"); if (IsGameOver) { return(false); } if (SelectedLine == null) { return(false); } if (SelectedCells.Count >= 2) { int cellIndex = Definition.GetCellIndex(x, y); int lastCellIndex = SelectedCells.Last(); if (lastCellIndex == cellIndex) { lastCellIndex = SelectedCells[SelectedCells.Count - 2]; } if (!AreCellsNextToEachOther(lastCellIndex, cellIndex)) { ClearSelectedShape(); return(false); } } var allCells = Grid.Flatten().ToList(); var touchingCelling = allCells.Where(c => c.TouchState == TouchState.Touching); if (cell.Fixed && // Start or end cell.GameLine == SelectedLine && // Same as beggining (touchingCelling.Count() > 1)) // Selected more than a single cell { foreach (var touchedCell in touchingCelling) { touchedCell.TouchState = TouchState.Touched; touchedCell.MarkerVisible = false; } cell.TouchState = TouchState.Touched; cell.MarkerVisible = false; SelectedLine = null; SelectedCells.Clear(); UpdateAllCells(); if (CheckComplete()) { return(false); } GridStates.Push(state); return(true); } ClearSelectedShape(); return(false); }
public bool TouchCell(SquareDataViewModel cell) { if (cell == null) { return(false); } int x, y; FindXY(cell, out x, out y); int cellIndex = Definition.GetCellIndex(x, y); int lastCellIndex = SelectedCells.Count == 0 ? -1 : SelectedCells.Last(); Debug.WriteLine($"TouchCell: {x}, {y}: {cell}"); if (IsGameOver) { return(false); } if (SelectedLine == null) { return(false); } if (lastCellIndex == cellIndex) { return(false); // Ain't moved } if (cell.TouchState == TouchState.Touched) { return(false); } // Touching another line start/end if (cell.Fixed && cell.GameLine != SelectedLine) { return(false); } // If selected cell is already in the list. This is when the user goes backwards if (SelectedCells.Contains(cellIndex)) { // The user has back tracked var index = SelectedCells.LastIndexOf(cellIndex); while (SelectedCells.Count > index) { var last = SelectedCells.Last(); SelectedCells.RemoveAt(SelectedCells.Count() - 1); int lastX, lastY; Definition.GetCellXY(last, out lastX, out lastY); var lastCell = Grid[lastX, lastY]; lastCell.TouchState = TouchState.UnTouched; } if (index < SelectedCells.Count) { SelectedCells.RemoveRange(index + 1, SelectedCells.Count - index); } return(true); } if (lastCellIndex >= 0) { if (!AreCellsNextToEachOther(lastCellIndex, cellIndex)) { return(false); } } cell.TouchState = TouchState.Touching; cell.GameLine = SelectedLine; SelectedCells.Add(cellIndex); return(true); }