private void MakeTiles() { tileWidth = gameViewWidth / 4; tilesArray = new ArrayList(); coordsArray = new ArrayList(); int counter = 1; #region MyRegion for (int h = 0; h < 4; h++) { for (int v = 0; v < 4; v++) { MyTextView textTile = new MyTextView(this); GridLayout.Spec rowSpec = GridLayout.InvokeSpec(h); GridLayout.Spec colSpec = GridLayout.InvokeSpec(v); GridLayout.LayoutParams tileLayoutParams = new GridLayout.LayoutParams(rowSpec, colSpec); // 0 1 2 3 v/x // 0 T T T T // 1 T T T T // 2 T T T T // 3 T T T T textTile.Text = counter.ToString(); textTile.SetTextColor(Color.Black); textTile.TextSize = 40; textTile.Gravity = GravityFlags.Center; tileLayoutParams.Width = tileWidth - 10; tileLayoutParams.Height = tileWidth - 10; tileLayoutParams.SetMargins(5, 5, 5, 5); textTile.LayoutParameters = tileLayoutParams; textTile.SetBackgroundColor(Color.Green); Point thisLoc = new Point(v, h); coordsArray.Add(thisLoc); textTile.xPos = thisLoc.X; textTile.yPos = thisLoc.Y; textTile.Touch += TextTile_Touch; tilesArray.Add(textTile); mainLayout.AddView(textTile); counter++; } } #endregion mainLayout.RemoveView((MyTextView)tilesArray[15]); tilesArray.RemoveAt(15); }
// x=2, y=3 // x=3, y=3 private void TextTile_Touch(object sender, View.TouchEventArgs e) { if (e.Event.Action == MotionEventActions.Up) { MyTextView thisTile = (MyTextView)sender; Console.WriteLine("\r\r\r this tile is at: \r x={0} y={1}", thisTile.xPos, thisTile.yPos); Console.WriteLine("\r\r\r empty tile is at: \r x={0} y={1}", emptySpot.X, emptySpot.Y); float xDiff = (float)Math.Pow(thisTile.xPos - emptySpot.X, 2); float yDiff = (float)Math.Pow(thisTile.yPos - emptySpot.Y, 2); float dist = (float)Math.Sqrt(xDiff + yDiff); if (dist == 1) { // tile can move // memorize where the tile used to be Point currPoint = new Point(thisTile.xPos, thisTile.yPos); // now take the tile to the empty GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.Y); GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.X); GridLayout.LayoutParams newLocalParams = new GridLayout.LayoutParams(rowSpec, colSpec); thisTile.xPos = emptySpot.X; thisTile.yPos = emptySpot.Y; newLocalParams.Width = tileWidth - 10; newLocalParams.Height = tileWidth - 10; newLocalParams.SetMargins(5, 5, 5, 5); thisTile.LayoutParameters = newLocalParams; emptySpot = currPoint; } } }