Results 1 to 6 of 6
Thread: Tic Tac Toe
- 12-07-2011, 04:26 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Tic Tac Toe
Hey guys,
I'm working on a Tic Tac Toe game and I have few questions:
- When game is over, should I make a method that set all the butons to "" or somthing like destroy the frame and create a new one?
- I'm having an action Listener to every button which make my code very long! also after every click I have to check if game is over so I ask the same question 9 times! sounds ineffective.
If I could have a single method that invokes every time I press on 1-9 button, and a variable that will hold the last button clicked that could save me some lines :P
Code to demonstrate:
Java Code:b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b1.setText("X"); flag = false; } else { b1.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b2.setText("X"); flag = false; } else { b2.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b3.setText("X"); flag = false; } else { b3.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b4.setText("X"); flag = false; } else { b4.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b5.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b5.setText("X"); flag = false; } else { b5.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b6.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b6.setText("X"); flag = false; } else { b6.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b7.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b7.setText("X"); flag = false; } else { b7.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b8.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b8.setText("X"); flag = false; } else { b8.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } }); b9.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(flag) { b9.setText("X"); flag = false; } else { b9.setText("O"); flag = true; } if(isOver()) { result = JOptionPane.showConfirmDialog(null, "Try Again?", "Sorry, You Lost!", JOptionPane.YES_OPTION, JOptionPane.CLOSED_OPTION); if(result == JOptionPane.YES_OPTION) initialization(); else System.exit(0); } } });
- 12-07-2011, 04:30 PM #2
Re: Tic Tac Toe
Instead of using anonymous inner classes, write an inner class that implements ActionListener, create an instance of that class and use it in all the addListener calls.have a single method that invokes every time I press on 1-9 button
The Event class's getSource method returns a reference to the component that caused the event.a variable that will hold the last button clicked
Save its value for the last button clicked.Last edited by Norm; 12-07-2011 at 04:53 PM. Reason: implements
- 12-07-2011, 04:46 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Tic Tac Toe
Ok I created an inner class but I think you got confused cause ActionListener is an interface so I have to implement it and not extend it .. anyway I called this class ButtonClicked.
So I created an instance of the ButtonClicked called bc and in each button ActionListener I called the actionPerformed method of bc (bc.actionPerformed(e)) and of course send the ActionEvent parameter.
Now in my actionPerformed method in ButtonClicked class I wrote the following code: e.getSource().setText("X"); and it's not recognize the e.getSource as a button.
Thanks for your help.
- 12-07-2011, 04:55 PM #4
Re: Tic Tac Toe
Read the API doc for the getSource method to see what it returns.
You will have to cast it to the specific type of component you are using.
- 12-07-2011, 05:37 PM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Tic Tac Toe
I read it, it returns an object.
But I need somehow to convert it too a Button object.
I tryed:
But it says that it can't convery from Object to JButton.Java Code:JButton lastClicked = e.getSource(); b.setText("X");
- 12-07-2011, 05:47 PM #6
Re: Tic Tac Toe
Do you know about casting? You use casting when you want to tell the compiler you know what you are doing and you don't want the compiler to give an error message. However you can still get an error message when you try to cast an object to the wrong class when you execute the code.
The syntax for a cast: put the type inside of parans: (<THETYPEHERE>) and put that in front of the variable that you want to cast. int x = (int)1.4;


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks