4

This tictactoe program is a 2player game. The GUI i made are frame and buttons after that i started coding. Actually my program is working in this kind of coding.

private String letter= " ";
private int count= 0;


    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        count++;
                if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){

                letter = "X";

                } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){

                letter = "O";

                }

        if(evt.getSource() == btn1){

                        btn1.setText(letter);

                } else if(evt.getSource() == btn2){

                        btn2.setText(letter);

                } else if(evt.getSource() == btn3){

                        btn3.setText(letter);

                } else if(evt.getSource() == btn4){

                        btn4.setText(letter);

                } else if(evt.getSource() == btn5){

                        btn5.setText(letter);

                } else if(evt.getSource() == btn6){

                        btn6.setText(letter);

                } else if(evt.getSource() == btn7){

                        btn7.setText(letter);

                } else if(evt.getSource() == btn8){

                        btn8.setText(letter);

                } else if(evt.getSource() == btn9){

                        btn9.setText(letter);




    }         
    }                                    

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    count++;
                if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){

                letter = "X";

                } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){

                letter = "O";

                }

        if(evt.getSource() == btn1){

                        btn1.setText(letter);

                } else if(evt.getSource() == btn2){

                        btn2.setText(letter);

                } else if(evt.getSource() == btn3){

                        btn3.setText(letter);

                } else if(evt.getSource() == btn4){

                        btn4.setText(letter);

                } else if(evt.getSource() == btn5){

                        btn5.setText(letter);

                } else if(evt.getSource() == btn6){

                        btn6.setText(letter);

                } else if(evt.getSource() == btn7){

                        btn7.setText(letter);

                } else if(evt.getSource() == btn8){

                        btn8.setText(letter);

                } else if(evt.getSource() == btn9){

                        btn9.setText(letter);

Yeah, it is actually working but my problem with this codes is I need to put the codes in every button e.g. button7, button1 (these just the example of my buttons) which is the codes are the action performed of the button.

I want only a single command that each button perform and I dont want to copy paste in every buttons because they are identical codes. I think there is a code to do that, what do you think? Please help!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jayseer
  • 189
  • 2
  • 7
  • 15
  • 1
    Create single `ActionListener` and then register the same to all the buttons. Then identify button using `event.getSource()` and do the rest stuff accordingly. – Ved Oct 31 '12 at 05:54
  • Hi! in my generated codes I found this line btn1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn1ActionPerformed(evt); I think java already automatically generated the codes. What do you think is wrong? – Jayseer Oct 31 '12 at 06:07

2 Answers2

3

I really don't know how you trying to solve this game logic, though your code can be compressed to this form :

int count = -1;
String letter = "";  
JButton[] button = new JButton[9];  // These three being your Instance Variables
for (int i = 0; i < 9; i++, counter++)
{
    final int counter = i;
    button[i] = new JButton("");
    button[i].addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            count++;
            if (count % 2 != 0)
                letter = "X";
            else
                letter = "O";
            button[counter].setText(letter); 
        }
    });
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

So basically, you want to collapse you logic into a single handler. This handler needs a single reference to the button that it is acting on...

Something like...

public class ActionHandler implements ActionListener {

    private JButton master;

    public ActionHandler(JButton master) {
        this.master = master;
    }

    protected void setText(String text) {
        master.setText(text);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        String text = null;

        count++;
        if (count / 2f == Math.round(count / 2f)) {
            text = "O";
        } else {
            text = "X";
        }

        setText(text);

    }

}

Now you setup code may be different then this, this is just an example...

buttons = new JButton[9]; // You can use this to reset the board ;)
setLayout(new GridLayout(3, 3));
for (int index = 0; index < 9; index++) {
    JButton btn = new JButton(Integer.toString(index));
    buttons[index] = btn;
    btn.addActionListener(new ActionHandler(btn));
    add(btn);
}

The basic idea is that as you create a new button, you assign it's own ActionHandler passing it a reference of the button. This allows the action handler to control the button based on the state of the game.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366