Tic Tac Toe in Java: A Step-by-Step Tutorial

Logan Aug 28, 2026

Welcome to our comprehensive guide on creating a Tic Tac Toe game using Java. Tic Tac Toe, a timeless classic, is the perfect project for beginners in Java programming. Not only is it a fun game to play, but it also introduces you to fundamental programming concepts like classes, methods, and loops. So, let's dive into this engaging tutorial, optimized for search engines to help you find just what you're looking for.

Tic Tac Toe JavaScript | Step by Step Tutorial - Letstacle
Tic Tac Toe JavaScript | Step by Step Tutorial - Letstacle

By the end of this tutorial, you'll have a solid understanding of Java basics and a fully functional Tic Tac Toe game that you can play in the console. Excited? Let's get started!

Tic Tac Toe Game Source Code in Java (Complete Guide)
Tic Tac Toe Game Source Code in Java (Complete Guide)

Setting Up the Project

The first step is to set up a new project in your favorite Integrated Development Environment (IDE). If you're using the popular IntelliJ IDEA, simply create a new Java project, choose a name and location for your project, and click 'Finish'.

Tic Tac Toe in Java with Source Code
Tic Tac Toe in Java with Source Code

Once your project is set up, create a new class file named 'TicTacToe'. This file will contain the main game logic.

Defining the Game Board

I Built Tic Tac Toe With JavaScript - Mitchum.Blog
I Built Tic Tac Toe With JavaScript - Mitchum.Blog

Start by defining the game board using a 2D char array. Each position in the array will represent a cell on the board, using 'X' for Player 1, 'O' for Player 2, and ' ' for empty cells.

Next, create a method named 'printBoard' to display the current state of the game board. This will help players visualize their moves and checkmate situations.

Player Moves

Tic-Tac-Toe Program in Java (written by a student)
Tic-Tac-Toe Program in Java (written by a student)

Next, you need to allow players to make their moves. Create a method named 'getPlayerMove' that takes the current player's symbol and the board as parameters.

In this method, prompt the player to enter their move as a row and column number. Validate the input to ensure it's within the board bounds and that the chosen cell is empty. If the move is valid, update the board and return true. If not, display an error message and have the player try again.

Game Logic

Learn Tic Tac Toe Game in Java
Learn Tic Tac Toe Game in Java

Now that your players can make valid moves, it's time to implement the game logic. Create a method named 'checkForWin' that takes the board and current player's symbol as parameters.

This method should check the entire board for winning combinations. A winning combination is three of the same symbol in a row (horizontally, vertically, or diagonally). If a winner is found, return the winner's symbol. If not, return ' ' to indicate no winner yet.

How to Build Tic Tac Toe with JavaScript, HTML and CSS - Tutorial
How to Build Tic Tac Toe with JavaScript, HTML and CSS - Tutorial
Arduino Touch Tic Tac Toe Game
Arduino Touch Tic Tac Toe Game
Learn How to Create an Advanced Tic Tac Toe Game Using HTML CSS and JavaScript
Learn How to Create an Advanced Tic Tac Toe Game Using HTML CSS and JavaScript
5.2K views · 1.7K reactions | UNO Triple Tic-Tac-Toe.
#uno #unochallenge #boardgame #tictactoe #настольнаяигра | Катерина К.А
5.2K views · 1.7K reactions | UNO Triple Tic-Tac-Toe. #uno #unochallenge #boardgame #tictactoe #настольнаяигра | Катерина К.А
Build Tic Tac Toe with AI Using Swift
Build Tic Tac Toe with AI Using Swift
Tic Tac Toe Game in Verilog and LogiSim
Tic Tac Toe Game in Verilog and LogiSim
Tic Tac Toe in JavaScript with Source Code
Tic Tac Toe in JavaScript with Source Code
Tic Tac Toe Challenge
Tic Tac Toe Challenge

Checking for Draw

After checking for a win, you should also check for a draw. If the board is full and there's no winner, the game is a draw. So, create another method named 'checkForDraw' that returns true if the board is full and false otherwise.

To check if the board is full, simply count the number of empty cells on the board and return true if it's zero, and false otherwise.

Game Loop

Finally, you need to put everything together with a game loop. In the main 'main' method, create a Game object (you'll also need to define the Game class with an instance of the TicTacToe class), and then start the game loop.

In each iteration of the loop, print the current state of the board, get the move from the current player, and check the game status. If there's a winner or a draw, print the result and exit the loop. Otherwise, switch to the other player and continue the loop.

And there you have it - a fully functional Tic Tac Toe game in Java! This guide has walked you through the process, from setting up your project to implementing the game logic. So what's next? It's time to practice and expand your skills. Consider adding support for two-player gameplay over the network, or even creating a simple AI to play against. Happy coding!