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

Thread: Tic Tac Toe Issue

  1. #1
    Jakesta42 is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Smile Tic Tac Toe Issue

    Hey all,

    I'm currently in the process of making Tic Tac Toe, and most of it is working. The way I wrote it (Warning: It's written in a god-awful manage) is with
    9 buttons, with the a String called letter alternating between X and O, based on a boolean (switching every ActionPerformed). In the ActionPerformed
    section, there's a huge load of if statements accounting for every winning possibility (I know, not the best way to do it).

    I also have a boolean which makes sure that the button values are not null (So that it only checks the buttons when they have values). There is also
    another boolean called 'Win', initially set to false, but when the winning condition is met, it is said to true. When 'Win' is true, then a dialogue box pops up saying 'lettervalue' + "Wins!".

    The problem is that, although most of the winning combinations work, some of them do not, and the 'letterValue' stays as a blank string, so it just
    comes up as ' Wins!'. After a few hours of debugging, a friend of mine and I managed to figure out that the string was being set before the
    Action Performed, so it's reading the blank button.

    The combinations that still do not work (which are the last two sets of if statements):

    Row1Column1 + Row1Column2 + Row1Column3
    Row1Column3 + Row2Column3 + Row3Column3



    I've tried to fix it, but no luck, so I need some advice...

    XML Code:
    //Credit to: http://forum.codecall.net for inspiration
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
     
    public class TicTacToeMain extends Applet implements ActionListener
    {
     
    	boolean IfNull = true;
    	private JButton Row1Column1 = new JButton("");			//Column 1 
    	private JButton Row1Column2 = new JButton("");	
    	private JButton Row1Column3 = new JButton("");	
     
    	private JButton Row2Column1 = new JButton("");				//Column 2
    	private JButton Row2Column2 = new JButton("");	
    	private JButton Row2Column3 = new JButton("");	
     
    	private JButton Row3Column1 = new JButton("");				//Column 3
    	private JButton Row3Column2 = new JButton("");	
    	private JButton Row3Column3 = new JButton("");	
     
     
    	private JFrame MyWindow = new JFrame("Tic-Tac-Toe");
    	private boolean XTurn;
    	private boolean Win = false;
    	String WinnerString = "";
    	private boolean Tie = false;
     
    	private String letterValue = "";
     
    	public void init()
    	{
    		Color white = new Color(255,255,255);
    		MyWindow.setBackground(white);
    		MyWindow.setLayout(new GridLayout(4,3)); 
    		MyWindow.setSize(500,500);
    		MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		MyWindow.add(Row1Column1);
    		Row1Column1.addActionListener(this);
    		MyWindow.add(Row1Column2);
    		Row1Column2.addActionListener(this);
    		MyWindow.add(Row1Column3);
    		Row1Column3.addActionListener(this);
     
    		MyWindow.add(Row2Column1);
    		Row2Column1.addActionListener(this);
    		MyWindow.add(Row2Column2);
    		Row2Column2.addActionListener(this);
    		MyWindow.add(Row2Column3);
    		Row2Column3.addActionListener(this);
     
    		MyWindow.add(Row3Column1);
    		Row3Column1.addActionListener(this);
    		MyWindow.add(Row3Column2);
    		Row3Column2.addActionListener(this);
    		MyWindow.add(Row3Column3);
    		Row3Column3.addActionListener(this);
     
    		MyWindow.setVisible(true);
    		XTurn = true;
     
     
    	}
     
    	public void actionPerformed(ActionEvent e) 
    	{
     
    		System.out.println(Row2Column3.getText() + ".");
    		if(XTurn == true)
    		{
    			letterValue = "X";
    		}
    		else if(XTurn == false)
    		{
    			letterValue = "O";
    		}
     
    		if(e.getSource() == Row1Column1)
    		{
    			Row1Column1.setText(letterValue);
    			Row1Column1.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row1Column2)
    		{
    			Row1Column2.setText(letterValue);
    			Row1Column2.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row1Column3)
    		{
    			Row1Column3.setText(letterValue);
    			Row1Column3.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row2Column1)
    		{
    			Row2Column1.setText(letterValue);
    			Row2Column1.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row2Column2)
    		{
    			Row2Column2.setText(letterValue);
    			Row2Column2.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row2Column3)
    		{
    			Row2Column3.setText(letterValue);
    			Row2Column3.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row3Column1)
    		{
    			Row3Column1.setText(letterValue);
    			Row3Column1.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row3Column2)
    		{
    			Row3Column2.setText(letterValue);
    			Row3Column2.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
    		else if(e.getSource() == Row3Column3)
    		{
    			Row3Column3.setText(letterValue);
    			Row3Column3.setEnabled(false);
    			if(XTurn == true)
    			{
    				XTurn = false;
    			}
    			else if (XTurn == false)
    			{
    				XTurn = true;
    			}
    		}
     
     
    		//Win Possibilities 
     
     
    		if(Row1Column1.getText() != "" && Row1Column2.getText() != "" && Row1Column3.getText() != "" 
    			|| Row2Column1.getText() != "" && Row2Column2.getText() != "" && Row2Column3.getText() != "" 
    				|| Row3Column1.getText() != "" && Row3Column2.getText() != "" && Row3Column3.getText() != ""
    					|| Row1Column1.getText() != "" && Row2Column2.getText() != "" && Row3Column3.getText() != ""
    						|| Row1Column3.getText() != "" && Row2Column3.getText() != "" && Row3Column3.getText() != ""
    							|| Row1Column2.getText() != "" && Row2Column2.getText() != "" && Row3Column2.getText() != ""
    								|| Row1Column3.getText() != "" && Row2Column2.getText() != "" && Row3Column1.getText() != ""
    									|| Row1Column1.getText() != "" && Row2Column1.getText() != "" && Row3Column1.getText() != ""
    										|| Row1Column3.getText() != "" && Row2Column2.getText() != "" && Row3Column1.getText() != ""
    											|| Row1Column3.getText() != "" && Row2Column3.getText() != "" && Row3Column3.getText() != ""
    												|| Row3Column1.getText() != "" && Row3Column2.getText() != "" && Row3Column3.getText() != "")
    		{
    			IfNull = false;
    		}
     
     
    		CheckForWin();
    		CheckForTrue();
    		CheckForTie();
     
     
    	}
     
     
    	public void CheckForTrue()
    	{
    		if(Win == true)
    		{
    			if(WinnerString == "")
    			{
    				JOptionPane.showMessageDialog(this, WinnerString + " Wins!" );
    			}
    			else
    			{
    				PopUpWin(WinnerString);			
    			}
     
    		}
    	}
    	//}
    	public void CheckForTie()
    	{
    		if(Tie == true)
    		{
    			JOptionPane.showMessageDialog(this, "It's a tie!");
    		}
    		else
    		{
     
    		}
    	}
    	public void CheckForWin()
    	{
    		if(Row1Column1.getText() == Row1Column2.getText()  && Row1Column2.getText() == Row1Column3.getText() && IfNull == false)	
    		{
    			WinnerString = Row1Column1.getText();
    			Win = true;
    		}
    		//
    		else if(Row2Column1.getText() == Row2Column2.getText() && Row2Column2.getText() == Row2Column3.getText() && IfNull == false)
    		{
    			WinnerString = Row2Column1.getText();
    			Win = true;
    		}
    		//
    		else if (Row3Column1.getText() == Row3Column2.getText() && Row3Column2.getText() == Row3Column3.getText() && IfNull == false)
    		{
    			WinnerString = Row3Column1.getText();
    			Win = true;
    		}
    		//
    		else if(Row1Column1.getText() == Row2Column1.getText() && Row2Column1.getText() == Row3Column1.getText() && IfNull == false)
    		{
    			WinnerString = Row1Column1.getText();
    			Win = true;
    		}	
    		//
    		else if(Row1Column1.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column3.getText() && IfNull == false)
    		{
    			WinnerString = Row1Column1.getText();
    			Win = true;
    		}
    		//
    		else if(Row1Column2.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column2.getText() && IfNull == false)
    		{
    			WinnerString = Row1Column2.getText();
    			Win = true;
    		}
    		else if(Row1Column3.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column1.getText() && IfNull == false)
    		{
    			WinnerString = Row1Column3.getText();
    			Win = true;
    		}
    		else if(Row1Column3.getText() == Row2Column3.getText() && Row2Column3.getText() == Row3Column3.getText() && IfNull == false)
    		{
    			WinnerString = Row2Column3.getText();
    			Win = true;
    		}
    		else if (Row3Column1.getText() == Row3Column2.getText() && Row3Column2.getText() == Row3Column3.getText() && IfNull == false)
    		{
    			WinnerString = Row3Column1.getText();
    			Win = true;
    		}
     
    	}
    	public void PopUpWin(String i)
    	{
    		JOptionPane.showMessageDialog(this, i + " Wins!");
    	}
     
    }
    Last edited by Jakesta42; 08-12-2011 at 06:06 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Get rid of those silly increasing indents.
    Code conventions for the Java programming language

    db

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,443
    Rep Power
    25

    Default

    DarrylBurke likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Thanks Norm.

    db

  5. #5
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    I really don't feel like reading through this, but if you wanna take a look at how I did mine and model yours after it feel free to do that.
    Java Code:
    if(b1.getIcon() == X && b2.getIcon() == X && b3.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b1.getIcon() == X && b4.getIcon() == X && b7.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b9.getIcon() == X && b3.getIcon() == X && b6.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b7.getIcon() == X && b8.getIcon() == X && b9.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b4.getIcon() == X && b5.getIcon() == X && b6.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b1.getIcon() == X && b5.getIcon() == X && b9.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b2.getIcon() == X && b5.getIcon() == X && b8.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b5.getIcon() == X && b3.getIcon() == X && b7.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end();
    		}else if(b1.getIcon() == O && b2.getIcon() == O && b3.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b1.getIcon() == O && b4.getIcon() == O && b7.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b9.getIcon() == O && b3.getIcon() == O && b6.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b7.getIcon() == O && b8.getIcon() == O && b9.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b4.getIcon() == O && b5.getIcon() == O && b6.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b1.getIcon() == O && b5.getIcon() == O && b9.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}else if(b2.getIcon() == O && b5.getIcon() == O && b8.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end();
    		}
    Also the end() thing is a method I made that just resets everything.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,443
    Rep Power
    25

    Default

    Looks like a ugly mess of code that should be reduced to a loop with an array.

  7. #7
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Looks like a ugly mess of code that should be reduced to a loop with an array.
    How would I do that?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,443
    Rep Power
    25

    Default

    Define an array for the squares and another 2D array of indexes for the winning rows. (Whoops need 2 arrays)
    Loop thru the array of the winning rows using their values to index into the array of squares. If the squares on a row are not empty and they match, you have a winner.
    I wrote a JS version a while back: http://normsstuff.zxq.net/Games/TicTacToe.html

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

    Default

    I just looked around in your code, he is really inefficient and there is alot of unnecessary code
    Just a small example:
    Java Code:
    if(XTurn == true)
    {
    	letterValue = "X";
    }
    	else if(XTurn == false)
    {
    	letterValue = "O";
    }
    Your else if is unnecessary.
    The meaning of this else means that the XTurn is false, so why do you ask again the same question?
    That what happens when people rush and not learn properly.
    The code should be:
    Java Code:
    if(XTurn)
    	letterValue = "X";
    else
    	letterValue = "O";
    I'm not a pro in java at all, those are very basic things.

    Now I have a question, why the buttons has to be private?
    Last edited by tnrh1; 08-17-2011 at 06:45 AM.

Similar Threads

  1. another tic tac toe issue
    By hoangx87 in forum New To Java
    Replies: 3
    Last Post: 04-18-2011, 10:26 AM
  2. JSP issue
    By RamaLakshmi in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-21-2011, 01:55 PM
  3. nio issue
    By mawandiadeepak in forum Networking
    Replies: 2
    Last Post: 03-17-2010, 05:23 AM
  4. JSF issue
    By premjo in forum New To Java
    Replies: 0
    Last Post: 02-14-2010, 02:19 PM
  5. Issue
    By FlashNinja in forum New To Java
    Replies: 20
    Last Post: 11-28-2009, 09:44 PM

Tags for this Thread

Posting Permissions

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