Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Tictactoe problem

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [Solved] Tictactoe problem

    Right now I have what is basically just a board on which X's and O's can be placed, except my O's ae not being displayed =/. If you could take a look at it, that'd be great.
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class tictactoe implements ActionListener{
     
        int x = 0; //counter for x turn
        int o = 0; //counter for o turn
        int t = 0; //counter keeping track of what turn it is
        int boardArray[][] = new int[3][3]; //the board
     
        JFrame frame = new JFrame("Tick-tack-toe");
     
        JPanel panel = new JPanel();
     
        ImageIcon whitesquare = new ImageIcon("/home/dean/Java images/whitesquare.gif");
        ImageIcon bluesquare = new ImageIcon("/home/dean/Java images/lightbluesquare.gif");
        ImageIcon redx = new ImageIcon("/home/dean/Java images/redx.gif");
        ImageIcon bluecircle = new ImageIcon("/home/dean/Java images/bluecircle.gif");
     
        JButton b00 = new JButton(whitesquare);
        JButton b01 = new JButton(whitesquare);
        JButton b02 = new JButton(whitesquare);
        JButton b10 = new JButton(whitesquare);
        JButton b11 = new JButton(whitesquare);
        JButton b12 = new JButton(whitesquare);
        JButton b20 = new JButton(whitesquare);
        JButton b21 = new JButton(whitesquare);
        JButton b22 = new JButton(whitesquare);
     
        GridBagConstraints b00c = new GridBagConstraints();
        GridBagConstraints b01c = new GridBagConstraints();
        GridBagConstraints b02c = new GridBagConstraints();
        GridBagConstraints b10c = new GridBagConstraints();
        GridBagConstraints b11c = new GridBagConstraints();
        GridBagConstraints b12c = new GridBagConstraints();
        GridBagConstraints b20c = new GridBagConstraints();
        GridBagConstraints b21c = new GridBagConstraints();
        GridBagConstraints b22c = new GridBagConstraints();
     
        public void constraints()
        {
            b00c.gridx = 0;
            b00c.gridy = 0;
            b00c.weightx = 1;
            b00c.weighty = 1;
     
            b01c.gridx = 1;
            b01c.gridy = 0;
            b01c.weightx = 1;
            b01c.weighty = 1;
     
            b02c.gridx = 2;
            b02c.gridy = 0;
            b02c.weightx = 1;
            b02c.weighty = 1;
     
            b10c.gridx = 0;
            b10c.gridy = 1;
            b10c.weightx = 1;
            b10c.weighty = 1;
     
            b11c.gridx = 1;
            b11c.gridy = 1;
            b11c.weightx = 1;
            b11c.weighty = 1;
     
            b12c.gridx = 2;
            b12c.gridy = 1;
            b12c.weightx = 1;
            b12c.weighty = 1;
     
            b20c.gridx = 0;
            b20c.gridy = 2;
            b20c.weightx = 1;
            b20c.weighty = 1;
     
            b21c.gridx = 1;
            b21c.gridy = 2;
            b21c.weightx = 1;
            b21c.weighty = 1;
     
            b22c.gridx = 2;
            b22c.gridy = 2;
            b22c.weightx = 1;
            b22c.weighty = 1;
     
     
            makeFrame();
        }
     
        public void makeFrame()
        {
            panel.setLayout(new GridBagLayout());
     
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
            panel.add(b00, b00c);
            panel.add(b01, b01c);
            panel.add(b02, b02c);
            panel.add(b10, b10c);
            panel.add(b11, b11c);
            panel.add(b12, b12c);
            panel.add(b20, b20c);
            panel.add(b21, b21c);
            panel.add(b22, b22c);
     
            frame.add(panel);
     
            xturn();
        }
     
        public void xturn()
        {
            t++;
            x = 1;
            listenForClick();
        }
     
        public void oturn()
        {
            t++;
            o = 1;
            listenForClick();
        }
     
        public void listenForClick()
        {
            b00.addActionListener(this);
            b01.addActionListener(this);
            b02.addActionListener(this);
            b10.addActionListener(this);
            b11.addActionListener(this);
            b12.addActionListener(this);
            b20.addActionListener(this);
            b21.addActionListener(this);
            b22.addActionListener(this);
        }
     
        public void check()
        {
            if(t < 5)
            {
                if(x == 1){x = 0; oturn();return;}
                else if(o == 1){o = 0; xturn();return;}
            }
            else if(t >= 5)
            {
                //this is where rows of 3 will be checked for
                if(x == 1){x = 0; oturn(); return;}
                else if(o == 1){o = 0; xturn(); return;}
            }
        }
     
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource() == b00)
            {
                if(x == 1){boardArray[0][0] = 1; b00.setIcon(redx); check();return;} 
                else if(o == 1){boardArray[0][0] = 0; b00.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b01)
            {
                if(x == 1){boardArray[1][0] = 1; b01.setIcon(redx); check();return;}
                else if(o == 1){boardArray[1][0] = 0; b01.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b02)
            {
                if(x == 1){boardArray[2][0] = 1; b02.setIcon(redx); check();return;}
                else if(o == 1){boardArray[2][0] = 0; b02.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b10)
            {
                if(x == 1){boardArray[0][1] = 1; b10.setIcon(redx); check();return;}
                else if(o == 1){boardArray[0][1] = 0; b10.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b11)
            {
                if(x == 1){boardArray[1][1] = 1; b11.setIcon(redx); check();return;}
                else if(o == 1){boardArray[1][1] = 0; b11.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b12)
            {
                if(x == 1){boardArray[2][1] = 1; b12.setIcon(redx); check();return;}
                else if(o == 1){boardArray[2][1] = 0; b12.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b20)
            {
                if(x == 1){boardArray[0][2] = 1; b20.setIcon(redx); check();return;}
                else if(o == 1){boardArray[0][2] = 0; b20.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b21)
            {
                if(x == 1){boardArray[1][2] = 1; b21.setIcon(redx); check();return;}
                else if(o == 1){boardArray[1][2] = 0; b21.setIcon(bluecircle); check();return;}
            }
     
            if(e.getSource() == b22)
            {
                if(x == 1){boardArray[2][2] = 1; b22.setIcon(redx); check();return;}
                else if(o == 1){boardArray[2][2] = 0; b22.setIcon(bluecircle); check();return;}
            }
        }
     
        public static void main(String[] args)
        {
            tictactoe qw = new tictactoe();
            qw.constraints();
        }
     
    }
    Last edited by Nostromo; March 28th, 2011 at 04:38 PM.


Similar Threads

  1. i neeeed help in tictactoe in java !!
    By yinky in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 29th, 2009, 11:17 PM