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

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


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

Desconectado Desconectado

Mensajes: 16


Ver Perfil
Ayuda conecta4
« en: 16 Junio 2013, 17:56 pm »

Hola buenas estoy acabando el conecta4 y por ahora me funciona correctamente, pero el equipo es un numero aleatorio y queria que tuviera la posibilidad de ganar, me podrian explicar alguna manera sencilla, este es mi codigo:
import java.awt.*;
import javax.swing.ImageIcon;
public class cuatroenraya extends javax.swing.JFrame {
    public cuatroenraya() {
        initComponents();
        getContentPane().setBackground(Color.black);
        this.jTextField1.setVisible(false);
        this.jButton4.setVisible(false);
        this.jTextField2.setVisible(false);
        this.jButton5.setVisible(false);
        this.jButton6.setVisible(false);
    }
    static int vector[] = new int [7];
    static int tablero[][] = new int[6][7];
    int turnoj1=1;
    int turnoj2=1;
    boolean finpartida;
    boolean numjug;
    public static void inicializotablero()
    {
        for(int i=0;i<6;i++)
        {
            for (int j=0;j<7;j++)
            {
                tablero[j]=0;
            }
        }
        for(int i=0;i<7;i++)
        {
            vector=0;
        }
    }
    void movimientomaquina()
    {
         for(int i=0;i<6;i++)
        {
            for (int j=0;j<7;j++)
            {
                if(tablero[j]!=1)
                {
                    if(tablero[j+1]!=1)
                    {
                        if(tablero[j+2]!=1)
                        {
                           
                        }
                    }
                }
            }
        }
    }
    public static boolean ganadora()
    { 
        boolean gan=false;
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<7;j++)
            {
                ganar(i,j);
                if(ganar(i,j)==true)
                {
                   gan=true;
                }
            }
        }
        return gan;
    }
    public static void menu() throws Exception
    {
        inicializotablero();
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        ImageIcon fondo = new ImageIcon(new ImageIcon(getClass().getResource("fondo.jpg")).getImage());
        g.drawImage(fondo.getImage(), -150, 0, null);
        dibujartablero(g);
       
    }
    void comprobarganadormj()
    {
        if(ganadora()==true)
        {
            if(turnoj1==turnoj2)
            {
                this.jLabel1.setText("Gana el jugador 2");
                this.jButton4.setVisible(false);
                this.jButton5.setVisible(false);
                this.jButton6.setVisible(false);
                this.jTextField1.setVisible(false);
                this.jTextField2.setVisible(false);
            }
            if(turnoj1>turnoj2)
            {
                this.jLabel1.setText("Gana el jugador 1");
                this.jButton4.setVisible(false);
                this.jButton5.setVisible(false);
                this.jButton6.setVisible(false);
                this.jTextField1.setVisible(false);
                this.jTextField2.setVisible(false);
            }
        }
    }
    public void comprobarganadorj()
    {
        if(ganadora()==true)
        {
            if(turnoj1==turnoj2)
            {
                this.jLabel1.setText("Gana el jugador 2");
                this.jButton4.setVisible(false);
                this.jButton5.setVisible(false);
                this.jButton6.setVisible(false);
                this.jTextField1.setVisible(false);
                this.jTextField2.setVisible(false);
            }
            if(turnoj1>turnoj2)
            {
                this.jLabel1.setText("Gana el jugador 1");
                this.jButton4.setVisible(false);
                this.jButton5.setVisible(false);
                this.jButton6.setVisible(false);
                this.jTextField1.setVisible(false);
                this.jTextField2.setVisible(false);
            }
        }
    }
    void dibujartablero(Graphics g)
    {
        ImageIcon fichablanco = new ImageIcon(new ImageIcon(getClass().getResource("fichagris.png")).getImage());
        ImageIcon ficharoja = new ImageIcon(new ImageIcon(getClass().getResource("ficharoja.png")).getImage());
        ImageIcon fichanegra = new ImageIcon(new ImageIcon(getClass().getResource("fichanegra.png")).getImage());
        int x=150;
        int y=150;
        for(int i=0;i<6;i++)
        {
            for(int j=0;j<7;j++)
            {
                if(tablero[j]==0)
                {
                    g.drawImage(fichablanco.getImage(), x, y, null);
                    x=x+100;
                    if (j==tablero.length-1)
                    {
                        x=150;
                        y=y+80;
                    }
                }
                if(tablero[j]==1)
                {
                    g.drawImage(ficharoja.getImage(), x, y, null);
                    x=x+100;
                    if (j==tablero.length-1)
                    {
                        x=150;
                        y=y+80;
                    }
                }
                if(tablero[j]==2)
                {
                    g.drawImage(fichanegra.getImage(), x, y, null);
                    x=x+100;
                    if (j==tablero.length-1)
                    {
                        x=150;
                        y=y+80;
                    }
                }
            }
        }
    }
    public void unjugador()
    {
        if(turnoj1==turnoj2)
        {
            this.jLabel1.setText("");
            String text;
            int n;
            text=jTextField1.getText();
            n=Integer.parseInt(text);
            n=n-1;
            tablero[5-vector[n]][n]=1;
            vector[n]++;
            turnoj1++;
            comprobarganadorj();
            repaint();
        }
        if(turnoj1-1==turnoj2)
        {
            movimientomaquina();
            turnoj2++;
            comprobarganadorj();
            repaint();
        }
    }
    public void multijugadorj1()
    {
        this.jLabel1.setText("");
        if(turnoj1==turnoj2)
        {
            if(!ganadora())
            {
                String textj1;
                int j1;
                textj1=jTextField1.getText();
                j1=Integer.parseInt(textj1);
                j1=j1-1;
                tablero[5-vector[j1]][j1]=1;
                vector[j1]++;
                jTextField1.setText("");
                turnoj1++;
                comprobarganadormj();
                repaint();
            }
        }
        else
        {
            this.jLabel1.setText("No es tu turno");
        }
    }
    public void multijugadorj2()
    {
        this.jLabel1.setText("");
        if(turnoj1-1==turnoj2)
        {
            String textj2;
            int j2;
            textj2=jTextField2.getText();
            j2=Integer.parseInt(textj2);
            j2=j2-1;
            tablero[5-vector[j2]][j2]=2;
            vector[j2]++;
            jTextField2.setText("");
            turnoj2++;
            comprobarganadormj();
            repaint();
        }
        else
        {
            this.jLabel1.setText("No es tu turno");
        }
    }
    static boolean ganar(int i, int j)
    {
        boolean juggan;
        if(derecha(i,j))
        {
            juggan=true;
        }
        else if(diagonalderecha(i,j))
        {
            juggan=true;
        }
        else if(diagonalizquierda(i,j))
        {
            juggan=true;
        }
        else if(abajo(i,j))
        {
            juggan=true;
        }
        else
        {
            juggan=false;
        }
        return juggan;
    }
    static boolean derecha (int i, int j)
    {
        boolean retorno;
        if(j<3)
        {
        if(tablero[j]==tablero[j+1] && tablero[j]!=0)
        {
            if(tablero[j]==tablero[j+2] && tablero[j]!=0)
            {
                if(tablero[j]==tablero[j+3] && tablero[j]!=0)
                {
                    retorno=true;
                }
                else
                {
                    retorno=false;
                }
            }
            else
            {
                retorno=false;
            } 
        }
        else
        {
            retorno=false;
        }
        }
        else
        {
            retorno=false;
        }
        return retorno;
    }
    static boolean diagonalderecha (int i, int j)
    { boolean retorno;
        if(i<=2 && j<=3)
        {
        if(tablero[j]==tablero[i+1][j+1] && tablero[j]!=0)
        {
            if(tablero[j]==tablero[i+2][j+2])
            {
                if(tablero[j]==tablero[i+3][j+3])
                {
                    retorno=true;
                }
                else
                {
                    retorno=false;
                }
            }
            else
            {
                retorno=false;
            }
        }
        else
        {
            retorno=false;
        }
        }
        else
        {
            retorno=false;
           
        }
        return retorno;
    }   
    static boolean abajo(int i, int j)
    {
        boolean retorno;
        if(i<=2)
        {
        if(tablero[j]==tablero[i+1][j] && tablero[j]!=0)
        {
            if(tablero[j]==tablero[i+2][j] && tablero[j]!=0)
            {
                if(tablero[j]==tablero[i+3][j] && tablero[j]!=0)
                {
                    retorno=true;
                }
                else
                {
                    retorno=false;
                }
            }
            else
            {
                retorno=false;
            } 
        }
        else
        {
            retorno=false;
        }
        }
        else
        {
            retorno=false;
        }
        return retorno;
    }
    static boolean diagonalizquierda (int i, int j)
    {
        boolean retorno;
        if(i<=2 && j>2)
        {
            if(tablero[j]==tablero[i+1][j-1] && tablero[j]!=0)
            {
                if(tablero[j]==tablero[i+2][j-2])
                {
                    if(tablero[j]==tablero[i+3][j-3])
                    {
                        retorno=true;
                    }   
                    else
                    {
                        retorno=false;
                    }
                }
                else
                {
                    retorno=false;
                }
            }
            else
            {
                retorno=false;
            }
        }
        else
        {
            retorno=false;
        }
        return retorno;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));

        jButton1.setText("Un Jugador");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Multijugador");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Salir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Enviar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Enviar");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Enviar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(871, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE))
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(17, 17, 17))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton5)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(60, 60, 60)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(33, 33, 33))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton5)
                .addGap(35, 35, 35)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(268, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        System.exit(0);
    }                                       

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.jButton4.setVisible(false);
        this.jTextField2.setVisible(true);
        this.jButton5.setVisible(true);
        this.jTextField1.setVisible(true);
        this.jButton6.setVisible(true);
        inicializotablero();
        this.jTextField1.setText("");
        this.jTextField2.setText("");
        repaint();
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.jTextField1.setVisible(true);
        this.jButton4.setVisible(true);
        this.jTextField2.setVisible(false);
        this.jButton5.setVisible(false);
        this.jButton6.setVisible(false);
        inicializotablero();
        this.jTextField1.setText("");
        this.jTextField2.setText("");
        repaint();
    }                                       

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
               unjugador(); 
    }                                       

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        multijugadorj2();
    }                                       

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        multijugadorj1();
    }                                       

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(cuatroenraya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(cuatroenraya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(cuatroenraya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(cuatroenraya.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cuatroenraya().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
}


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines