0

I am making this puzzle game which receives a letter from the keyboard with Scanner(System.in) and it changes the puzzle state.

Now, I am making a GUI for the game. I have a JPanel with JLabels which shows the puzzle and another JLabel with 2 JButtons, one it's an Exit Button already implemented and another has to be a Replay Button. This Replay button has to simulate the restart of the whole game. My question is how can I stop the mainloop immediately after I press the Replay Button?

Is there any system call I can use to when I click the button so it generates an interruption on the main loop?

Here is the main logic loop:

    public static void main(String args[]){

    Cliente i=new Cliente();

    int labSize;
    labSize=i.func1();

    boolean b1;
    b1=i.func2();
    boolean b2;
    b2=i.func3();

    int nDarts;
    nDarts=i.func4();

    int nDrags;
    nDrags=i.func5();

    Game g=new Game(labSize,nDarts,nDrags);

    g.boolInit(b1, b2);

    g.initGame();

    i.setTab(g.getLab(), g.getLabSize());

    do{
        g.updateGame();

        i.setTab(g.getLab(), g.getLabSize());
        i.showTab();

        g.moveHeroObj(i);

        g.firingDartsLogic(i);

        g.weaponCaught();
        g.shieldCaught();
        g.dartLogic();

        g.flamesLogic();
        if(!g.didGameEnd()){
            g.dragonKillLogic();
            g.dragonLogic();
        }

        g.updateHeroSymbol();
        g.updateDragonSymbol();

        if(g.finishedGame())
            g.changeEndBool();

    }while(!g.didGameEnd());

    g.updateGame();

    i.setTab(g.getLab(), g.getLabSize());
    i.showTab();

    i.exitFunc();
    return;
}

And here is the GUI:

public GameGUI(int n) {
    labSize=n;

    mainFrame = new JFrame("Maze Game");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
    mainFrame.setVisible(false);
    
    choicesPanel=new JPanel();
    choicesPanel.setLayout(new GridLayout(0, 1));

    labPanel=new JPanel(new GridLayout(labSize,labSize));
    
    replayButton=new JButton("Replay");
    replayButton.setAlignmentY(Component.TOP_ALIGNMENT);
    replayButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    replayButton.addActionListener(new ReplayHandler());
    
    exitButton=new JButton("Exit");
    exitButton.setAlignmentY(Component.TOP_ALIGNMENT);
    exitButton.setAlignmentX(Component.LEFT_ALIGNMENT);
    exitButton.addActionListener(new ExitHandler());
    
    choicesPanel.add(replayButton);
    choicesPanel.add(exitButton);
    
    mainFrame.getContentPane().add(choicesPanel);
    mainFrame.getContentPane().add(labPanel);
}

private class ExitHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String[] holder={"Yes","Cancel"};
        if(JOptionPane.showOptionDialog(null, "Are you sure you want to exit?", "", 0, 0, null, holder, 0)==0){
            System.exit(0);
        }
    }
    
}

private class ReplayHandler implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        String[] holder={"Yes","No"};
        if(JOptionPane.showOptionDialog(null, "Start a new game?", "", 0, 0, null, holder, 0)==0){
        }
    }
    
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Easiest way would be to have a reference of your game in your `ReplayHandler` and a specifid method e.g. `game.restartGame()` – Murat Karagöz Apr 01 '15 at 10:20

1 Answers1

1

My question is how can i stop the mainloop immediately after i press the Replay Button? Is there any system call i can use to when i click the button so it generates an interruption on the main loop?

You don't. You don't use the same type of "game loop" program structure that you use for a linear console program as you would for an event driven GUI, just like you wouldn't use a new Scanner(System.in) with a Swing GUI. Rather you would change the state of your game when buttons are pressed. The only time that you'd use a game loop would be if you needed animation of some sort, which doesn't seem to be the case here.

As a side recommendation, avoid method names like func1(), func2(). When you come back to debug this code 6 months from now, you will be wondering what the heck these do. Instead, give your variables and methods names that make sense, names that make the code self-commenting.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373