UnicMinds

TicTacToe Program in Java

Programming Tic-Tac-Toe game in Java

We all know the classic Tic-Tac-Toe game we play by drawing on paper or as a board game. In this post, we will see how we can write a program to print a Tic-Tac-Toe game on screen and for two players to play this game alternatively. Each player has to choose X or O in the respective slot of the 3×3 matrix of the game by choosing the column and row of the matrix where they want to place the X or O.

Rules that will be followed in this Tic-Tac-Toe game are as follows:

  • The game board is stored in a 3×3 array of rows and columns number 0,1, and 2.
  • The first player will play as ‘X’ and the second player will play as ‘O’
  • Players will enter their moves by entering row and column data between 0 and 2
  • When an invalid move is entered, they will be informed that the move (entered value) is invalid – entering out of the scope or entering in the same place where there is already a value entered.
  • When the game ends, the program will show the player that won or tie.

Displayed Game Board is shown below:

Input Values by players:

X enter the column for your move (0-2):

2

X enter the row for your move(0-2):

2

As seen below you have to enter the value ‘X’ in the third row and third column.

Below is the step by step explanation of the code.

Step 1: Create the class tictactoe and the data members 

The class tictactoe has a 2D char array which represents the game board ( 3 rows and 3 columns) along with the variables for capturing the row position and column position of a particular . The last data member represents the current player in the game.

Constructor function initialises the values as shown in the code snippet.

Step 2: Creating the method to display the board.

The following code displays the board with each element in the array is separated by “|”

Step 3: Validating the input and entering in the board

The following method in the tictactoe class validates any move from a given player.

If the player’s row position is less than 0 or greater than the length of the board, it’s a invalid move as the row value starts from 0 and ends at 2.

If the player’s column position is less than 0 or greater than the length of the board, it’s a invalid move as the row value starts from 0 and ends at 2.

If the player’s move is at a place where there is an existing ‘O’ or ‘X’, then it’s a invalid move.

Step 4: Checking if there is a winner 

The following code checks if there is any winner. It checks all the possibilities of having winner such as :  ‘X’ in the first row , second row and third row or in the first column , second column and third column

‘O’ in the first row , second row and third row or in the first column , second column and third column

‘X’ in the all the positions in the 2D game board array diagonally 

‘O’ in the all the positions in the 2D game board array diagonally 

Step 5: Check if game board is full or not

This method checks if any of the item in the 2D array has space in it. If there is any space as one of items in the array, then it returns false ( representing the board is not full), otherwise it returns True 

Step 6: Creating the main class 

Step 7: Checking for winner or draw

The while loop checks if there are 9 valid moves , Unless the 9 valid moves are made, the following are checked 

If any move is valid or not, with every valid move , it is checked whether there is a winner or the gameboard is full.

If there is any winner before 9 valid moves are made, the loop breaks.Otherwise, the loop runs till the game is full.

Here is the link for the complete Java program on Replit. 

Conclusion

In this blog, we learned how to implement the fundamental tic-tac-toe game in Java, written by one of our students. In our Java course for kids, we cover all the basic programming constructs and help children understand and develop their computational thinking using advanced object oriented programming structures too. The basic computational thinking and programming skills can change the way kids interact with the technologies they use daily and in turn change their world-view for the better. Join our extensive range of coding courses from Scratch to Ethical Hacking and Computing Olympiad Preparation.

If you want to learn Java with us, take a free Java trial class for kids.

Tic Tac Toe program in Java
BOOK A FREE TRIAL