0

I'm working on a personal project where I'm trying to create Simon game in Java. It is essentially a memory game where the user has to repeat the sequence the computer generates. So, I created a basic CLI version of the game and now I'm looking to give it a GUI. I haven't worked with Java Swing before so I'm having some trouble replicating the same behavior in GUI form.

The structure of the CLI version looked something like this:

public static void main(String[] args) {
        GameContext simon = new GameContext();
        simon.start();
        
        while (!simon.getRoundEndState()) {
            simon.playSequence();
            simon.pickColour();
        }   
}

Here's what the playSequence() method looks like. It essentially generates a number between 0 and 3 and keeps adding one number to the array each round if the player gets the previous one right.

    public void playSequence() {
        int rand = getRandomNumber(4);
        computerSequence.add(rand);
        for(int i:computerSequence) {
            System.out.println(i);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        gameContext.setState(gameContext.getHumanPlayingState());   
    }

pickColour() method that asks the user for the pattern and checks if the pattern matches the one generated by the computer.

    public void pickColour() {
        boolean roundWon = true;
        for(int i=0; i<gameContext.getSequence().size();i++){
            Scanner input = new Scanner(System.in);
            Integer userInput = input.nextInt();
            if(userInput == gameContext.getSequence().get(i)) {
                continue;
            }
            else {
                System.out.println("Input mismatched the sequence");
                roundWon = false;
                break;
            }
        }
        
        if (roundWon == true) {
            score++;
            gameContext.setState(gameContext.getComputerPlayingState());
        } else {
            gameContext.setRoundEndState(true);
            System.out.println("Your score is: " + score);
            gameContext.setState(gameContext.getInLobbyState());
        }
        
        
    }

Please note that I'm using the state pattern here and hence the change in states. I got the first part working where I need to light up the colors after the computer generates the the sequence. So now, playSequence() looks something like this:

    public void playSequence() {
        int rand = getRandomNumber(4);
        computerSequence.add(rand);
        gameContext.setState(gameContext.getHumanPlayingState());   
    }

And I added a mouse listener to the start button which looks something like this:

        btnStart.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                simon = new GameContext();
                
                simon.start();
                
                while (!simon.getRoundEndState()) {
                    simon.playSequence();
                    for(int i : simon.getSequence()) {
                        lightPanels(i);
                        Timer timer = new Timer(1000, e -> {
                            darkenPanels(i);
                        });
                        timer.setRepeats(false);
                        timer.start();
                    
                    }
                    simon.pickColour();
                }

            }
        });

I have 4 JPanels that now act as the input buttons. How do I change simon.pickColour() so that instead the asking the user for the correct sequence like it did in the CLI version, it would register the clicks made in the JPanels as the input.

  • What you really want to do is to animate the buttons so it looks like they've been pressed? That's a very different thing than clicking on them again. – NomadMaker Oct 25 '20 at 08:00

1 Answers1

0

It essentially generates a number between 0 and 3 and keeps adding one number to the array each round if the player gets the previous one right.

and

I have 4 JPanels that now act as the input buttons.

Why have 4 panels. Just have one panel that contains 4 buttons. You would then add an ActionListener to the button (not a MouseListner) to handle the clicking of the button.

The buttons would be created with code like:

for (int i = 1; i <= 4; i++)
{
    JButton button = new JButton("" + i);
    button.addActionListener(...);
    panel.add( button );
}

The ActionListener code would then get the text of the button and add the text to an ArrayList to track the order the the buttons that have been clicked.

A working example of this approach of sharing an ActionListener for all the buttons can be found here: https://stackoverflow.com/a/33739732/131872. Note the "action command" will default from the text that is set on the button.

camickr
  • 321,443
  • 19
  • 166
  • 288