Question

I have some working code for word guessing game using Swing. Currently, after the game finishes, I need to rerun the main app, for starting another game. I would like to give an option for

New Game
 YES NO

I have implemented using model/controller pattern. I am pasting here my mainapp and hangmanPane. but it has other class files which I am not including.

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                HangManModel model = new DefaultHangManModel("HangManGame");
                HangManPane hangManPane = new HangManPane(model);

                JFrame frame = new JFrame("Hang Man");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(hangManPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}

public class HangManPane extends JPanel {

    private KeyPadPane keyPadPane;
    private GuessPane guessPane;
    private HangManModel model;
    private HangManModelHandler hangManModelHandler;

    public HangManPane(HangManModel model) {

        setLayout(new BorderLayout());

        add(getGuessPane(), BorderLayout.CENTER);
        add(getKeyPadPane(), BorderLayout.SOUTH);

        setModel(model);

    }

    public HangManPane() {

        this(null);

    }

    public void setModel(HangManModel value) {

        getKeyPadPane().setModel(value);
        getGuessPane().setModel(value);

        if (model != null) {

            model.removeHangManModelListener(getHangManModelListener());

        }

        HangManModel old = model;
        model = value;
        firePropertyChange("model", old, model);

        if (model != null) {

            model.addHangManModelListener(getHangManModelListener());

        }

    }

    protected HangManModelListener getHangManModelListener() {

        if (hangManModelHandler == null) {

            hangManModelHandler = new HangManModelHandler();

        }

        return hangManModelHandler;

    }

    protected KeyPadPane getKeyPadPane() {

        if (keyPadPane == null) {

            keyPadPane = new KeyPadPane();

        }

        return keyPadPane;

    }

    protected GuessPane getGuessPane() {

        if (guessPane == null) {

            guessPane = new GuessPane();

        }

        return guessPane;

    }

    protected class HangManModelHandler implements HangManModelListener {

        @Override
        public void newGame(HangManModelEvent evt) {
        }

        @Override
        public void wonGame(HangManModelEvent evt) {

            JOptionPane.showMessageDialog(HangManPane.this, "You Won!", "Winner", JOptionPane.INFORMATION_MESSAGE);

        }

        @Override
        public void lostGame(HangManModelEvent evt) {

            JOptionPane.showMessageDialog(HangManPane.this, "You Lose!", "Winner", JOptionPane.INFORMATION_MESSAGE);

        }

        @Override
        public void stateChanged(HangManModelEvent evt) {
        }

    }

}

P.S: StackOverflow User MadProgrammer actually helped in implementing most of this code.

Was it helpful?

Solution

In the HangManPane class, you could to provide a playAgain method, for example...

protected void playAgain(String msg) {

    int result = JOptionPane.showConfirmDialog(this, 
            "<html>" + msg + "<br>Do you want to play again?", 
            "Play Agaion", 
            JOptionPane.YES_NO_OPTION, 
            JOptionPane.QUESTION_MESSAGE);

    switch (result) {
        case JOptionPane.YES_OPTION:
            setModel(new DefaultHangManModel("Your new secret word"));
            break;
        default:
            SwingUtilities.getWindowAncestor(this).dispose();
            break;
    }

}

In the HangManListener implementation, you would simply then call this method, for example...

    @Override
    public void wonGame(HangManModelEvent evt) {

        playAgain("You won");

    }

    @Override
    public void lostGame(HangManModelEvent evt) {

        playAgain("You lost");

    }

OTHER TIPS

Currently, after the game finishes, I need to rerun the main app, for starting another game. I would like to give an option for

New Game YES NO

Then code it.

  • use a JOptionPane that asks this question.
  • Give your classes reset() methods that can then be called if the user wants to run another game. You should be the one to create this code though.

StackOverflow User MadProgrammer actually implemented most of this code.

Not a good idea. Borrow ideas yes, but not code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top