Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Norm

Thread: Tic Tac Toe

  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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);
    				}
    			}
    		});

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default Re: Tic Tac Toe

    have a single method that invokes every time I press on 1-9 button
    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.
    a variable that will hold the last button clicked
    The Event class's getSource method returns a reference to the component that caused the event.
    Save its value for the last button clicked.
    Last edited by Norm; 12-07-2011 at 04:53 PM. Reason: implements

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Tic Tac Toe

    Quote Originally Posted by Norm View Post
    Instead of using anonymous inner classes, write an inner class that extends ActionListener, create an instance of that class and use it in all the addListener calls.
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default 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.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Tic Tac Toe

    I read it, it returns an object.
    But I need somehow to convert it too a Button object.
    I tryed:

    Java Code:
    JButton lastClicked = e.getSource();
    				b.setText("X");
    But it says that it can't convery from Object to JButton.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default 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;
    tnrh1 likes this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •