Ver Mensaje Individual
  #2 (permalink)  
Antiguo 20/08/2012, 15:07
Avatar de Lalounam
Lalounam
 
Fecha de Ingreso: mayo-2012
Ubicación: México D.F.
Mensajes: 59
Antigüedad: 12 años
Puntos: 19
Respuesta: juego simon dice

Mi estimado, tu pregunta me puso la espina de hacer este juego tan llamativo, no me agrada mucho contestar por completo una duda incluyendo todo el código, pero dado que tu ya tenía algo empezado no creo que esté mal, además puedes tomar de mi código lo que te sirva, saludos.

Código Java:
Ver original
  1. package main;
  2.  
  3. import java.awt.Color;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.WindowEvent;
  9. import java.awt.event.WindowListener;
  10. import java.util.ArrayList;
  11. import java.util.Random;
  12.  
  13. import javax.swing.JButton;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JPanel;
  17.  
  18. public class Main {
  19.  
  20.     private final static long serialVersionUID = 437634839L;
  21.  
  22.     private static int difficulty = 1;
  23.     private static int follow = 0;
  24.  
  25.     private static boolean doSimon = false;
  26.     private static boolean exit = false;
  27.     private static boolean doAction = false;
  28.  
  29.     private static JButton b = new JButton("Nuevo");
  30.     private static JButton s = new JButton("Salir");
  31.  
  32.     private static JLabel l = new JLabel("Bienvenido!!!");
  33.  
  34.     private static final Random random = new Random();
  35.     private static final ArrayList<Integer> says = new ArrayList<Integer>();
  36.     private static final ArrayList<JPanel> buts = new ArrayList<JPanel>();
  37.  
  38.     public static void main(String[] args) {
  39.         JFrame board = new JFrame();
  40.         board.setSize(600, 200);
  41.  
  42.         insertButtons(board);
  43.  
  44.         board.setVisible(true);
  45.  
  46.         simon();
  47.         actions();
  48.  
  49.         b.addActionListener(new ActionListener() {
  50.             public void actionPerformed(ActionEvent e) {
  51.                 doSimon = true;
  52.                 doAction = true;
  53.                 b.setEnabled(false);
  54.             }
  55.         });
  56.  
  57.         s.addActionListener(new ActionListener() {
  58.             public void actionPerformed(ActionEvent e) {
  59.                 exit = true;
  60.             }
  61.         });
  62.  
  63.         while (!exit) {
  64.             if (doSimon) {
  65.                 waiting(1000);
  66.                 simonSays();
  67.                 doSimon = false;
  68.             }
  69.         }
  70.  
  71.         board.dispose();
  72.     }
  73.  
  74.     private static void insertButtons(final JFrame board) {
  75.         buts.add(new JPanel());
  76.         buts.add(new JPanel());
  77.         buts.add(new JPanel());
  78.         buts.add(new JPanel());
  79.         buts.add(new JPanel());
  80.         buts.add(new JPanel());
  81.  
  82.         buts.get(0).setBackground(Color.BLUE);
  83.         buts.get(0).setBounds(0, 0, 100, 100);
  84.         buts.get(0).setLayout(null);
  85.  
  86.         buts.get(1).setBackground(Color.GREEN);
  87.         buts.get(1).setBounds(100, 0, 100, 100);
  88.         buts.get(1).setLayout(null);
  89.  
  90.         buts.get(2).setBackground(Color.CYAN);
  91.         buts.get(2).setBounds(200, 0, 100, 100);
  92.         buts.get(2).setLayout(null);
  93.  
  94.         buts.get(3).setBackground(Color.BLACK);
  95.         buts.get(3).setBounds(300, 0, 100, 100);
  96.         buts.get(3).setLayout(null);
  97.  
  98.         buts.get(4).setBackground(Color.ORANGE);
  99.         buts.get(4).setBounds(400, 0, 100, 100);
  100.         buts.get(4).setLayout(null);
  101.  
  102.         buts.get(5).setBackground(Color.RED);
  103.         buts.get(5).setBounds(500, 0, 100, 100);
  104.         buts.get(5).setLayout(null);
  105.  
  106.         board.add(buts.get(0));
  107.         board.add(buts.get(1));
  108.         board.add(buts.get(2));
  109.         board.add(buts.get(3));
  110.         board.add(buts.get(4));
  111.         board.add(buts.get(5));
  112.  
  113.         JPanel bot = new JPanel();
  114.         bot.setBounds(0, 100, 500, 35);
  115.  
  116.         JPanel lab = new JPanel();
  117.         lab.setBounds(0, 135, 500, 35);
  118.  
  119.         bot.add(b);
  120.         bot.add(s);
  121.         lab.add(l);
  122.  
  123.         board.add(lab);
  124.         board.add(bot);
  125.  
  126.         board.setLayout(null);
  127.  
  128.         board.addWindowListener(exitListener());
  129.     }
  130.  
  131.     private static void fine() {
  132.         follow = 0;
  133.         difficulty++;
  134.         simon();
  135.         l.setText("BIEN!");
  136.     }
  137.  
  138.     private static void wrong() {
  139.         follow = 0;
  140.         l.setText("Perdiste... nivel máximo alcanzado:" + difficulty);
  141.         l.setForeground(Color.RED);
  142.         doAction = false;
  143.         difficulty = 1;
  144.         simon();
  145.         b.setEnabled(true);
  146.     }
  147.  
  148.     private static void simon() {
  149.         says.clear();
  150.         for (int i = 0; i < difficulty; i++) {
  151.             says.add(random.nextInt(6));
  152.         }
  153.     }
  154.  
  155.     private static void simonSays() {
  156.         l.setText("Nivel:" + difficulty);
  157.         l.setForeground(Color.GRAY);
  158.         doAction = false;
  159.         for (int i = 0; i < says.size(); i++) {
  160.             doClick(says.get(i));
  161.             waiting(200);
  162.         }
  163.         l.setForeground(Color.BLACK);
  164.         doAction = true;
  165.     }
  166.  
  167.     private static void doClick(final int index) {
  168.         final Color current = buts.get(index).getBackground();
  169.         buts.get(index).setBackground(Color.WHITE);
  170.         waiting(300);
  171.         buts.get(index).setBackground(current);
  172.     }
  173.  
  174.     private static void actions() {
  175.         addMouseListenerToPanel(buts.get(0), 0);
  176.         addMouseListenerToPanel(buts.get(1), 1);
  177.         addMouseListenerToPanel(buts.get(2), 2);
  178.         addMouseListenerToPanel(buts.get(3), 3);
  179.         addMouseListenerToPanel(buts.get(4), 4);
  180.         addMouseListenerToPanel(buts.get(5), 5);
  181.     }
  182.  
  183.     private static void waiting(int time) {
  184.         try {
  185.             Thread.sleep(time);
  186.         } catch (Exception e) {
  187.         }
  188.     }
  189.  
  190.     private static void addMouseListenerToPanel(final JPanel jp, final int index) {
  191.         final MouseListener listener = new MouseListener() {
  192.             final Color color[] = new Color[1];
  193.  
  194.             public void mouseReleased(MouseEvent e) {
  195.                 if (doAction) {
  196.                     ((JPanel) e.getSource()).setBackground(color[0]);
  197.                 }
  198.             }
  199.  
  200.             public void mousePressed(MouseEvent e) {
  201.                 if (doAction) {
  202.                     color[0] = ((JPanel) e.getSource()).getBackground();
  203.                     ((JPanel) e.getSource()).setBackground(Color.WHITE);
  204.                     if (says.get(follow++).intValue() == index) {
  205.                         if (says.size() <= follow) {
  206.                             fine();
  207.                             doSimon = true;
  208.                         }
  209.                     } else {
  210.                         wrong();
  211.                     }
  212.                 }
  213.             }
  214.  
  215.             public void mouseClicked(MouseEvent e) {
  216.             }
  217.  
  218.             public void mouseExited(MouseEvent e) {
  219.             }
  220.  
  221.             public void mouseEntered(MouseEvent e) {
  222.             }
  223.         };
  224.  
  225.         jp.addMouseListener(listener);
  226.     }
  227.  
  228.     private static WindowListener exitListener() {
  229.         return new WindowListener() {
  230.  
  231.             public void windowActivated(WindowEvent e) {
  232.  
  233.             }
  234.  
  235.             public void windowClosed(WindowEvent e) {
  236.                 exit = true;
  237.             }
  238.  
  239.             public void windowClosing(WindowEvent e) {
  240.                 exit = true;
  241.             }
  242.  
  243.             public void windowDeactivated(WindowEvent e) {
  244.  
  245.             }
  246.  
  247.             public void windowDeiconified(WindowEvent e) {
  248.  
  249.             }
  250.  
  251.             public void windowIconified(WindowEvent e) {
  252.  
  253.             }
  254.  
  255.             public void windowOpened(WindowEvent e) {
  256.  
  257.             }
  258.  
  259.         };
  260.     }
  261. }

Última edición por Lalounam; 20/08/2012 a las 16:41