Blahhh... Java v.GUI programming

Discussion in 'OT Technology' started by Macbeth, Dec 13, 2009.

  1. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Basically for my final assignment I am supposed to create a reset button for a tic tac toe board. I thought it would be easy, but I am retarded and I am having a lot of difficulties. It also is supposed to have the capability to check who won, highlight the buttons of the winning area (so if three x's are in a row, it highlights those buttons), which is why the code looks so cluttered.

    Rather than actually "resetting" the board, it simply creates a new one instead. What the fuck am I supposed to do? :hs:

    Code:
    /**************************************************************
     * TicTacToe.java
     * Dean & Dean
     *
     * This program implements the game of tic-tac-toe.
     * When the first blank button is clicked, its label changes
     * to an X. Subsequent clicked blank buttons change their labels
     * to O and X in alternating sequence.
     **************************************************************/
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    public class TicTacToe extends JFrame
    {
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        JButton button5 = new JButton();
        JButton button6 = new JButton();
        JButton button7 = new JButton();
        JButton button8 = new JButton();
        JButton button9 = new JButton();
    
        private boolean xTurn = true; // keeps track of whether
        // it's X's turn or O's turn
        //***********************************************************
        public TicTacToe()
        {
            setTitle("Tic-Tac-Toe");
            setSize(200, 220);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            createContents();
            setVisible(true);
        } // end TicTacToe constructor
        //***********************************************************
        // Create components and add to window.
        public void resetContents() {
            button1.repaint();
            button2.repaint();
            button3.repaint();
            button4.repaint();
            button5.repaint();
            button6.repaint();
            button7.repaint();
            button8.repaint();
            button9.repaint();
        }
    
        public void createContents()
        {
            Listener listener = new Listener();
            Listener2 listener2 = new Listener2();
    
            button1.addActionListener(listener);
            button2.addActionListener(listener);
            button3.addActionListener(listener);
            button4.addActionListener(listener);
            button5.addActionListener(listener);
            button6.addActionListener(listener);
            button7.addActionListener(listener);
            button8.addActionListener(listener);
            button9.addActionListener(listener);
    
    
            JPanel TicPanel;
            JPanel xPanel;
            JPanel ContPanel;
            JButton resetButton;
    
    
            // Adding tic tac toe board
            setLayout(new BorderLayout());
            TicPanel = new JPanel(new GridLayout(3,3));
            TicPanel.add(button1);
            TicPanel.add(button2);
            TicPanel.add(button3);
            TicPanel.add(button4);
            TicPanel.add(button5);
            TicPanel.add(button6);
            TicPanel.add(button7);
            TicPanel.add(button8);
            TicPanel.add(button9);
    
            add(TicPanel, BorderLayout.CENTER);
    
            // Creating control panel
            ContPanel = new JPanel(new FlowLayout());
            resetButton = new JButton("reset");
            ContPanel.add(resetButton);        
            add(ContPanel, BorderLayout.SOUTH);
            resetButton.addActionListener(listener2);
        } // end createContents
        //***********************************************************
        // If user clicks a button, change its label to "X" or "O".
        private class Listener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton btn = (JButton) e.getSource();
                if (btn.getText().isEmpty())
                {
                    btn.setText(xTurn ? "X" : "O");
                    xTurn = !xTurn;
                }
    
            } // end actionPerformed
        } // end class Listener
        //***********************************************************
        // If user clicks a button, change its label to "X" or "O".
        private class Listener2 implements ActionListener
        {
            public void actionPerformed(ActionEvent e) {
                TicTacToe resetButton = new TicTacToe();
                resetButton.resetContents();
                
            }    
        } // end class Listener
        //***********************************************************
        public static void main(String[] args)
        {
            new TicTacToe();
        }
    } // end class TicTacToe
     
  2. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    Update: Don't understand why it is working now, but now that I put the setText in the listener, it is working fine. Now to work on everything else...

    Code:
    /**************************************************************
     * TicTacToe.java
     * Dean & Dean
     *
     * This program implements the game of tic-tac-toe.
     * When the first blank button is clicked, its label changes
     * to an X. Subsequent clicked blank buttons change their labels
     * to O and X in alternating sequence.
     **************************************************************/
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    public class TicTacToe extends JFrame
    {
        JButton button1 = new JButton();
        JButton button2 = new JButton();
        JButton button3 = new JButton();
        JButton button4 = new JButton();
        JButton button5 = new JButton();
        JButton button6 = new JButton();
        JButton button7 = new JButton();
        JButton button8 = new JButton();
        JButton button9 = new JButton();
    
        private boolean xTurn = true; // keeps track of whether
        // it's X's turn or O's turn
        //***********************************************************
        public TicTacToe()
        {
            setTitle("Tic-Tac-Toe");
            setSize(200, 220);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            createContents();
            setVisible(true);
        } // end TicTacToe constructor
        //***********************************************************
        // Create components and add to window.
        public void resetContents() {
            button1.setText("");
        }
    
        public void createContents()
        {
            Listener listener = new Listener();
            Listener2 listener2 = new Listener2();
            setLayout(new BorderLayout());
            
    
            button1.addActionListener(listener);
            button2.addActionListener(listener);
            button3.addActionListener(listener);
            button4.addActionListener(listener);
            button5.addActionListener(listener);
            button6.addActionListener(listener);
            button7.addActionListener(listener);
            button8.addActionListener(listener);
            button9.addActionListener(listener);
    
    
            JPanel TicPanel;
            JPanel xPanel;
            JPanel ContPanel;
            JButton resetButton;
            
            
            // Creating control panel
            ContPanel = new JPanel(new FlowLayout());
            resetButton = new JButton("reset");
            resetButton.addActionListener(listener2);
            ContPanel.add(resetButton);        
            add(ContPanel, BorderLayout.SOUTH);
    
    
            // Adding tic tac toe board
            TicPanel = new JPanel(new GridLayout(3,3));
            TicPanel.add(button1);
            TicPanel.add(button2);
            TicPanel.add(button3);
            TicPanel.add(button4);
            TicPanel.add(button5);
            TicPanel.add(button6);
            TicPanel.add(button7);
            TicPanel.add(button8);
            TicPanel.add(button9);
            add(TicPanel, BorderLayout.CENTER);
        } // end createContents
        //***********************************************************
        // If user clicks a button, change its label to "X" or "O".
        private class Listener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton btn = (JButton) e.getSource();
                if (btn.getText().isEmpty())
                {
                    btn.setText(xTurn ? "X" : "O");
                    xTurn = !xTurn;
                }
    
            } // end actionPerformed
        } // end class Listener
        //***********************************************************
        private class Listener2 implements ActionListener
        {
            public void actionPerformed(ActionEvent e) {
                button1.setText("");
                button2.setText("");
                button3.setText("");
                button4.setText("");
                button5.setText("");
                button6.setText("");
                button7.setText("");
                button8.setText("");
                button9.setText("");
            }    
        } // end class Listener
        //***********************************************************
        public static void main(String[] args)
        {
            new TicTacToe();
        }
    } // end class TicTacToe
     
  3. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Can you use multiple classes?

    Board class and a gui class.

    board class keeps track of the grid and where mark is at
    gui class should draw the current state of each grid. Should return what type of spot it is, X_SPOT, O_SPOT, BLANK_SPOT.

    The grid class when probe of what type of spot it is should look to its nieghbors to see if there are any like grid spots to alert if theres a winner.
     
  4. Macbeth

    Macbeth That's illogical... OT Supporter

    Joined:
    Mar 1, 2005
    Messages:
    18,408
    Likes Received:
    0
    Location:
    Under your bed.
    We can use multiple classes, yes. I finished the program last night (I didn't add the capability of highlighting the winning sequence though [when a game is won, it is supposed to highlight the winning Xs or Os]). :dunno:

    Code:
    /**************************************************************
     * TicTacToe.java
     * 
     *
     * This program implements the game of tic-tac-toe.
     * When the first blank button is clicked, its label changes
     * to an X. Subsequent clicked blank buttons change their labels
     * to O and X in alternating sequence.
     * 
     * This program also implements a reset button, counters for x
     * and o wins, and it also has radiobuttons to determine the starting
     * player (assuming it is a fresh game).
     * 
     * By XXXXX XXXXX
     **************************************************************/
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    public class TicTacToe extends JFrame
    {
        // Initialization of 9 separate buttons as instance
        // variables for ease of use in various methods.
        JButton b1 = new JButton();
        JButton b2 = new JButton();
        JButton b3 = new JButton();
        JButton b4 = new JButton();
        JButton b5 = new JButton();
        JButton b6 = new JButton();
        JButton b7 = new JButton();
        JButton b8 = new JButton();
        JButton b9 = new JButton();
        
        // Text fields that keep score
        JTextField text = new JTextField("X0");
        JTextField text2 = new JTextField("O0");
        
        // Radio buttons declared as instance variables for ease of use
        JRadioButton startingPlayer = new JRadioButton("X", true);
        JRadioButton otherPlayer = new JRadioButton("O");
        ButtonGroup startPlayer = new ButtonGroup();
    
        
        int counter = 0; // Counter used for radiobuttons
        int xWinCounter = 1; // x Win counter
        int oWinCounter = 1; // y Win counter
    
        private boolean xTurn = true; // keeps track of whether
        // it's X's turn or O's turn
        
        
        public TicTacToe()
        {
            setTitle("Tic-Tac-Toe");
            setSize(250, 270);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            createContents();
            setVisible(true);
        } // end TicTacToe constructor
    
        // Reset window used once a game is won
        public void resetContents() {
            b1.setText("");
            b2.setText("");
            b3.setText("");
            b4.setText("");
            b5.setText("");
            b6.setText("");
            b7.setText("");
            b8.setText("");
            b9.setText("");
            xTurn  = true; // Makes x the starting player
            counter = 0; // Resets radiobutton counter
        }
        // Create components and add to window.
        public void createContents()
        {
            // Listeners and layouts
            Listener listener = new Listener();
            Listener2 listener2 = new Listener2();
            Listener3 listener3 = new Listener3();
            setLayout(new BorderLayout());
    
            // Implement listeners to tic tac toe board buttons
            b1.addActionListener(listener);
            b2.addActionListener(listener);
            b3.addActionListener(listener);
            b4.addActionListener(listener);
            b5.addActionListener(listener);
            b6.addActionListener(listener);
            b7.addActionListener(listener);
            b8.addActionListener(listener);
            b9.addActionListener(listener);
    
            // Declare various panels
            JPanel TicPanel;
            JPanel ContPanel;
            JButton resetButton;
    
            // Adds listeners to radiobuttons
            startPlayer.add(startingPlayer);
            startPlayer.add(otherPlayer);
            startingPlayer.addActionListener(listener3);
            otherPlayer.addActionListener(listener3);
    
    
            // Creating control panel
            ContPanel = new JPanel(new FlowLayout());
            resetButton = new JButton("reset");
            resetButton.addActionListener(listener2);
            ContPanel.add(text2);
            ContPanel.add(otherPlayer);
            ContPanel.add(resetButton);    
            ContPanel.add(startingPlayer);
            ContPanel.add(text);
            add(ContPanel, BorderLayout.SOUTH);
    
    
            // Adding tic tac toe board
            TicPanel = new JPanel(new GridLayout(3,3));
            TicPanel.add(b1);
            TicPanel.add(b2);
            TicPanel.add(b3);
            TicPanel.add(b4);
            TicPanel.add(b5);
            TicPanel.add(b6);
            TicPanel.add(b7);
            TicPanel.add(b8);
            TicPanel.add(b9);
            add(TicPanel, BorderLayout.CENTER);
        } // end createContents
    
        // Check victory method called in action listener 1
        // This method is called each turn to check for a victory.
        public void checkVictory() {
            // Check X victory
            if (b1.getText() == "X" && b2.getText() == "X" && b3.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b4.getText() == "X" && b5.getText() == "X" && b6.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b7.getText() == "X" && b8.getText() == "X" && b9.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b1.getText() == "X" && b4.getText() == "X" && b7.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b2.getText() == "X" && b5.getText() == "X" && b8.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b3.getText() == "X" && b6.getText() == "X" && b9.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b1.getText() == "X" && b5.getText() == "X" && b9.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b3.getText() == "X" && b5.getText() == "X" && b7.getText() == "X") {
                text.setText("X" + xWinCounter);
                xWinCounter ++;
                resetContents();
                counter = 0;
            }
    
    
            // Check if O won
            if (b1.getText() == "O" && b2.getText() == "O" && b3.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b4.getText() == "O" && b5.getText() == "O" && b6.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b7.getText() == "O" && b8.getText() == "O" && b9.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b1.getText() == "O" && b4.getText() == "O" && b7.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b2.getText() == "O" && b5.getText() == "O" && b8.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b3.getText() == "O" && b6.getText() == "O" && b9.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b1.getText() == "O" && b5.getText() == "O" && b9.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
            if (b3.getText() == "O" && b5.getText() == "O" && b7.getText() == "O") {
                text2.setText("O" + oWinCounter);
                oWinCounter ++;
                resetContents();
                counter = 0;
            }
        }
    
        
        // Listeners
        // If user clicks a button, change its label to "X" or "O".
        private class Listener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                JButton btn = (JButton) e.getSource();
                if (btn.getText().isEmpty())
                {
                    btn.setText(xTurn ? "X" : "O");
                    xTurn = !xTurn;
                    checkVictory(); // Checks victory each time a button is clicked
                    counter ++; // Adjusts counter for radiobuttons
                }
    
            } // end actionPerformed
        } // end class Listener
        //***********************************************************
        // Reset button listener
        private class Listener2 implements ActionListener
        {
            public void actionPerformed(ActionEvent e) {
                b1.setText("");
                b2.setText("");
                b3.setText("");
                b4.setText("");
                b5.setText("");
                b6.setText("");
                b7.setText("");
                b8.setText("");
                b9.setText("");
                xTurn  = true;
                counter = 0;
                startingPlayer.setEnabled(true); // Re - enables radiobuttons
                otherPlayer.setEnabled(true); // Re - enables radiobuttons
            }    
        } // end class Listener
        //***********************************************************
        private class Listener3 implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                if (counter > 0) {
                    startingPlayer.setEnabled(false);
                    otherPlayer.setEnabled(false);
                }
                else {
                    if (startingPlayer.isSelected())
                        xTurn = true;
                    else if (otherPlayer.isSelected())
                        xTurn = false;    
                }
            } // end actionPerformed
        } // end class Listener
        public static void main(String[] args)
        {
            new TicTacToe();
        }
    } // end class TicTacToe
     
  5. CodeX

    CodeX Guest

    lol graphics in java, I remember using swing, what a piece of shit.
     

Share This Page