Welcome to Dream.In.Code
Become a Java Expert!

Join 150,049 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,703 people online right now. Registration is fast and FREE... Join Now!




TicTacToeHelp Continued

 
Reply to this topicStart new topic

TicTacToeHelp Continued, GUI

mooch901
12 Jun, 2008 - 07:12 PM
Post #1

New D.I.C Head
*

Joined: 11 Jun, 2008
Posts: 16

The only problem now pbl is that whenver I click on a Jbutton it says X, or O wins automatically. I dont need three in a row, it automatically says whoever's turn goes first wins the game when i start the game.

This post has been edited by mooch901: 12 Jun, 2008 - 07:14 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: TicTacToeHelp Continued
12 Jun, 2008 - 07:16 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(mooch901 @ 12 Jun, 2008 - 08:12 PM) *

The only problem now pbl is that whenver I click on a Jbutton it says X, or O wins automatically. I dont need three in a row, it automatically says whoever's turn goes first wins the game when i start the game.

re-posted your new code... as I said it will be a lot shorter biggrin.gif

I may have have forgotten something..... if you post the code I'll cut & paste it and test it
Wow doing QA on myself..... I swear I won't be leniant
User is online!Profile CardPM
+Quote Post

mooch901
RE: TicTacToeHelp Continued
12 Jun, 2008 - 07:59 PM
Post #3

New D.I.C Head
*

Joined: 11 Jun, 2008
Posts: 16

sorry for the wait.
CODE
//The "TicTacToe" class.
import java.awt.*;
//import hsa.Console;
import javax.swing.*;
import java.awt.event.*;
/*
*/
public class TicTacToe extends JFrame implements ActionListener  // inputting an action listener for the buttons
{
    private String letter = "";
    private int count = 0;
    private boolean win = false;
    private JButton[] [] button = new JButton [3] [3];
    JPanel TicPane;
    Container contentPane;

    public void actionPerformed (ActionEvent e)
    {

        String event;
        event = e.getActionCommand ();
        if (event.equals ("PRESS TO PLAY TIC TAC TOE"))

            {
                TicTacToee ();
                return;
            }

        if (event.equals ("About"))
        {
            about ();
            return;
        }

        if (event.equals ("Restart"))  //this else if statements runs when the button named "Restart" is clicked on
        {
            //stop (choice);  // this line stops the music from playing
            hide ();  // this line willhide the output window
            TicTacToe window = new TicTacToe (); // creating a windown called GasStation
            window.setTitle ("Welcome to the TicTacToe Game");  // Giving the window a title
            window.setSize (900, 700);  // giving the size of the window
            window.setVisible (true);  // asking if this true
        }

        // so it is a button
        count++;
        if (count % 2 == 1)
            letter = "X";
        else
            letter = "O";

        JButton b = (JButton) e.getSource ();
        b.setText (letter);
        b.setFont (new Font ("Times New Activate", Font.BOLD, 60));
        win = true;
        b.setEnabled (false);


        win = false;
        //horizontal wins
        for (int i = 0; i < 3; i++)
        {
            if (button [i] [0].getText ().equals (button [i] [1].getText ()) && button [i] [0].equals (button [i] [2]))
                win = true;
        }
        //virticle wins
        for (int i = 0; i < 3; i++)
        {
            if (button [0] [i].getText ().equals (button [1] [i].getText ()) && button [0] [i].equals (button [2] [i]))
                win = true;
        }
        //diagonal wins
        if (button [0] [0].getText ().equals (button [1] [1].getText ()) && button [2] [0].getText ().equals (button [1] [1].getText ()))
           ;
        win = true;
        if (button [2] [0].getText ().equals (button [1] [1].getText ()) && button [0] [2].getText ().equals (button [1] [1].getText ()))
           ;
        win = true;

        if (win==true)
        {
            JOptionPane.showMessageDialog (null, letter + " WINS!");
        }
        else if (count == 10 && win == false)
        {
            JOptionPane.showMessageDialog (null, "Tie Game!");
        }



    }





    public void TicTacToee ()
    {

        Frame frame = new Frame ("Button Frame");
        frame.setLayout (null);
        int k = 1;
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                button [i] [j] = new JButton ("" + k);
                k++;
                button [i] [j].addActionListener (this);
                button [i] [j].setBackground (Color.white);
                button [i] [j].setSize (150, 150);
                button [i] [j].setLocation (i * 150 + 5, j * 150 + 30);
                frame.add (button [i] [j]);
            }
        }
        frame.setSize (460, 485);
        frame.setVisible (true);
        frame.addWindowListener (new WindowAdapter ()
        {
            public void windowClosing (WindowEvent e)
            {
                System.exit (0);
            }
        }


        );




    }


    public void about ()
    {
        JLabel text, text1;
        JButton restart;
        if (TicPane != null)  // this if statement runs if there is something on the JPanel
        {
            contentPane.remove (TicPane);  // if there it is it makes the JPanel blank
        }

        TicPane = new JPanel ();  // getting a new JPanel
        TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 300, 30));  // creating a layout, to organize the output screen
        text = new JLabel (" This is a game designed by Rishi Patel which allows you to have a multi-player game of TicTacToe");
        text1 = new JLabel ("Created June of 2008");
        validate ();  // this method sets everything up in the out put screen
        contentPane = getContentPane ();  // getting a new content pane
        if (TicPane != null)  // this if statement runs if there is something on the JPanel
        {
            contentPane.remove (TicPane);  // if there it is it makes the JPanel blank
        }


        restart = new JButton ("Restart");
         restart.addActionListener (this);
         TicPane.setLayout (null);
         restart.setSize (500, 500);
         restart.setLocation (5, 30);
         TicPane = new JPanel ();          // getting a new JPanel
         TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 600, 30));
         TicPane.add (text);
         TicPane.add (text1);
         TicPane.add (restart);
         contentPane.add (TicPane);          // adding the JPanel to the content pane
         validate ();
        
    }

    
    
         public TicTacToe ()
        
    {       JMenuBar menuBar;
         JMenu menu;
         JMenuItem menuItem;
         menuBar = new JMenuBar ();
         setJMenuBar (menuBar);
                 //Create file menu
         menu = new JMenu ("File");
         menuBar.add (menu);
         menuItem = new JMenuItem ("Restart");
         menuItem.addActionListener (this);
         menu.add (menuItem);
         menuItem = new JMenuItem ("Quit");
         menuItem.addActionListener (this);
         menu.add (menuItem);
                 //Create file menu
         menu = new JMenu ("Help");
         menuBar.add (menu);
                 //File Menu Items
         menuItem = new JMenuItem ("About");
         menuItem.addActionListener (this);
         menu.add (menuItem);
         if (TicPane != null)          // this if statement runs if there is something on the JPanel
        
              {
                     contentPane.remove (TicPane);
                    
                }
            
            
             TicPane = new JPanel ();          // getting a new JPanel
         TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 20, 15));          // creating a layout, to organize the output screen
         contentPane = getContentPane ();          // getting the content pane
         JButton activate;          // Initializing all the buttons in this program
         TicPane = new JPanel ();          // making a new JPanel
         TicPane.setLayout (new FlowLayout (FlowLayout.CENTER, 20, 10));
         activate = new JButton ("PRESS TO PLAY TIC TAC TOE");
         activate.addActionListener (this);
         TicPane.add (activate);
                 contentPane.add (TicPane);          // adding the JPanel to the content pane
         validate ();
        
    }


    
    
     public static void main (String[] args)
    
    {
         TicTacToe window = new TicTacToe ();
         window.setTitle ("Welcome to the TicTacToe Game");          // Giving the window a title
         window.setSize (900, 700);          // giving the size of the window
         window.setVisible (true);
        
        
            
    }


    }

User is offlineProfile CardPM
+Quote Post

pbl
RE: TicTacToeHelp Continued
12 Jun, 2008 - 09:41 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
CODE

if (button [0] [0].getText ().equals (button [1] [1].getText ()) && button [2] [0].getText ().equals (button [1] [1].getText ()))
          ;       // <------------------ nthis one
        win = true;
        if (button [2] [0].getText ().equals (button [1] [1].getText ()) && button [0] [2].getText ().equals (button [1] [1].getText ()))
          ;       // <------------------------ this one
        win = true;


That will turned on win = true every times....
remove the ";"
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month