• Martes 14 de Mayo de 2024, 14:22

Autor Tema:  Tres en Raya Problema  (Leído 1293 veces)

Flashtlk

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Tres en Raya Problema
« en: Viernes 1 de Enero de 2010, 03:07 »
0
Mi problema es que no se ponen la ficha del ordenador
Si hago el primer movimiento en el centro no me aparece la figurita X T_T pero si al comienzo hago click en otro lado si que aparece ...esta ahi pero no se muestra ahi dejo el code haha el algoritmo min y max lo copie del Video de Jorge Rubira  haha XD ....espero que me ayuden
Código: Java
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;;
  4.  
  5.  
  6. public class principal extends Frame implements ActionListener{
  7.  
  8.     Button  arreglobotones[][];
  9.     logica obj = new logica();
  10.     String sfiguras[];
  11.     int tablero[][];
  12.     Label l1;
  13.     Button b1 = new Button("Empezar Partida");
  14.   principal()
  15.   {
  16.        setVisible(true);
  17.         setSize(600,400);
  18.         setLayout(null);
  19.         iniciarcomponentes();
  20.         b1.addActionListener(this);
  21.   }
  22.  
  23.   public void iniciarcomponentes()
  24.   {
  25.        l1= new Label("");
  26.       arreglobotones= new Button[3][3];
  27.       sfiguras=new String[3];
  28.       sfiguras[0]="";
  29.       sfiguras[1]="0";
  30.       sfiguras[2]="X";
  31.      
  32.       for(int i =0 ;i<arreglobotones.length;i++)
  33.      {
  34.  
  35.          for(int j =0 ;j<arreglobotones[i].length;j++)
  36.          {
  37.              arreglobotones[i][j]= new Button();
  38.              arreglobotones[i][j].addActionListener(this);
  39.              arreglobotones[i][j].setBounds(80+(80*j),80+(80*i),80,80);
  40.              add(arreglobotones[i][j]);
  41.              arreglobotones[i][j].setLabel(sfiguras[0]);
  42.           }
  43.          
  44.       }
  45.       l1.setBounds(200,60,100,20);
  46.       add(l1);
  47.       b1.setBounds(350,120,100,25);
  48.       add(b1);
  49.      
  50.   }
  51.  
  52.  
  53.   public void actionPerformed(ActionEvent ev)
  54.   {
  55.       tablero=obj.tableros;
  56.       Object boton= ev.getSource();
  57.       for(int i =0 ;i<arreglobotones.length;i++)
  58.           {
  59.  
  60.          for(int j =0 ;j<arreglobotones[i].length;j++)
  61.           {
  62.              if(tablero[i][j]==-1 && arreglobotones[i][j]==boton)
  63.               {
  64.                  obj.pulsaboton(i,j);
  65.                  obj.ponerfichaordenador();
  66.                  arreglobotones[i][j].setLabel(sfiguras[tablero[i][j]+1]);
  67.                  
  68.              
  69.            
  70.               }
  71.               for(int k =0 ;k<arreglobotones.length;k++){
  72.  
  73.                    for(int l=0 ;l<arreglobotones[i].length;l++)
  74.                {
  75.                        if(tablero[k][l]==1)
  76.                        {
  77.                            arreglobotones[i][j].setLabel(sfiguras[tablero[i][j]+1]);
  78.                        }
  79.                }
  80.                   }
  81.              
  82.              
  83.              ganas();
  84.              System.out.print(tablero[i][j] + " ");
  85.          
  86.       }
  87.          System.out.println();
  88.       }
  89.       if(boton==b1)
  90.       {
  91.           obj.empezarpartida();
  92.           empieza();
  93.       }
  94.   }
  95.  
  96.   public void ganas()
  97.   {
  98.       int gano = obj.nganador;
  99.               if(gano==0)
  100.               {
  101.                   l1.setText("Has Ganado");
  102.               }
  103.               else if (gano==1)
  104.               {
  105.                   l1.setText("Has perdido");
  106.               }
  107.   }
  108.  
  109.   public void empieza()
  110.   {
  111.       l1.setText("");
  112.       for(int i =0 ;i<arreglobotones.length;i++)
  113.      {
  114.  
  115.          for(int j =0 ;j<arreglobotones[i].length;j++)
  116.          {
  117.             arreglobotones[i][j].setLabel(sfiguras[0]);
  118.  
  119.  
  120.  
  121.          }
  122.  
  123.       }
  124.   }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.     public static void main(String[] args)
  132.  
  133. {         principal obj = new principal();
  134.           obj.addWindowListener(new WindowAdapter(){
  135.            public void windowClosing(WindowEvent e){
  136.              System.exit(0);
  137.            }
  138.         }
  139.         );
  140.  
  141.  
  142.      
  143.     }
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  


Código: Java
  1.  
  2.  
  3. public class logica
  4. {
  5.       public int tableros[][]= new int[3][3];
  6.       public int nganador=-1;
  7.       int ncontar=0;
  8.  
  9.       public logica()
  10.       {
  11.           empezarpartida();
  12.           ncontar=0;
  13.       }
  14.       public void empezarpartida()
  15.       {
  16.            for(int i =0 ;i<tableros.length;i++)
  17.       {
  18.          for(int j =0 ;j<tableros[i].length;j++)
  19.           {
  20.  
  21.                 tableros[i][j]=-1;
  22.           }
  23.  
  24.  
  25.            }
  26.            nganador=-1;
  27.       }
  28.  
  29.       public void pulsaboton(int i ,int j)
  30.       {
  31.           if(i>=0 && j>=0 && j<=3 && i<=3 && tableros[i][j]==-1)
  32.           {
  33.               if(nganador==-1)
  34.               {
  35.                   tableros[i][j]=0;
  36.                   nganador=ganarpartida();
  37.                  
  38.               }
  39.           }
  40.       }
  41.  
  42.       public int ganarpartida()
  43.       {
  44.           if(tableros[0][0]!=-1 && tableros[0][0]==tableros[1][1] && tableros[0][0]==tableros[2][2])
  45.  
  46.           {
  47.               return tableros[0][0];
  48.           }
  49.           if(tableros[0][2]!=-1 && tableros[0][2]==tableros[1][1] && tableros[0][2]==tableros[2][0])
  50.  
  51.           {
  52.               return tableros[0][2];
  53.           }
  54.           for(int i =0 ;i<tableros.length;i++)
  55.           {
  56.                if(tableros[i][0]!=-1 && tableros[i][0]==tableros[i][1] && tableros[i][0]==tableros[i][2])
  57.  
  58.                {
  59.                    return tableros[i][0];
  60.                }
  61.  
  62.                if(tableros[0][i]!=-1 && tableros[0][i]==tableros[1][i] && tableros[0][i]==tableros[2][i])
  63.  
  64.                {
  65.                    return tableros[0][i];
  66.                }
  67.  
  68.           }
  69.  
  70.           return -1;
  71.  
  72.  
  73.       }
  74.  
  75.       //Algoritmo Minimax
  76.       public boolean tablerocompleto()
  77.       {
  78.            for(int i =0 ;i<tableros.length;i++)
  79.       {
  80.          for(int j =0 ;j<tableros[i].length;j++)
  81.           {
  82.              if(this.tableros[i][j]==-1)
  83.              {
  84.              return false;
  85.              }
  86.  
  87.           }
  88.  
  89.  
  90.            }
  91.            return true;
  92.       }
  93.  
  94.       public boolean finpartida()
  95.       {
  96.            return tablerocompleto() || ganarpartida()!=-1;
  97.       }
  98.  
  99.       public void ponerfichaordenador(){
  100.           if(!finpartida())
  101.           {
  102.               int f=0,c=0;
  103.               int v=Integer.MIN_VALUE;
  104.               int aux;
  105.                 for(int i =0 ;i<tableros.length;i++){
  106.                 for(int j =0 ;j<tableros[i].length;j++){
  107.                  if(tableros[i][j]==-1)
  108.                  {
  109.                   tableros[i][j]=1;
  110.                   aux=min();
  111.                   if(aux>v){
  112.                    v=aux;
  113.                    f=i;
  114.                    c=j;
  115.                   }
  116.                   tableros[i][j]=-1;
  117.                  }
  118.  
  119.             }
  120.  
  121.            }
  122.               tableros[f][c]=1;
  123.           }
  124.           nganador=ganarpartida();
  125.  
  126.  
  127.       }
  128.  
  129.       private int max()
  130.       {
  131.           if(finpartida())
  132.           {
  133.               if(ganarpartida()!=-1) return -1;
  134.               else return 0;
  135.  
  136.           }
  137.           int v =Integer.MIN_VALUE;
  138.           int aux;
  139.           for(int i =0 ;i<tableros.length;i++){
  140.                 for(int j =0 ;j<tableros[i].length;j++){
  141.                     if(this.tableros[i][j]==-1){
  142.                        tableros[i][j]=1;
  143.                        aux=min();
  144.                        if(aux>v) v=aux;
  145.                        tableros[i][j]=-1;
  146.  
  147.  
  148.  
  149.                     }
  150.                 }
  151.           }
  152.           return v;
  153.       }
  154.  
  155.       private int min()
  156.       {
  157.           if(finpartida())
  158.           {
  159.               if(ganarpartida()!=-1) return 1;
  160.               else return 0;
  161.  
  162.           }
  163.           int v =Integer.MAX_VALUE;
  164.           int aux;
  165.           for(int i =0 ;i<tableros.length;i++){
  166.                 for(int j =0 ;j<tableros[i].length;j++){
  167.                     if(this.tableros[i][j]==-1){
  168.                        tableros[i][j]=0;
  169.                        aux=max();
  170.                        if(aux<v) v=aux;
  171.                        tableros[i][j]=-1;
  172.                     }
  173.                 }
  174.           }
  175.           return v;
  176.       }
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.