//Marcar un boton del grid
        private void Marcar(LetraTablero lb)
        {
            lb.AltToggle();

            if (lb.IsToggled)
            {
                lb.Scale           = 1.3;
                lb.BorderWidth     = 1.7;
                lb.FontAttributes  = FontAttributes.Bold;
                lb.BackgroundColor = Color.FromHex("#bfbfbf");
                if (Activos.Count > 0)
                {
                    LetraTablero anterior = Activos[Activos.Count - 1];
                    anterior.BackgroundColor = Color.FromHex("#e6e6e6");
                    anterior.FontAttributes  = FontAttributes.None;
                }
                Activos.Add(lb);
            }
            else
            {
                Activos.RemoveAt(Activos.Count - 1);
                lb.Scale           = 1;
                lb.BorderWidth     = 1;
                lb.BackgroundColor = Color.FromHex("#ffffff");
                lb.FontAttributes  = FontAttributes.None;
                if (Activos.Count > 0)
                {
                    LetraTablero anterior = Activos[Activos.Count - 1];
                    anterior.BackgroundColor = Color.FromHex("#bfbfbf");
                    anterior.FontAttributes  = FontAttributes.Bold;
                }
            }
        }
        /* Metodo para limpiar la lista de palabras activadas. */
        public void LimpiarActivos(bool marcar)
        {
            if (Activos.Count == 0)
            {
                return;
            }

            LetraTablero actual = null;

            for (int i = 0; i < Activos.Count; i++) //mas rapido que foreach
            {
                actual                 = Activos[i];
                actual.Scale           = 1;
                actual.BorderWidth     = 1;
                actual.BackgroundColor = Color.FromHex("#ffffff");
                if (marcar)
                {
                    actual.BorderColor = Color.Red;
                    actual.TextColor   = Color.Red;
                }

                actual.AltToggle();
            }
            actual.FontAttributes = FontAttributes.None;
            Activos.Clear();
        }
        /*  Revisa donde esta el actual desde el anterior
         *  -1 = no determinado
         *  0 = no cercano
         *  1 = abajo
         *  2 = abajo derecha
         *  3 = derecha
         *  4 = arriba derecha
         *  5 = arriba
         *  6 = arriba izquierda
         *  7 = izquierda
         *  8 = abajo izquierda */
        private int DetDireccion(LetraTablero lt, int dir = 0)
        {
            if (Activos.Count == 0) //no se ha determinado direccion
            {
                return(-1);
            }

            int i   = lt.I;
            int j   = lt.J;
            int max = tableroButtons.Length;

            if ((dir == 0 || dir == 1) && (j - 1) >= 0 && tableroButtons[i][j - 1] == Activos[Activos.Count - 1])
            {
                return(1);
            }

            if (i - 1 >= 0 && dir < 5)
            {
                --i;
                if ((dir == 0 || dir == 2) && (j - 1) >= 0 && tableroButtons[i][j - 1] == Activos[Activos.Count - 1])
                {
                    return(2);
                }
                if ((dir == 0 || dir == 3) && tableroButtons[i][j] == Activos[Activos.Count - 1])
                {
                    return(3);
                }
                if ((dir == 0 || dir == 4) && (j + 1) < max && tableroButtons[i][j + 1] == Activos[Activos.Count - 1])
                {
                    return(4);
                }
                ++i;
            }//revisando hacia la derecha

            //arriba
            if ((dir == 0 || dir == 5) && (j + 1) < max && tableroButtons[i][j + 1] == Activos[Activos.Count - 1])
            {
                return(5);
            }

            if (i + 1 < max && dir < 9)
            {
                ++i;
                if ((dir == 0 || dir == 6) && (j + 1) < max && tableroButtons[i][j + 1] == Activos[Activos.Count - 1])
                {
                    return(6);
                }
                if ((dir == 0 || dir == 7) && tableroButtons[i][j] == Activos[Activos.Count - 1])
                {
                    return(7);
                }
                if ((dir == 0 || dir == 8) && (j - 1) >= 0 && tableroButtons[i][j - 1] == Activos[Activos.Count - 1])
                {
                    return(8);
                }
            }//revisando hacia izquierda

            return(0); //no esta cerca
        }
        //Eventos de los botones en el grid
        private void OnTableroButtonClicked(object sender, EventArgs args)
        {
            LetraTablero b = (LetraTablero)sender;

            if (Activos.Count == 1)
            {
                direccion = DetDireccion(b);
            }

            if (Cercania(b, !b.IsToggled))
            {
                Marcar(b);
            }//if Cercania
        }
        /* Valida si hay un boton activo cercano al ultimo que se pulso,
         * y si va en la misma direccion */
        private bool Cercania(LetraTablero lb, bool avanza)
        {
            //si no hay nada en la lista siempre es cercano, igual cuando va deshaciendo el camino
            if (Activos.Count == 0 || (!avanza && lb == Activos[Activos.Count - 1]))
            {
                return(true);
            }

            int dir = DetDireccion(lb, direccion);

            if (dir != 0)
            {
                return(true);
            }

            return(false);
        }
        //METODOS PRIVADOS-----------------------------------

        /* Metodo que inicializa todo el juego: tablero, tiempo, lista de palabras,
         * puntuaciones. */
        private void Inicializar(Grid tabLayout = null)
        {
            int i, nivel, numLista;

            nivel          = contenedor.Nivel;
            numLista       = contenedor.EstiloPalabras;
            _totalPalabras = 4 + (nivel * 2);
            //generar _tablero
            if (_tablero == null)
            {
                _tablero = new Tablero(nivel, numLista);
            }
            else
            {
                _tablero.Inicializar(nivel, numLista);
            }

            //cargar lista de palabras en etiquetas
            if (LabelPalabras.Count != 0)
            {
                PistasPage.LimpiarLabels();
                LabelPalabras.Clear();
            }
            for (i = 0; i < TotalPalabras; i++)
            {
                LabelPalabras.Add(new Label {
                    Text = GetPalabra(i)
                });
            }

            Activos.Clear();
            LabelPalabras.TrimExcess();

            //vaciar grid de tablero
            if (TableroLayout != null)
            {
                TableroLayout.Children.Clear();
                TableroLayout.RowDefinitions.Clear();
                TableroLayout.ColumnDefinitions.Clear();
            }
            if (tabLayout != null) //en teoria es para la primera vez
            {
                TableroLayout = tabLayout;
            }

            //inicializar colunas y filas del grid
            i      = 0;
            nivel += 6; //reutilizo n
            while (i < nivel)
            {
                TableroLayout.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(1, GridUnitType.Star)
                });
                TableroLayout.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });
                ++i;
            }

            //cargar labels e insertarlas en el grid
            tableroButtons = new LetraTablero[nivel][];
            char[][] tab = GetTablero().GetMatriz();
            for (i = 0; i < nivel; i++)
            {
                tableroButtons[i] = new LetraTablero[nivel];
                for (int j = 0; j < nivel; j++)
                {
                    tableroButtons[i][j]          = new LetraTablero(i, j, tab[i][j]);
                    tableroButtons[i][j].Clicked += OnTableroButtonClicked;
                    TableroLayout.Children.Add(tableroButtons[i][j], i, j);
                } //for j
            }     //for i

            //actualizar la puntuacion cuando se pasa de nivel o se reinicia tablero
            _puntuacion = -1;
            contenedor.AumentarPuntuacion();
        }