Ver Mensaje Individual
  #5 (permalink)  
Antiguo 21/10/2011, 17:13
link01
 
Fecha de Ingreso: octubre-2011
Mensajes: 58
Antigüedad: 12 años, 7 meses
Puntos: 0
Respuesta: Pasar de código estructurado a Orientado a Objetos

Código Java:
Ver original
  1. //Todas los metodos
  2. package Ahorcaito;
  3.  
  4.  
  5.  
  6. import java.awt.Font;
  7.  
  8. import java.awt.event.ActionEvent;
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. public class Funciones {
  17.  
  18.     private Font tamano, tamanoG;
  19.  
  20.     private double posicionDEpalabra;
  21.  
  22.     private char[] arrayGuiones;
  23.  
  24.     private char[] letra;
  25.  
  26.     private String[] agregar = new String[10];
  27.  
  28.     private boolean chequearif;
  29.  
  30.     private int fallos=0, k=0;
  31.  
  32.     private String palabraElegida = "", letraEscogida = "", auxLetra = "", guionesBajos = "", guionesBajosExternos = "", string = "", solucion = "";
  33.  
  34.     private String[] lista_de_palabras = {"perro","vaca","chivo","oveja","canguro","gallina", "pato", "conejo", "aguila","avestrus",
  35.  
  36.               "caracol","abeja","babosa","caballo","delfin","elefante","halcon","jabali","koala","lagarto","mosca"};
  37.  
  38.     Textos texto = new Textos();
  39.  
  40.     PanelImg pimg = new PanelImg();
  41.  
  42.    
  43.  
  44.             public void elegirPalabra(){
  45.  
  46.                 posicionDEpalabra = Math.random()*21;
  47.  
  48.                 palabraElegida = lista_de_palabras[(int)posicionDEpalabra];
  49.  
  50.             }
  51.  
  52.  
  53.  
  54.             public void presentarRayas(){
  55.  
  56.                 for(int i=0; i<palabraElegida.length(); i++){
  57.  
  58.                     guionesBajosExternos +="_ ";
  59.  
  60.                 }
  61.  
  62.                 texto.lbl_palabra.setText(guionesBajosExternos);
  63.  
  64.                 tamanoG = texto.lbl_palabra.getFont();
  65.  
  66.                 texto.lbl_palabra.setFont(new Font(tamanoG.getFontName(),tamanoG.getStyle(), 20));
  67.  
  68.             }
  69.             public void NumRayasInterno(){
  70.  
  71.                 for(int i=0; i<palabraElegida.length(); i++){
  72.  
  73.                     guionesBajos += "_";
  74.  
  75.                 }
  76.  
  77.                 arrayGuiones = guionesBajos.toCharArray();
  78.  
  79.             }
  80.  
  81.             public void actionPerformed(ActionEvent e) {
  82.  
  83.                 if(e.getSource() == Menu.salir)
  84.  
  85.                     System.exit(0);
  86.  
  87.                 if(e.getSource() == PanelLetras.boton){
  88.  
  89.                     letraEscogida = (((String) PanelLetras.jcb.getSelectedItem()).toLowerCase());
  90.  
  91.                     agregar[k] = letraEscogida.toUpperCase();
  92.  
  93.                     k++;
  94.  
  95.                     PanelLetras.jcb.removeItem(PanelLetras.jcb.getSelectedItem());
  96.  
  97.                     auxLetra += letraEscogida;
  98.  
  99.                     auxLetra = auxLetra.toUpperCase();
  100.  
  101.                     texto.lbl_letras.setText(auxLetra+=",  ");
  102.  
  103.                     buscarLetra();
  104.  
  105.                 }
  106.  
  107.                 if(e.getSource() == Menu.reiniciar)
  108.  
  109.                     reiniciar();
  110.  
  111.             }
  112.             public void buscarLetra(){
  113.  
  114.                 chequearif = false;
  115.  
  116.                 string = "";
  117.  
  118.                 solucion = "";
  119.  
  120.                 letra = letraEscogida.toCharArray();
  121.  
  122.                 int index = 0;
  123.  
  124.                 for(int i=0;i<(palabraElegida.length());i++){
  125.  
  126.                     if(palabraElegida.substring(i, i+1).equals(letraEscogida) == true){
  127.  
  128.                         if(chequearif == true){
  129.  
  130.                             string = "";
  131.  
  132.                             solucion = "";
  133.  
  134.                             index = palabraElegida.indexOf(letraEscogida, index+1);
  135.  
  136.                             arrayGuiones[index] = letra[0];
  137.  
  138.                             for(int j=0;j<arrayGuiones.length;j++){
  139.  
  140.                                 string += arrayGuiones[j]+" ";
  141.  
  142.                                 solucion += arrayGuiones[j];
  143.  
  144.                             }
  145.  
  146.  
  147.  
  148.                             texto.lbl_palabra.setText(string);
  149.  
  150.                         }
  151.  
  152.                         if(chequearif == false){
  153.  
  154.                             index = palabraElegida.indexOf(letraEscogida);
  155.  
  156.                             arrayGuiones[index] = letra[0];
  157.  
  158.                             for(int j=0;j<arrayGuiones.length;j++){
  159.  
  160.                                 string += arrayGuiones[j]+" ";
  161.  
  162.                                 solucion += arrayGuiones[j];
  163.  
  164.                             }
  165.  
  166.                             texto.lbl_palabra.setText(string);
  167.  
  168.                             chequearif = true;
  169.  
  170.                         }
  171.  
  172.                     }
  173.  
  174.                 }
  175.  
  176.                
  177.  
  178.                 ganarPerder();
  179.  
  180.             }
  181.             public void ganarPerder(){
  182.                 if(solucion.equals(palabraElegida) == true){
  183.  
  184.                     texto.lbl_msj.setText("¡HA GANADO! ¡FELICIDADES!");
  185.  
  186.                     tamano = texto.lbl_msj.getFont();
  187.  
  188.                     texto.lbl_msj.setFont(new Font(tamano.getFontName(),tamano.getStyle(),18));
  189.  
  190.                     PanelLetras.jcb.setEnabled(false);
  191.  
  192.                     PanelLetras.boton.setEnabled(false);
  193.  
  194.                 }
  195.                 if(chequearif == false){
  196.  
  197.                     fallos=fallos+1;
  198.  
  199.                     texto.lbl_img.setIcon(pimg.imagen[fallos]);
  200.  
  201.                     texto.lbl_msj.setText("Le quedan " +(6-fallos)+ " intentos");
  202.  
  203.                     if(fallos == 6){
  204.  
  205.                         texto.lbl_msj.setText("¡HA PERDIDO!\n La palabra era: " +palabraElegida);
  206.  
  207.                         tamano = texto.lbl_msj.getFont();
  208.  
  209.                         texto.lbl_msj.setFont(new Font(tamano.getFontName(),tamano.getStyle(),13));
  210.  
  211.                         PanelLetras.jcb.setEnabled(false);
  212.  
  213.                         PanelLetras.boton.setEnabled(false);
  214.  
  215.                     }
  216.  
  217.                 }
  218.  
  219.             }
  220.             public void reiniciar(){
  221.  
  222.                 PanelLetras.jcb.setEnabled(true);
  223.  
  224.                 PanelLetras.boton.setEnabled(true);
  225.  
  226.                 fallos = 0;
  227.  
  228.                 texto.lbl_img.setIcon(pimg.imagen[0]);
  229.  
  230.                 texto.lbl_msj.setText("Le quedan 6 intentos");
  231.  
  232.                 texto.lbl_palabra.setText("");
  233.  
  234.                 texto.lbl_letras.setText("");
  235.  
  236.                 auxLetra = "";
  237.  
  238.                 solucion= "";
  239.  
  240.                 string = "";
  241.  
  242.                 chequearif = false;
  243.                 guionesBajos = "";
  244.                 guionesBajosExternos = "";
  245.                 elegirPalabra();
  246.                 presentarRayas();
  247.                 NumRayasInterno();
  248.                 for(int i=0;i<k;i++){
  249.                     PanelLetras.jcb.addItem(agregar[i]);
  250.                 }
  251.                 k=0;
  252.             }        
  253. }
__________________
La libertad más difícil de conservar es la de equivocarse. - Morris Wes

Lo que faltaba en internet: http://binar10s.blogspot.com/