Título: torres de hanoi Publicado por: resckate 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
for (i = 1 ; i < numcols ; i++) h = 0; for (i = 0 ; i < nivel ; i++) Ficha
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
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; } } Título: Re: torres de hanoi Publicado por: [Case] 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.
Título: Re: torres de hanoi Publicado por: resckate en 10 Septiembre 2011, 20:04 pm en los static final int y los static final COlors se estan declarando metodos o variables????
Título: Re: torres de hanoi Publicado por: Valkyr 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. |