elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a la Factorización De Semiprimos (RSA)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  torres de hanoi
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: torres de hanoi  (Leído 5,467 veces)
resckate

Desconectado Desconectado

Mensajes: 8


Ver Perfil
torres de hanoi
« en: 10 Septiembre 2011, 01:48 am »

k onda a todos, pues me consegui este codigo de que resuelve las torres de hanoi, alguien puede explicarme el codigo??? porfavor!!!!!
import java.awt.*;

import java.applet.*;

public class Torre_de_Hanoi extends Applet

{

    static final int XDATS = 400;

    static final int YDOTS = 200;

    static final int NULL = -1;

    static final int MAX = 20;

    static final int MIN = 3;

    static final int MAXCOLS = 6;

    static final int MINCOLS = 3;

    static final Color cfondo = Color.blue;

    static final Color cbarra = Color.white;

    static final Color cfin = Color.red;

    static final Color ctorre = Color.green;


    boolean first = true;

    int origen, destino;

    int movimientos;

    int h[] = new int [MAXCOLS];

    int Ficha[] [] = new int [MAXCOLS] [MAX];

    int Centorr[] = new int [MAXCOLS];

    Button B[] = new Button [MAXCOLS];

    TextField TFTorres, TFCols, TFMovimientos;

    int nivel = 5, numcols = 3;


    void Parametros ()

    {

        String param;

        param = getParameter ("DISCOS");

        if (param != null)
            nivel = Integer.parseInt (param);

        param = getParameter ("COLUMNAS");

        if (param != null)
            numcols = Integer.parseInt (param);

    }


    public void init ()

    {

        int i;

        setBackground (cfondo);

        resize (XDATS, YDOTS);

        if (first)

            {

                Parametros ();

                Herramientas ();

                first = false;

            }

        for (i = 0 ; i < MAXCOLS ; i++)

            if (i < numcols)
                B .enable ();

        else
            B .disable ();


        for (i = 0 ; i < numcols ; i++)

            Centorr = (XDATS / (numcols + 1)) * (i + 1);

        h
  • = nivel;

        for (i = 1 ; i < numcols ; i++)

            h = 0;

        for (i = 0 ; i < nivel ; i++)

            Ficha
  • = nivel - i;

        movimientos = 0;

        origen = destino = NULL;

    }


    public void Herramientas ()

    {

        setLayout (new BorderLayout ());

        Panel p = new Panel ();

        p.setBackground (cfondo);

        for (int i = 0 ; i < MAXCOLS ; i++)

            p.add (B = new Button (Integer.toString (i + 1)));

       

        p.add (new Button ("Resolver"));

        p.add (new Button ("Jugar de Nuevo"));

        add ("South", p);

        Panel g = new Panel ();

        g.setBackground (cfondo);

        g.add (new Label ("Columnas"));

        g.add (TFCols = new TextField (Integer.toString (numcols)));

        g.add (new Label ("Discos"));

        g.add (TFTorres = new TextField (Integer.toString (nivel)));

        g.add (new Label ("Movimientos"));
       

        TFMovimientos = new TextField ("0", 8);

        TFMovimientos.disable ();

        g.add (TFMovimientos);

        add ("North", g);

    }


    public void Dibujar_Torres (Graphics g, Color coltorr)

    {

        int x, y, Long;

        int l, k;

        int anchomin = (Centorr [1] - Centorr
  • ) / nivel;

        int altotorre = (YDOTS - 85) / nivel;

        g.setColor (cbarra);

        for (k = 0 ; k < numcols ; k++)

            g.fillRect (Centorr [k] - 1, 35, 2, YDOTS - 75);

        g.setColor (coltorr);

        for (k = 0 ; k < numcols ; k++)

            for (l = 0 ; l < h [k] ; l++)

                {

                    Long = anchomin * Ficha [k] [l];

                    x = (Centorr [k] - (Long / 2));

                    y = YDOTS - 60 - l * altotorre;

                    g.fillRect (x, y, Long, altotorre - altotorre / 3);

                }

    }


    public void paint (Graphics g)

    {

        Dibujar_Torres (g, ctorre);

        TFMovimientos.setText (Integer.toString (movimientos));

        for (int i = 1 ; i < numcols ; i++)

            if (h == nivel)
                Final ();
           

    }


    public void Final ()

    {

        Dibujar_Torres (getGraphics (), cfin);

    }


    boolean valido (int origen, int destino)

    {

        if (origen == NULL || destino == NULL || origen == destino)
            return false;

        if (h [origen] == 0)
            return false;

        if (h [destino] == 0)
            return true;

        if (Ficha [destino] [h [destino] - 1] < Ficha [origen] [h [origen] - 1])
            return false;

        return true;

    }


    public void Resolver ()

    {

        Mover (nivel, 0, 1, numcols - 1);

    }


    void Desplazar (int origen, int destino)

    {

        h [origen]--;

        Ficha [destino] [h [destino]] = Ficha [origen] [h [origen]];

        h [destino]++;

        movimientos++;

    }


    void Mover (int Numero, int Comienzo, int Auxiliar, int Final)

    {

        int varaux;

        for (int i = Numero ; i > 0 ; i--)

            {

                Mover (i - 1, Comienzo, Final, Auxiliar);

                Desplazar (Comienzo, Final);

                update (getGraphics ());

                varaux = Comienzo;

                Comienzo = Auxiliar;

                Auxiliar = varaux;

            }

    }


    public boolean action (Event e, Object o)

    {

        if (e.target instanceof Button)

            {

                int i;

                for (i = 0 ; i < numcols ; i++)

                    if ((Integer.toString (i + 1)).equals (e.arg))

                        {

                            if (origen == NULL)

                                {

                                    origen = i;

                                    B [origen].disable ();

                                }

                            else

                                {

                                    destino = i;

                                    for (i = 0 ; i < numcols ; i++)

                                        B .enable ();

                                }

                            break;

                        }

                if ("Jugar de Nuevo".equals (e.arg) || "Resolver".equals (e.arg))

                    {

                        nivel = Integer.parseInt (TFTorres.getText ());

                        numcols = Integer.parseInt (TFCols.getText ());

                        if (nivel < MIN)
                            nivel = MIN;

                        else if (nivel > MAX)
                            nivel = MAX;

                        if (numcols < MINCOLS)
                            numcols = MINCOLS;

                        else if (numcols > MAXCOLS)
                            numcols = MAXCOLS;

                        TFTorres.setText (Integer.toString (nivel));

                        TFCols.setText (Integer.toString (numcols));

                        TFMovimientos.setText ("0");

                        init ();

                    }

                if ("Cancelar".equals (e.arg))

                    {

                        origen = destino = NULL;

                        for (i = 0 ; i < numcols ; i++)

                            B .enable ();

                    }

                if ("Resolver".equals (e.arg))
                    Resolver ();

                if (valido (origen, destino))

                    {

                        Desplazar (origen, destino);

                        origen = destino = NULL;

                    }

                repaint ();

                return true;

            }

        return false;

    }


   
}


En línea

[Case]


Desconectado Desconectado

Mensajes: 474



Ver Perfil WWW
Re: torres de hanoi
« Respuesta #1 en: 10 Septiembre 2011, 05:24 am »

¿Quieres que te expliquemos todo?, seria mejor que tu leyeras algo en google sobre como funciona el algoritmo e intentaras entender; si no entiendes algo, preguntanos y con gusto ayudaríamos.


En línea

resckate

Desconectado Desconectado

Mensajes: 8


Ver Perfil
Re: torres de hanoi
« Respuesta #2 en: 10 Septiembre 2011, 20:04 pm »

en los static final int     y    los static final COlors  se estan declarando metodos o variables????
En línea

Valkyr


Desconectado Desconectado

Mensajes: 646


Divide y vencerás


Ver Perfil
Re: torres de hanoi
« Respuesta #3 en: 10 Septiembre 2011, 22:01 pm »

en los static final int     y    los static final COlors  se estan declarando metodos o variables????

Se están declarando constantes.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Torres de hanoi con netbeans « 1 2 »
Java
kch_l 11 31,882 Último mensaje 15 Marzo 2016, 01:51 am
por isylar21
Torres de hanoi
.NET (C#, VB.NET, ASP)
S1dD3xt35 0 3,389 Último mensaje 21 Marzo 2010, 01:20 am
por S1dD3xt35
ayuda con torres de hanoi en vb
Programación Visual Basic
Freelancer 1 6,503 Último mensaje 23 Agosto 2011, 00:03 am
por ignorantev1.1
Torres de Hanoi
Programación C/C++
m@o_614 4 3,650 Último mensaje 21 Diciembre 2011, 06:02 am
por naderST
[ANSI C] Torre de hanoi
Programación C/C++
CrashNebula 3 3,555 Último mensaje 28 Mayo 2013, 17:43 pm
por CrashNebula
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines