1

Sorry from my bad english, not native speaker. I am working on a SimonSays Game on Java in a GUI. Im new to coding. I managed to make the aplication work on console, however its been a mess to make it work graphically. The program compares the LinkedLists from the generated sequence (secuenciaSimon) to the one entered by the user throught buttons (secuenciaUsuarioGUI) however, the problem is that the compare method is called by a click on any button so the LinkedList from the generated Sequence by simon is larger that the one introduced by the user.

Yellow Button Code

private void bAmarilloMousePressed(java.awt.event.MouseEvent evt) {
   secuenciaUsuarioGUI.add(3); //Adds  the selection to the LinkedList yellow=3
   System.out.println("Secuencua Usuario GUI:" + secuenciaUsuarioGUI.toString()); 
   comparaSecuencia();
   generaSecuencia();  //Adds another value to the LinkedList
}

Compare code

public boolean comparaSecuencia(){
    for (int i = 0; i < secuenciaSimon.size(); i++) {      

            //Here the pause should be

            if(secuenciaSimon.get(i) != secuenciaUsuarioGUI.get(i)){

                System.out.println("Not equal");
                 return false;
            }

    } 
    System.out.println("Equal");
    puntuacion += 100; //Score
    secuenciaUsuarioGUI.clear(); //Clears the LinkedList From the user
    return true;


}

TL;DR Need to wait for "n" inputs of a button on a GUI before running more code without freezing the program.

Thanks

Franco
  • 848
  • 1
  • 12
  • 24

1 Answers1

2

Use an int count variable, set it to 0, and increment it with each button press. Only do your action when the count is adequate, i.e., when it == 3.

For example

// this is a class field
private int count = 0;

// in the code where you create your GUI
button.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent evt){
    count++;  // increment count
    // do something with the button pressed, add information to a list
    secuenciaUsuarioGUI.add(/* ?? something ?? */);
    if (count == 3) {  
      // check the sequence
      comparaSecuencia(); // ?? maybe this
      count = 0; // reset
    }
  }
});

A key concept is that you must make your code event-driven. You're not creating a linear console program and so much code that used to be in for loops are no longer in for loops, but rather you will increment counters or change your object's state when an event occurs and then react to that change in state.

Note: if you're listening for the user to press a JButton, don't use a MouseListener but rather an ActionListener.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Sounds great, however i want to be able to compare before reaching the three buttons. For example if the sequence is Red>Blue>Blue and i input Blue>Blue, would the program wait until the last button, or call it false since the first button pressed is diferent from the sequence? – Franco May 05 '13 at 20:17
  • @Fravo: then you must code this logic into your program. The key though is that you must make your code **event-driven**. You're not creating a linear console program and so code that used to be in for loops are no longer in for loops, but rather you will increment counters or change your object's state when an event occurs and then react to that change in state. – Hovercraft Full Of Eels May 05 '13 at 20:19
  • Thank you, i managed to solve it by implementing you code example and adding an extra comparator using userSecuence.size Have a great day. – Franco May 05 '13 at 20:32