Foro de elhacker.net

Programación => Java => Mensaje iniciado por: estherlui en 27 Mayo 2012, 13:15 pm



Título: Ayuda algoritmo en java
Publicado por: estherlui en 27 Mayo 2012, 13:15 pm
Hola buenas tardes a todos, necesito ayuda con este algoritmo en java, es el juego de la vida, tengo hecho esto, pero no se por donde seguir o que hacer mas, llevo más de una semana dándole vueltas y ya se me ha bloqueado hasta la mente, si alguien me pudiera ayudar le estaría muy agradecida.
Lo que tengo echo es lo siguiente:
------------------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;


public class matriz extends JFrame {

private JButton[][] boton;
private int n;

/** Creates a new instance of Main */
   public matriz(int n) {
      this.n = n;
      boton = new JButton[n][n];
      this.setLayout(new GridLayout(n,n));
      for (int i = 0; i < n; i++) {
         for (int j = 0; j < n; j++) {
            boton[j] = new JButton();
            String nombre = new Integer(i).toString();
            nombre += new Integer(j).toString();
            boton[j].setActionCommand(nombre);
            this.add(boton[j]);
         }
      }
      this.addListeners();
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
      this.setSize(new Dimension(700,700));
   }
   public void addListeners() {
      for (int i = 0; i < n; i++) {
         for (int j = 0; j < n; j++) {
            boton[j].addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent evt) {
                  JButton evento = (JButton)evt.getSource();
                  System.out.println("apretado el boton "+evento.getActionCommand());
               }
            });
         }
      }
   }

   public static void main(String[] args) {
// TODO code application logic here

      matriz m = new matriz(7);
      m.setVisible(true);
      
   for (i = 0; i < n; i++)
   {
     for (j= 0; j < n; j++)
          {
       m = 0;
    if (boton[j] == celula < 2)
                 {
   boton.setBackground(Color.gray);
         
       }
       else if (botton[j] == celula > 3)
                 {
         boton.setBackground(Color.red);
       }
       else if (boton[j] == (celula == 2 || celula == 3))
                 {
         boton.setBackground(Color.red);
       }
       else if (boton[j] == celula == 3)
                 {
         boton.setBackground(Color.gray);
       }
          }
   }
}
   }


Título: Re: Ayuda algoritmo en java
Publicado por: Proteus1989 en 27 Mayo 2012, 23:05 pm
Aquí tienes el código completo, busca lo que necesites.

http://casidiablo.net/juego-de-la-vida-java/