Tic Tac Toe Game Source Code in Java: In this article, we are going to use very simple programming concepts like – Array, Method, and Loops for creating the Tic Tac Toe game using Java Programming Language.
So, if you are familiar with these concepts, then you can follow this article to enhance your knowledge.
Before jumping into the coding part, let’s take a look at the game which we are going to create.
Table of Contents
What is Tic Tac Toe Game
In your childhood, you might have played the Tic Tac Toe game on paper in classroom or with your cousins at a family gathering and enjoyed it a lot.
However, if you haven’t played it then let me give you a brief introduction to Tic Tac Toe.
It is a game of 2 players which is played on a 3 by 3 square grid where players are denoted by X and O.
So, you have to choose one symbol between X and O, which will be your identity, and make a straight line using your symbol(Vertically, Horizontally, Diagonally).
Now that you got an idea about this game. Let’s take a look at the actual code for creating this game.
Tic Tac Toe Game Source Code in Java
Here is a Tic Tac Toe Game Source Code in Java Programming Language:
import java.util.Arrays;
import java.util.Scanner;
public class TicTacToe {
static String[] gameBoard;
static String winner;
static String player;
public static String whoIsWinner(){
for (int i=0; i<8; i++){
String check = null;
switch (i){
case 0: check = gameBoard[0] + gameBoard[1] + gameBoard[2];
break;
case 1: check = gameBoard[3] + gameBoard[4] + gameBoard[5];
break;
case 2: check = gameBoard[6] + gameBoard[7] + gameBoard[8];
break;
case 3: check = gameBoard[0] + gameBoard[3] + gameBoard[6];
break;
case 4: check = gameBoard[1] + gameBoard[4] + gameBoard[7];
break;
case 5: check = gameBoard[2] + gameBoard[5] + gameBoard[8];
break;
case 6: check = gameBoard[0] + gameBoard[4] + gameBoard[8];
break;
case 7: check = gameBoard[2] + gameBoard[4] + gameBoard[6];
break;
}
if(check.equals("XXX"))
return "X";
else if (check.equals("OOO"))
return "O";
}
for (int i=0; i<9; i++){
if(Arrays.asList(gameBoard).contains(String.valueOf(i+1)))
break;
else if(i == 8)
return "Tie";
}
System.out.println("Its " + player + " Chance:");
return null;
}
public static void showBoard(){
System.out.println("#############");
System.out.println("| " + gameBoard[0] + " | " + gameBoard[1] + " | " + gameBoard[2] + " |");
System.out.println("| " + gameBoard[3] + " | " + gameBoard[4] + " | " + gameBoard[5] + " |");
System.out.println("| " + gameBoard[6] + " | " + gameBoard[7] + " | " + gameBoard[8] + " |");
System.out.println("#############");
}
public static void main(String[] args) {
winner = null;
player = "X";
gameBoard = new String[9];
for(int i=0; i<9; i++){
gameBoard[i] = String.valueOf(i+1);
}
showBoard();
System.out.println("Its X Chance: ");
Scanner sc = new Scanner(System.in);
while(winner == null){
int input;
input = sc.nextInt();
if(input > 0 && input <= 9){
if(gameBoard[input-1].equals(String.valueOf(input))){
gameBoard[input-1] = player;
showBoard();
if(player == "X")
player = "O";
else
player = "X";
winner = whoIsWinner();
} else {
System.out.println("Enter value into another slot as it is already taken");
}
}
}
if (winner == "Tie")
System.out.println("Its a Tie!");
else{
System.out.println("Congratulations!" + winner + " Wins");
}
}
}
Output:
Here we can see, a 3 by 3 matrix having values 1 to 9 which denotes the position in the grid.
So, the player has to enter the value between 1 to 9 which will specify the position in which they want to insert their symbol (X or O).
If you understand the above code, Well Done.
However, if you are struggling to understand the working of a program, Don’t Worry. We will help you in understanding it better and make it look simpler.
Understanding the Source Code
In the above program, firstly we have created 3 global static variables so that we can access these variables from anywhere in the class.
Then we created 2 methods, named showBoard() and whoIsWinner().
As the name suggests, showBoard() is used to print the current position of our 3 by 3 square grid, and whoIsWinner() is used for checking the current status of our game and it returns (X, O, or Tie) value based on the winner of the game.
These 2 methods are used inside our main method from where the execution of the program starts.
Inside the main() method, we are assigning the value from 1 to 9 using for loop to our gameBoard[] array which is used to represent our Tic Tac Toe Board.
Followed by that we used the showBoard() method to print the 3 by 3 square grid.
After that, we are going to run a while loop until the value of the winner becomes X, O, or Tie.
Inside the while loop, we are taking input from the user between 1 – 9, which will insert the player symbol into the specified block of the Tic Tac Toe Board.
After that, we are using whoIsWinner() method to return the value(X, O, Tie, or null) and store it in the winner variable.
This while loop will run till the whoIsWinner() method returns null.
However, if the value returned by whoIsWinner() method is not null, then the condition of the while loop gets false and the program gets out of the loop.
At last, the winner of the game gets printed on the screen using the println() method.
Hope you like the article on this simple but famous Game, which we used to play with our friends. If you are familiar with HTML, then you must take look at our article on HTML Games with Source Code.