Esempio n. 1
0
 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;
 }
Esempio n. 2
0
		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();
			}
		}
Esempio n. 3
0
		void ShowTiles(LinearLayout layout, Tile tile)
		{
			if(tile.Neighbors == 0)
				ClearNeighbors(layout, tile);
			else
				DisplayNeighborCount(layout, tile.Neighbors);
		}
Esempio n. 4
0
		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);
			}
		}
Esempio n. 5
0
		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);
		}
Esempio n. 6
0
		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();
			}
		}