public static IList<Tile> Neighbors(Tile tile, int size) { var neighbors = new List<Tile>(); // Top Left neighbors.Add(new Tile { X = tile.X - 1, Y = tile.Y - 1, Position = tile.Position - (size + 1) }); // Left neighbors.Add(new Tile { X = tile.X - 1, Y = tile.Y, Position = tile.Position - 1 }); // Bottom Left neighbors.Add(new Tile { X = tile.X - 1, Y = tile.Y + 1, Position = tile.Position + (size - 1) }); // Top neighbors.Add(new Tile { X = tile.X, Y = tile.Y - 1, Position = tile.Position - size }); // Bottom neighbors.Add(new Tile { X = tile.X, Y = tile.Y + 1, Position = tile.Position + size }); // Top Right neighbors.Add(new Tile { X = tile.X + 1, Y = tile.Y - 1, Position = tile.Position - (size - 1) }); // Right neighbors.Add(new Tile { X = tile.X + 1, Y = tile.Y, Position = tile.Position + 1 }); // Bottom Right neighbors.Add(new Tile { X = tile.X + 1, Y = tile.Y + 1, Position = tile.Position + (size + 1) }); neighbors.RemoveAll(t => t.X < 0 || t.Y < 0 || t.X >= size || t.Y >= size); return neighbors; }
void SetFlag(Tile tile) { var layout = _subViews[tile.Position]; if(tile.Flagged) { if (!_checked.Any(t => t.Position == tile.Position)) _checked.Add(tile); var img = new ImageView(this); img.SetImageResource(Resource.Drawable.flag); layout.AddView(img); } else { if (_checked.Any(t => t.Position == tile.Position)) _checked.Remove(_checked.First(t => t.Position == tile.Position)); //layout.SetBackgroundColor(Resources.GetColor(Resource.Color.white)); layout.RemoveAllViews(); } }
void ShowTiles(LinearLayout layout, Tile tile) { if(tile.Neighbors == 0) ClearNeighbors(layout, tile); else DisplayNeighborCount(layout, tile.Neighbors); }
void ClearNeighbors(LinearLayout tileLayout,Tile tile) { Flip(tileLayout); foreach(var n in Helpers.Neighbors(tile, _size)) { if(_checked.Any(t => t.Position == n.Position)) continue; _checked.Add(n); n.Neighbors = Helpers.CountNeighbors(_mines, n.X, n.Y); var layout = _subViews[n.Position]; ShowTiles(layout, n); } }
void TileLongClick (object sender, View.LongClickEventArgs e) { // Set/Release flag var tileLayout = (LinearLayout)sender; var position = _grid.IndexOfChild(tileLayout); var tile = _checked.FirstOrDefault(t => t.Position == position); if(tile == null) { int numberOfColumns = _grid.ColumnCount; int row = Convert.ToInt32(Math.Floor((double)(position/numberOfColumns))); int column = position - (row * numberOfColumns); var neighbors = Helpers.CountNeighbors(_mines, column, row); tile = new Tile { X = column, Y = row, Neighbors = neighbors, Position = position }; } tile.Flagged = !tile.Flagged; //_checked.Where(t => t.Position == position).First() = tile; SetFlag(tile); }
void TileClick (object sender, EventArgs e) { var tileLayout = (LinearLayout)sender; var position = _grid.IndexOfChild(tileLayout); int numberOfColumns = _grid.ColumnCount; int row = Convert.ToInt32(Math.Floor((double)(position/numberOfColumns))); int column = position - (row * numberOfColumns); var neighbors = Helpers.CountNeighbors(_mines, column, row); var tile = new Tile { X = column, Y = row, Neighbors = neighbors, Position = position }; _checked.Add(tile); if(_mines.Exists(m => m.X == column && m.Y == row)) { // You lose :( tileLayout.SetBackgroundColor(Color.Red); foreach(var v in _subViews) { var p = _grid.IndexOfChild(v); if(_checked.Any(t => t.Position == p)) continue; v.Click -= TileClick; v.LongClick -= TileLongClick; } } else { ShowTiles(tileLayout, tile); UpdateScore(); } }