How to create Tic Toc Teo Game with the help of java code

Ajay DhangarAjay Dhangar
4 min read

Table of contents

Tic-Tac-Toe Game in Java In the Tic-Tac-Toe game, you will see the approach of the game implemented. In this game, two players (one is a computer and another you) will be playing and you have one print board on the screen where from 1 to 9 numbers will be displayed or you can say it box number. Now, you have to choose X or O for the specific box number. For example, if you have to select any number then X or O will be shown on the print board, and turn for next will be there. The task is to create a Java program to implement a 3×3 Tic-Tac-Toe game for two players.

Game board:

image.png

Sample Input:

Enter a slot number to place X in 3

Sample Output:

image.png

Sample Input:

Now, O’s turn, Enter a slot number to place O in 5

Sample Output:

image.png

Below is the implementation of the game in Java :

// A simple program to demonstrate
// Tic-Tac-Toe Game.
import java.util.*;

public class GAME {

    static String[] board;
    static String turn;


    // CheckWinner method will
    // decide the combination
    // of three box given below.
    static String checkWinner()
    {
        for (int a = 0; a < 8; a++) {
            String line = null;

            switch (a) {
            case 0:
                line = board[0] + board[1] + board[2];
                break;
            case 1:
                line = board[3] + board[4] + board[5];
                break;
            case 2:
                line = board[6] + board[7] + board[8];
                break;
            case 3:
                line = board[0] + board[3] + board[6];
                break;
            case 4:
                line = board[1] + board[4] + board[7];
                break;
            case 5:
                line = board[2] + board[5] + board[8];
                break;
            case 6:
                line = board[0] + board[4] + board[8];
                break;
            case 7:
                line = board[2] + board[4] + board[6];
                break;
            }
            //For X winner
            if (line.equals("XXX")) {
                return "X";
            }

            // For O winner
            else if (line.equals("OOO")) {
                return "O";
            }
        }

        for (int a = 0; a < 9; a++) {
            if (Arrays.asList(board).contains(
                    String.valueOf(a + 1))) {
                break;
            }
            else if (a == 8) {
                return "draw";
            }
        }

    // To enter the X Or O at the exact place on board.
        System.out.println(
            turn + "'s turn; enter a slot number to place "
            + turn + " in:");
        return null;
    }

    // To print out the board.
    /* |---|---|---|
    | 1 | 2 | 3 |
    |-----------|
    | 4 | 5 | 6 |
    |-----------|
    | 7 | 8 | 9 |
    |---|---|---|*/

    static void printBoard()
    {
        System.out.println("|---|---|---|");
        System.out.println("| " + board[0] + " | "
                        + board[1] + " | " + board[2]
                        + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[3] + " | "
                        + board[4] + " | " + board[5]
                        + " |");
        System.out.println("|-----------|");
        System.out.println("| " + board[6] + " | "
                        + board[7] + " | " + board[8]
                        + " |");
        System.out.println("|---|---|---|");
    }

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        board = new String[9];
        turn = "X";
        String winner = null;

        for (int a = 0; a < 9; a++) {
            board[a] = String.valueOf(a + 1);
        }

        System.out.println("Welcome to 3x3 Tic Tac Toe.");
        printBoard();

        System.out.println(
            "X will play first. Enter a slot number to place X in:");

        while (winner == null) {
            int numInput;

        // Exception handling.
        // num Input will take input from users like from 1 to 9.
        // If it is not in the range from 1 to 9.
        // then it will show you an error "Invalid input."
            try {
                numInput = in.nextInt();
                if (!(numInput > 0 && numInput <= 9)) {
                    System.out.println(
                        "Invalid input; re-enter slot number:");
                    continue;
                }
            }
            catch (InputMismatchException e) {
                System.out.println(
                    "Invalid input; re-enter slot number:");
                continue;
            }

            // This game has two player x and O.
            // Here is the logic to decide the turn.
            if (board[numInput - 1].equals(
                    String.valueOf(numInput))) {
                board[numInput - 1] = turn;

                if (turn.equals("X")) {
                    turn = "O";
                }
                else {
                    turn = "X";
                }

                printBoard();
                winner = checkWinner();
            }
            else {
                System.out.println(
                    "Slot already taken; re-enter slot number:");
            }
        }

        // If no one wins or lose from both player x and O.
        // then here is the logic to print "draw".
        if (winner.equalsIgnoreCase("draw")) {
            System.out.println(
                "It's a draw! Thanks for playing.");
        }

        // For winner -to display Congratulations! message.
        else {
            System.out.println(
                "Congratulations! " + winner
                + "'s have won! Thanks for playing.");
        }
    }
}

image.png image.png image.png

1
Subscribe to my newsletter

Read articles from Ajay Dhangar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ajay Dhangar
Ajay Dhangar

Hi there! I am an aspiring software development engineer who is passionate about developing software that makes a meaningful impact. I strive to use my skills to solve real-world problems through software development. I am always eager to explore and learn emerging technologies such as web development, artificial intelligence, blockchain, and machine learning, and I am always looking to connect with people who share the same interests and goals as me. In addition to my software development pursuits, I am a branch ambassador for the Intellectual Property Rights Club and a member of the Industry Academia Club at my university. I am a problem solver at heart and have a knack for coding in Java, C, C++, and JavaScript. I have designed a project which you can check out here: https://github.com/Ajay-Dhangar. I am also experienced in developing responsive websites using JavaScript, HTML, CSS, Bootstrap, and React. I am well-versed in jQuery, Bootstrap, Tailwind, and Ajax. Furthermore, I have experience working with MySQL, GitHub, Git Bash, Visual Studio Code, Eclipse, and Codesandbox. If you would like to connect with me, I would love to chat. Feel free to message me if you need any help or would like to discuss anything.