예제 #1
0
        private void makeTilesMethod()
        {
            tileWidth = gameViewWidth / 4;

            tilesArr  = new ArrayList();
            coordsArr = new ArrayList();

            int counter = 1;

            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);

                    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.LightGreen);

                    Point thisLoc = new Point(v, h);
                    coordsArr.Add(thisLoc);

                    textTile.xPos = thisLoc.X;
                    textTile.yPos = thisLoc.Y;

                    textTile.Touch += TextTile_Touch;

                    tilesArr.Add(textTile);

                    mainLayout.AddView(textTile);

                    counter++;
                }
            }

            mainLayout.RemoveView((MyTextView)tilesArr[15]);
            tilesArr.RemoveAt(15);
        }
예제 #2
0
        void TextTile_Touch(object sender, View.TouchEventArgs e)
        {
            if (e.Event.Action == MotionEventActions.Up)
            {
                if (tilesArr.Contains(sender))
                {
                    MyTextView thisTile = (MyTextView)sender;

                    Console.WriteLine("Tile Position X:{0} -- Y:{1}", thisTile.xPos, thisTile.yPos);
                    Console.WriteLine("Empty Position X:{0} -- Y:{1}", emptySpot.X, emptySpot.Y);

                    float xDif = (float)Math.Pow(thisTile.xPos - emptySpot.X, 2);
                    float yDif = (float)Math.Pow(thisTile.yPos - emptySpot.Y, 2);

                    float dist = (float)Math.Sqrt(xDif + yDif);

                    if (dist == 1) // si es verdadero, puede mover
                    {
                        // memoriza donde estaba el tile
                        Point curPoint = new Point(thisTile.xPos, thisTile.yPos);

                        // mueve el tile al lugar vacio
                        GridLayout.Spec rowSpec = GridLayout.InvokeSpec(emptySpot.Y);
                        GridLayout.Spec colSpec = GridLayout.InvokeSpec(emptySpot.X);

                        GridLayout.LayoutParams newLocParams = new GridLayout.LayoutParams(rowSpec, colSpec);

                        thisTile.xPos = emptySpot.X;
                        thisTile.yPos = emptySpot.Y;

                        newLocParams.Width  = tileWidth - 10;
                        newLocParams.Height = tileWidth - 10;
                        newLocParams.SetMargins(5, 5, 5, 5);

                        thisTile.LayoutParameters = newLocParams;

                        // el vacio se mueve donde estaba el tile que tocamos
                        emptySpot = curPoint;
                    }
                }
            }
        }