Tic Tac Toe Game using Java Swing in Java

Hi, Today we are learning how to develop a Tic Tac Toe game using Java Swing in Java. This project uses some java swing functionalities to provide GUI.

Java-Swing is a lightweight and cross-platform package in java that is used to provide GUI to Java projects.

Let’s discuss the logic and code to develop this Game

So to build this game we are programing 2 classes.

First, we are discussing the code and logic’s to create TicTacToeGame.java

In this class first, we are creating a java swing application window using JFrame and also creating 2 JPanel’s one for the text to display and another one for the 9 buttons, and also creating a constructor because whenever we create an object of this class a new game window and a new game will be created.

public class TicTacToeGame implements ActionListener {// implements ActionListener Class to this class
    JFrame frame = new JFrame();// Frame object
    JPanel t_panel = new JPanel();// Pannel for text
    JPanel bt_panel = new JPanel();// Pannel for buttons
    JLabel textfield = new JLabel();// Label object to display text
    JButton[] bton = new JButton[9];// creating an array of button's because in tic tac toe game there are 9 position's
    int chance_flag = 0;
    Random random = new Random();
    boolean pl1_chance;
    

    TicTacToeGame() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 800);
        frame.getContentPane().setBackground(new Color(50, 50, 50));
        frame.setTitle("Tic Tac Toe");
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);

        
        textfield.setBackground(new Color(120, 20, 124));
        textfield.setForeground(new Color(25, 255, 0));
        textfield.setFont(new Font("Ink Free", Font.BOLD, 75));
        textfield.setHorizontalAlignment(JLabel.CENTER);
        textfield.setText("Tic Tac Toe");
        textfield.setOpaque(true);

        t_panel.setLayout(new BorderLayout());
        t_panel.setBounds(0, 0, 800, 100);

        bt_panel.setLayout(new GridLayout(3, 3));// setting layout of bt_pannel as gridlayout 
        bt_panel.setBackground(new Color(150, 150, 150));

        for (int i = 0; i < 9; i++) {
            bton[i] = new JButton();// creating object for each button element of array
            bt_panel.add(bton[i]);// adding each button to the pannel for buttons
            bton[i].setFont(new Font("Ink Free", Font.BOLD, 120));
            bton[i].setFocusable(false);
            bton[i].addActionListener(this);
        }
        
        t_panel.add(textfield);
        frame.add(t_panel, BorderLayout.NORTH);
        frame.add(bt_panel);

        startGame();// To start Game
    }

Now we are creating some functions to add some functionalities like start game, check the player move, Game Over, player one win, and player two wins.

public void startGame() {

       try {
           textfield.setText("Loading....");
           Thread.sleep(4000);
       } catch (InterruptedException e) {

           e.printStackTrace();
       }
       int chance=random.nextInt(100);

       if (chance%2 == 0) {
           pl1_chance = true;
           textfield.setText("X turn");
       } else {
           pl1_chance = false;
           textfield.setText("O turn");
       }
   }
   public void gameOver(String s){
       chance_flag = 0;
       Object[] option={"Restart","Exit"};
       int n=JOptionPane.showOptionDialog(frame, "Game Over\n"+s,"Game Over",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,option,option[0]);
       if(n==0){
           frame.dispose();
           new TicTacToeGame();
       }
       else{
           frame.dispose();
       }
   
   }

   public void matchCheck() {// In this function we are checking wining condition's using conditional statments
       if ((bton[0].getText() == "X") && (bton[1].getText() == "X") && (bton[2].getText() == "X")) {
           xWins(0, 1, 2);
       }
       else if ((bton[0].getText() == "X") && (bton[4].getText() == "X") && (bton[8].getText() == "X")) {
           xWins(0, 4, 8);
       }
       else if ((bton[0].getText() == "X") && (bton[3].getText() == "X") && (bton[6].getText() == "X")) {
           xWins(0, 3, 6);
       }
       else if ((bton[1].getText() == "X") && (bton[4].getText() == "X") && (bton[7].getText() == "X")) {
           xWins(1, 4, 7);
       }
       else if ((bton[2].getText() == "X") && (bton[4].getText() == "X") && (bton[6].getText() == "X")) {
           xWins(2, 4, 6);
       }
       else if ((bton[2].getText() == "X") && (bton[5].getText() == "X") && (bton[8].getText() == "X")) {
           xWins(2, 5, 8);
       }
      else if ((bton[3].getText() == "X") && (bton[4].getText() == "X") && (bton[5].getText() == "X")) {
           xWins(3, 4, 5);
       }
      else if ((bton[6].getText() == "X") && (bton[7].getText() == "X") && (bton[8].getText() == "X")) {
           xWins(6, 7, 8);
       }
     
       else if ((bton[0].getText() == "O") && (bton[1].getText() == "O") && (bton[2].getText() == "O")) {
           oWins(0, 1, 2);
       }
       else if ((bton[0].getText() == "O") && (bton[3].getText() == "O") && (bton[6].getText() == "O")) {
           oWins(0, 3, 6);
       }
       else if ((bton[0].getText() == "O") && (bton[4].getText() == "O") && (bton[8].getText() == "O")) {
           oWins(0, 4, 8);
       }
       else if ((bton[1].getText() == "O") && (bton[4].getText() == "O") && (bton[7].getText() == "O")) {
           oWins(1, 4, 7);
       }
       else if ((bton[2].getText() == "O") && (bton[4].getText() == "O") && (bton[6].getText() == "O")) {
           oWins(2, 4, 6);
       }
       else if ((bton[2].getText() == "O") && (bton[5].getText() == "O") && (bton[8].getText() == "O")) {
           oWins(2, 5, 8);
       }
       else if ((bton[3].getText() == "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O")) {
           oWins(3, 4, 5);
       } else if ((bton[6].getText() == "O") && (bton[7].getText() == "O") && (bton[8].getText() == "O")) {
           oWins(6, 7, 8);
       }
       else if(chance_flag==9) {
           textfield.setText("Match Tie");
            gameOver("Match Tie");
       }
   }

   public void xWins(int x1, int x2, int x3) {
       bton[x1].setBackground(Color.RED);
       bton[x2].setBackground(Color.RED);
       bton[x3].setBackground(Color.RED);

       for (int i = 0; i < 9; i++) {
           bton[i].setEnabled(false);
       }
       textfield.setText("X wins");
       gameOver("X Wins");
   }

   public void oWins(int x1, int x2, int x3) {
       bton[x1].setBackground(Color.RED);
       bton[x2].setBackground(Color.RED);
       bton[x3].setBackground(Color.RED);

       for (int i = 0; i < 9; i++) {
           bton[i].setEnabled(false);
       }
       textfield.setText("O Wins");
       gameOver("O Wins");
   }

Now we are creating an action performed function that is trigger when any button is clicked.

public void actionPerformed(ActionEvent e) {

        for (int i = 0; i < 9; i++) {
            if (e.getSource() == bton[i]) {
                if (pl1_chance) {
                    if (bton[i].getText() == "") {
                        bton[i].setForeground(new Color(255, 0, 0));
                        bton[i].setText("X");
                        pl1_chance = false;
                        textfield.setText("O turn");
                        chance_flag++;
                        matchCheck();
                    }
                } else {
                    if (bton[i].getText() == "") {
                        bton[i].setForeground(new Color(0, 0, 255));
                        bton[i].setText("O");
                        pl1_chance = true;
                        textfield.setText("X turn");
                        chance_flag++;
                        matchCheck();
                    }
                }
            }
        }
    }

These are the logics and code snippets to create TicTacToeGame.java class.

Next, we will create a main class to create the object of TicTacToeGame.java class.

new TicTacToeGame();

So these are logic’s and code snippets to develop this game now you just need to copy and paste the given below code and develop your own Tic Tac Toe Game.

TicTacToeGame.Java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class TicTacToeGame implements ActionListener {

  
    JFrame frame = new JFrame();
    JPanel t_panel = new JPanel();
    JPanel bt_panel = new JPanel();
    JLabel textfield = new JLabel();
    JButton[] bton = new JButton[9];
    int chance_flag = 0;
    Random random = new Random();
    boolean pl1_chance;
    

    TicTacToeGame() {

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 800);
        frame.getContentPane().setBackground(new Color(50, 50, 50));
        frame.setTitle("Tic Tac Toe");
        frame.setLayout(new BorderLayout());
        frame.setVisible(true);

        
        textfield.setBackground(new Color(120, 20, 124));
        textfield.setForeground(new Color(25, 255, 0));
        textfield.setFont(new Font("Ink Free", Font.BOLD, 75));
        textfield.setHorizontalAlignment(JLabel.CENTER);
        textfield.setText("Tic Tac Toe");
        textfield.setOpaque(true);

        t_panel.setLayout(new BorderLayout());
        t_panel.setBounds(0, 0, 800, 100);

        bt_panel.setLayout(new GridLayout(3, 3));
        bt_panel.setBackground(new Color(150, 150, 150));

        for (int i = 0; i < 9; i++) {
            bton[i] = new JButton();
            bt_panel.add(bton[i]);
            bton[i].setFont(new Font("Ink Free", Font.BOLD, 120));
            bton[i].setFocusable(false);
            bton[i].addActionListener(this);
        }
        
        t_panel.add(textfield);
        frame.add(t_panel, BorderLayout.NORTH);
        frame.add(bt_panel);

        startGame();
    }

    public void startGame() {

        try {
            textfield.setText("Loading....");
            Thread.sleep(4000);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
        int chance=random.nextInt(100);

        if (chance%2 == 0) {
            pl1_chance = true;
            textfield.setText("X turn");
        } else {
            pl1_chance = false;
            textfield.setText("O turn");
        }
    }
    public void gameOver(String s){
        chance_flag = 0;
        Object[] option={"Restart","Exit"};
        int n=JOptionPane.showOptionDialog(frame, "Game Over\n"+s,"Game Over",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,option,option[0]);
        if(n==0){
            frame.dispose();
            new TicTacToeGame();
        }
        else{
            frame.dispose();
        }
    
    }

    public void matchCheck() {
        if ((bton[0].getText() == "X") && (bton[1].getText() == "X") && (bton[2].getText() == "X")) {
            xWins(0, 1, 2);
        }
        else if ((bton[0].getText() == "X") && (bton[4].getText() == "X") && (bton[8].getText() == "X")) {
            xWins(0, 4, 8);
        }
        else if ((bton[0].getText() == "X") && (bton[3].getText() == "X") && (bton[6].getText() == "X")) {
            xWins(0, 3, 6);
        }
        else if ((bton[1].getText() == "X") && (bton[4].getText() == "X") && (bton[7].getText() == "X")) {
            xWins(1, 4, 7);
        }
        else if ((bton[2].getText() == "X") && (bton[4].getText() == "X") && (bton[6].getText() == "X")) {
            xWins(2, 4, 6);
        }
        else if ((bton[2].getText() == "X") && (bton[5].getText() == "X") && (bton[8].getText() == "X")) {
            xWins(2, 5, 8);
        }
       else if ((bton[3].getText() == "X") && (bton[4].getText() == "X") && (bton[5].getText() == "X")) {
            xWins(3, 4, 5);
        }
       else if ((bton[6].getText() == "X") && (bton[7].getText() == "X") && (bton[8].getText() == "X")) {
            xWins(6, 7, 8);
        }
      
        else if ((bton[0].getText() == "O") && (bton[1].getText() == "O") && (bton[2].getText() == "O")) {
            oWins(0, 1, 2);
        }
        else if ((bton[0].getText() == "O") && (bton[3].getText() == "O") && (bton[6].getText() == "O")) {
            oWins(0, 3, 6);
        }
        else if ((bton[0].getText() == "O") && (bton[4].getText() == "O") && (bton[8].getText() == "O")) {
            oWins(0, 4, 8);
        }
        else if ((bton[1].getText() == "O") && (bton[4].getText() == "O") && (bton[7].getText() == "O")) {
            oWins(1, 4, 7);
        }
        else if ((bton[2].getText() == "O") && (bton[4].getText() == "O") && (bton[6].getText() == "O")) {
            oWins(2, 4, 6);
        }
        else if ((bton[2].getText() == "O") && (bton[5].getText() == "O") && (bton[8].getText() == "O")) {
            oWins(2, 5, 8);
        }
        else if ((bton[3].getText() == "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O")) {
            oWins(3, 4, 5);
        } else if ((bton[6].getText() == "O") && (bton[7].getText() == "O") && (bton[8].getText() == "O")) {
            oWins(6, 7, 8);
        }
        else if(chance_flag==9) {
            textfield.setText("Match Tie");
             gameOver("Match Tie");
        }
    }

    public void xWins(int x1, int x2, int x3) {
        bton[x1].setBackground(Color.RED);
        bton[x2].setBackground(Color.RED);
        bton[x3].setBackground(Color.RED);

        for (int i = 0; i < 9; i++) {
            bton[i].setEnabled(false);
        }
        textfield.setText("X wins");
        gameOver("X Wins");
    }

    public void oWins(int x1, int x2, int x3) {
        bton[x1].setBackground(Color.RED);
        bton[x2].setBackground(Color.RED);
        bton[x3].setBackground(Color.RED);

        for (int i = 0; i < 9; i++) {
            bton[i].setEnabled(false);
        }
        textfield.setText("O Wins");
        gameOver("O Wins");
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {

        for (int i = 0; i < 9; i++) {
            if (e.getSource() == bton[i]) {
                if (pl1_chance) {
                    if (bton[i].getText() == "") {
                        bton[i].setForeground(new Color(255, 0, 0));
                        bton[i].setText("X");
                        pl1_chance = false;
                        textfield.setText("O turn");
                        chance_flag++;
                        matchCheck();
                    }
                } else {
                    if (bton[i].getText() == "") {
                        bton[i].setForeground(new Color(0, 0, 255));
                        bton[i].setText("O");
                        pl1_chance = true;
                        textfield.setText("X turn");
                        chance_flag++;
                        matchCheck();
                    }
                }
            }
        }
    }

}

 

Main_Class.Java

public class Main_Class {
    public static void main(String[] args) throws Exception {
       new TicTacToeGame();
    }
}

Output

In the image below, you can see the output result of our code.

 

Thank you, for reading this blog.

Hope this was useful.

Leave a Reply

Your email address will not be published. Required fields are marked *