public void SetGameDefinition(GameDefinition gameDefinition) { IsGameOver = false; IsHighScore = false; Definition = gameDefinition; SelectedCells.Clear(); StartTime = DateTime.Now; Grid = new SquareDataViewModel[Definition.Width, Definition.Height]; GridStates = new Stack <GridState>(); Color = Definition.GameSet.Color; for (int x = 0; x < Definition.Width; x++) { for (int y = 0; y < Definition.Height; y++) { Grid[x, y] = new SquareDataViewModel { MarkerVisible = true, //HighColor = Color, //MedColor = "#ADD8E6", }; } } int index = 0; foreach (var gameLine in Definition.Lines) { int startX, startY; int endX, endY; index++; //if (string.IsNullOrWhiteSpace(gameLine.Key)) // gameLine.Key = index.ToString(); Definition.GetStartXY(gameLine, out startX, out startY); Definition.GetEndXY(gameLine, out endX, out endY); var square = Grid[startX, startY]; square.Fixed = true; square.PaletteIndex = index; square.Text = gameLine.Text; square.GameLine = gameLine; square = Grid[endX, endY]; square.Fixed = true; square.Text = gameLine.Text; square.PaletteIndex = index; square.GameLine = gameLine; } Level = string.Format("Level {0}", Definition.Index + 1); GetHighScore(); }
public bool FindXY(SquareDataViewModel cell, out int x, out int y) { x = y = 0; for (int x1 = 0; x1 < Definition.Width; x1++) { for (int y1 = 0; y1 < Definition.Height; y1++) { if (Grid[x1, y1] == cell) { x = x1; y = y1; return(true); } } } return(false); }
public bool TouchStart(SquareDataViewModel cell) { int x, y; FindXY(cell, out x, out y); Debug.WriteLine($"TouchStart: {x}, {y}: {cell}"); if (IsGameOver) { return(false); } if (cell?.GameLine == null) { return(false); } if (!cell.Fixed) { return(false); } if (cell.TouchState == TouchState.Touched) { SelectedLine = cell.GameLine; ClearSelectedShape(); return(false); } SelectedLine = cell.GameLine; SelectedCells.Add(Definition.GetCellIndex(x, y)); cell.TouchState = TouchState.Touching; return(true); }
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); }