Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:10:48+00:00 2026-06-09T19:10:48+00:00

I have implemented maybe the simpliest solution to the game tictactoe, I have used

  • 0

I have implemented maybe the simpliest solution to the game tictactoe, I have used a gui to represent it. So I am asking if you could give me some suggestions and ideas how to make it more elegant. The part I am trying to improve is in the class TicTacToe and in the methods mousePressed and mouseEntered. Here is the code.

/**The TicTacToe class with the GUI
 */
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class TicTacToe extends JFrame implements MouseListener
{
    JLabel[] jl ={
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel(),
            new JLabel()} ;

    ImageIcon[] ox = {
            new ImageIcon("tic/o.gif"),
            new ImageIcon("tic/x.gif")  
    };

    static boolean isFirst;
    static int counter = 1;

    public TicTacToe()
    {

        JPanel panel = new JPanel(new GridLayout(3,3));

        Border border = new LineBorder(Color.red, 2);
        Border border2 = new LineBorder(Color.blue, 2);



        for(int i=0; i<jl.length; i++)
        {
            jl[i].setBorder(border2);
            jl[i].addMouseListener(this); 
            panel.add(jl[i] );
        }
        panel.setBorder(border);
        this.add(panel);




    }

    public static String count()
    {
        counter++;

        if(counter % 2 != 0)
        {
            isFirst  = true;
            return "First";
        }
            isFirst = false;
            return "Second";

    }


    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }


    @Override
    public void mousePressed(MouseEvent me)
    {
        int count = 0;
        count();


        for(int i=0; i<jl.length; i++)
        {

            if(jl[0].getIcon() == ox[0]   && jl[1].getIcon() == ox[0] && jl[2].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }

            else if(jl[3].getIcon() == ox[0]   &&  jl[4].getIcon() == ox[0] && jl[5].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }

            else if(jl[6].getIcon() == ox[0]   && jl[7].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won ");
                System.exit(0);
            }


            else if(jl[0].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won with the major diagonal ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[6].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won with the subdiagonal ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[0]   && jl[3].getIcon() == ox[0] && jl[6].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[1].getIcon() == ox[0]   && jl[4].getIcon() == ox[0] && jl[7].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[0]   && jl[5].getIcon() == ox[0] && jl[8].getIcon() == ox[0] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with 0 won  ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[1]   && jl[1].getIcon() == ox[1] && jl[2].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }

            else if(jl[3].getIcon() == ox[1]   &&  jl[4].getIcon() == ox[1] && jl[5].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }

            else if(jl[6].getIcon() == ox[1]   && jl[7].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won ");
                System.exit(0);
            }


            else if(jl[0].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won with the major diagonal ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[6].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won with the subdiagonal ");
                System.exit(0);
            }

            else if(jl[0].getIcon() == ox[1]   && jl[3].getIcon() == ox[1] && jl[6].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }

            else if(jl[1].getIcon() == ox[1]   && jl[4].getIcon() == ox[1] && jl[7].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }

            else if(jl[2].getIcon() == ox[1]   && jl[5].getIcon() == ox[1] && jl[8].getIcon() == ox[1] )
            {
                JOptionPane.showMessageDialog(null,
                        "Player with X won  ");
                System.exit(0);
            }




            if(me.getSource() == jl[i])
            {

                if(jl[i].getIcon() == ox[0] || jl[i].getIcon() == ox[1])
                {
                    JOptionPane.showMessageDialog(null,
                            "You can't insert at " + (i + 1) +  " the place is already taken ");
                    break;
                }


                if(isFirst)
                {
                    jl[i].setIcon(ox[0]);
                }
                else
                {
                    jl[i].setIcon(ox[1]);
                }
            }
        }



    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) 
    {
        if((jl[0].getIcon() == ox[0] || jl[0].getIcon() == ox[1]) && (jl[1].getIcon() == ox[0] || jl[1].getIcon() == ox[1] ) &&
                 (jl[2].getIcon() == ox[0] || jl[2].getIcon() == ox[1]) && (jl[3].getIcon() == ox[0] || jl[3].getIcon() == ox[1] ) &&
                    (jl[4].getIcon() == ox[0] || jl[4].getIcon() == ox[1]) && (jl[5].getIcon() == ox[0] || jl[5].getIcon() == ox[1] ) &&
                    (jl[6].getIcon() == ox[0] || jl[6].getIcon() == ox[1]) && (jl[7].getIcon() == ox[0] || jl[7].getIcon() == ox[1] )
                    && (jl[8].getIcon() == ox[0] || jl[8].getIcon() == ox[1] ))
            {

                    JOptionPane.showMessageDialog(null,
                            "Draw");
                    System.exit(0);

            }

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

/** The tester class with the main method and the frame
 */
import javax.swing.JFrame;
public class TestTicTacToe 
{
    public static void main(String[] args)
    {
//      JFrame application = new JFrame();
        TicTacToe frame = new TicTacToe();
        frame.setLocationByPlatform(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,400);
        frame.setVisible(true);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:10:50+00:00Added an answer on June 9, 2026 at 7:10 pm

    Do not use the icons as states! Use a appropriate (numeric) model for the board and the marks on the fields, like

    int[] board = new int[3][3];
    final static int X_MARK = 1;
    final static int O_MARK = -1;
    

    So if a player clicks on a field,

    1. check if the field is empty (the corresponding array cell has value 0)
    2. change the cell value to 1 or -1
    3. calculate if we have a winner or a draw (simply calculate the sums of rows, columns and diagonals, if a sum is either 3 or -3, then we have a winner, otherwise and if no field is 0, we have a draw)

    Then, use the model to update the board view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a caption solution for shadowbox which uses the 'rev' inline tag.
I think I have implemented some of my code wrong. I cannot figure out
Deal all, I have implemented some functions and like to ask some basic thing
I have implemented entities within the Symfony2 framework that are annotated to be used
I have implemented correctly bump's api, and added this code: - (void) configureBump {
I have implemented pagination to my data, but the problem is I only have
I have implemented a table view with multiple threads. Currently when a user taps
I have implemented a test method with Jersey to run on my Google AppEngine
I have implemented Facebook into my app but now I find that whenever I
I have implemented clean URLs using the following in my .htaccess RewriteEngine on RewriteCond

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.