Tic Tac Toe Java Code Against Computer With Source Code

Hello coders, Welcome to my new Tic Tac Toe Java Code Against Computer tutorial and in this tutorial, you will learn to make Tic Tac Toe game in Java where game will take place between human and computer. The program’s Source code is given at the end of the tutorial to download.

I am pretty sure all of you must have played Tic Tac Toe game in your childhood. But today lets make this game in Java and play with computer.

But before proceeding to coding part, we must have to know logic behind Tic Tac game so let’s see what Tic Tac Toe game is and how it is played.

An Introduction To Tic Tac Toe Game between Human And Computer

What is Tic Tac Toe game?

  • Tic Tac Toe is a two player game where each player is assigned a marking symbol X or O.
  • In a 3×3 grid, each player have to place their symbol.
  • The player who succeeds in placing three of their marks in a diagonal, horizontal, or vertical row is the winner.

Rules Of Playing Tic Tac Toe Game

The Tic Tac Toe game takes place in following steps.

  • In first step, we have to consist a 3×3 grid.
  • It is a two player game so each player is assigned with a symbol X or O.
  • In first turn, the first player(whosesoever turn) will place his/her marking symbol in any of the 9 cells.
  • In second turn, the second player will place his/her marking symbol in any of available 8 cells.
  • This process will go on until any of the players will make three successive square grids of the same sign either vertically, horizontally or diagonally.
  • If the player having X marking symbol will achieve the above result then he/she will win or if the player having O marking symbol will achieve the above result then he/she will win.
  • If the grid contains no free cell left and none of the above conditions arrived, the Game will end with a Draw.

So guys, now you have a general idea of tic tac toe game. Are you excited to create this awesome game in java? I know you are waiting for coding part so without wasting time let’s get started.

How To Create Tic Tac Toe Game Against Computer In Java?

Tic Tac Toe is a two player game which is played by generally two humans but in this tutorial you will learn to make this game which will played by human and computer.

Let’s see tic tac toe code in java.

Printing The Board

First of all we will print a 3×3 board grid. This board is consists of 2D array. Let’s see how to do that.

What We Did?

  • We have created board using 2d array of character types.
  • We have defined a function printBoard() which will print the board.

Function To Place The Player’s Moves

Here we will create a function that will used to place the movement of players.

Checking The Validation Of Movement

Now we will check the validation of movements of players.

Asking For Human Move

Now we will create a function that will used to ask and place the user’s move.

What We Did?

  • We have asked the user to enter their input.
  • Then checked user’s input is valid or not. If the user input will be valid then it will display the board filled with user’s marking symbol(X) at the place of user input. Otherwise it will display a message that the input from user is not a valid move.

Placing Computer Move

Now we have to place the computer move on the board. so the code is here.

What We Did?

  • Computer will choose random path so we have used random class of java to achieve this task.
  • So to use random class we have imported Random class on top of our program.
  • Computer will choose random number from 1-9 so we have used rand.nextInt(9) + 1. 
  • Then we have checked validation of computer move. If computer move is valid then it place computer marking symbol at the computer move on the board.

Displaying The Winning Result

In this part we will display the winning result of players.

What We Did?

  • We have defined a method hasContastantWon() that will check the winner contestant.
  • And in the isGameFinished() function We have used if statement to check the winning result of players.
  • If the hasContastantWon() method will return X marking symbol then human will win, and if  hasContastantWon() method will return O marking symbol then computer will win otherwise the game will end with a tie.

Use A Loop To Keep The Game Going

Now at last we will write following code inside main() method to repeat all the process until the game is finished.

Complete Code Of Tic Tac Toe Java Code Against Computer

Here is the complete code of 3×3 tic-tac-toe game in java.

So now it’s time to play this Tic Tac Toe game. Here is the Output.

Tic Tac Toe Java Code Against Computer
Tic Tac Toe Java Code Against Computer – fig – 1

 

Tic Tac Toe Java Code Against Computer
Tic Tac Toe Java Code Against Computer – fig – 2

Tic Tac Toe Java Code Against Computer Source Code Download

Guys, If you want to download the source code of the above program, then you can download the source code by clicking on the below download button.

So guys we have successfully completed Tic Tac Toe Java Code Against Computer Tutorial. I hope you have enjoyed this tutorial of making Tic Tac Toe Java 2d array against computer game. But still if you have any query then feel free to ask in comment section. And don’t forget to stay tuned with Tutorials field to learn this type of awesome games and tutorials. HAPPY CODING.

People Are Also Reading…

Leave a Comment