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

Thread: Tic Tac Toe help please

  1. #1
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default Tic Tac Toe help please

    I've been working on a tic tac toe game and I thought I had it figured out, but whenever I click any button, the whole board changes to "X," instead of just the button that was clicked.
    What am I doing wrong? I want to think the solution is pretty simple, but I've been overlooking it. What should I change in my code?
    Java Code:
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridLayout;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class TicTacToe extends JPanel {
    	JButton[][] buttons;
       int count = 0; 
    	public TicTacToe() {
    		setLayout(new GridLayout(3,3));
    		setPreferredSize(new Dimension(500, 500));
    		Font font = new Font("Sans", Font.BOLD, 100);
    		buttons = new JButton[3][3];
    			for (int row = 0; row < buttons.length; row++)
    				for (int col = 0; col < buttons[row].length; col++) {
    					JButton b = new JButton("");
    					b.setFont(font);
    					buttons[row][col] = b;
    					b.addActionListener(new ButtonActionListener());
    					add(b);
                }
    	}
    	 
    	class ButtonActionListener implements ActionListener {
    			
    		public void actionPerformed(ActionEvent event) {
    			count++;
    			for (int row = 0; row < buttons.length; row++)
    				for (int col = 0; col < buttons[row].length; col++) {
    					if (count % 2 != 0 && buttons[row][col].getText() == ""){
    						buttons[row][col].setText("X");
    					} else if(buttons[row][col].getText() == ""){
    						buttons[row][col].setText("O");
    				}
    			}
    		}
    	}
       
    	public static void buildGUI() {
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setContentPane(new TicTacToe());
    		frame.pack();
    		frame.setVisible(true);
    	}
        
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				buildGUI();
    			}
    		});
    	}
    }

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Tic Tac Toe help please

    Well you replace them all with X here. You go throught the whole panel and replace all empty cells with an "X". Explain what you intend to do here, line by line please and why you use the modulo operator.
    You can be sure that count will not change during the whole loop... so all cells are filled with the same value "X" if they are empty.

    Java Code:
    for (int col = 0; col < buttons[row].length; col++)
    {
        if (count % 2 != 0 && buttons[row][col].getText() == "")
    {
        buttons[row][col].setText("
    }
    else if(buttons[row][col].getText() == "")
    {
        buttons[row][col].setText("O");
    }

  3. #3
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default Re: Tic Tac Toe help please

    I use the modulo operator to distinguish each player's turn; when the count is odd, an 'X' should be marked and when the count is even, an 'O' should be marked.
    I want each player to be able to pick a button and if its empty, it should be marked accordingly, I just haven't been able to figure out how to access a particulat button chosen at random by a player.
    I used a loop expecting that the listener would know the difference when one button is pressed and not another one, and would run through all buttons until it identified which one was pressed and thought that the action would only be performed on the pressed button.
    How can I do that?

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Tic Tac Toe help please

    The parameter "event" of the callback method actionPerformed() delivers you what you are looking for. Using getSource() you get the object back that is causing the action. That is your button. You only need to set this one to "X" if it is not already set.

    Be careful though with the count variable - you only may increase it if the user made a VALID hit on the button. Currently you will count up even if the user presses a button that already was set by the other user...

    EDIT:
    And something else to simplify your code:
    Right at the start of the procedure include
    Java Code:
    char cUserTag = (count % 2) != 0 ? 'X' : 'O';
    Like that you get your right character in the variable cUserTag and can implement the rest of the code generically replacing the (empty) button text with cUserTag.
    Last edited by Sierra; 04-30-2012 at 12:36 PM.

  5. #5
    constden is offline Member
    Join Date
    Apr 2009
    Posts
    18
    Rep Power
    0

    Default Re: Tic Tac Toe help please

    It works :).
    Thank you!

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Tic Tac Toe help please

    Quote Originally Posted by constden View Post
    What am I doing wrong?
    Java Code:
    					if (count % 2 != 0 && buttons[row][col].getText() == ""){
    						buttons[row][col].setText("X");
    					} else if(buttons[row][col].getText() == ""){
    						buttons[row][col].setText("O");
    Comparing Strings for equality with the == operator is so wrong: use the equals( ... ) method instead.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Posting Permissions

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