Exemple #1
0
        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);
        }