Trybotics Logo

Write Your Own Tic Tac Toe Game in Java

DESCRIPTION

I am sure all of you know about the classic game of Tic Tic Toe. Since my elementary school years, Tic Tac Toe was a popular game I used to play with my friends. I have always been fascinated with the simplicity of the game. In my freshman year, my Java class required me to create a game, an interactive one. Tic Tac Toe was the first one that came to my mind. It took me a few weeks to develop this program and I wanted to share this with all of you. Hope you all get to create this program and share with me your journey.

Happy Tic Tac Toeing :)

Here are simple steps to write your own tic tac toe program in java.

Description:

Here is the setup:

Tic tac toe board represented by a two dimensional character array. There are 3 rows and 3 columns, numbered 0 through 2 each way.

There are 2 players. The user and computer. The player has one action. Pick the next best cell on the board.

There is a gaming module which controls the next steps and the decision making.

Here is how the Tic Tac Toe program works:

1. Initialize board

2. Repeat steps below in a loop:

a. Get move from user (Player must enter move in array index format [row][column])

b. Check if the move is valid

c. Mark the chosen move the board

d. Check game status. Break from the loop if the game has ended.

e. Get move from Computer

f. Check game status. Break from the loop if the game has ended.

3. Declare game result (User wins/Computer Wins/ Draw)

Attached image shows these high level steps. Please see attached java project for the full code.

Description:

Download and Install the IDE (Interactive Development Environment). Eclipse is the IDE used in this tutorial and can be using this link below:

https://www.eclipse.org/downloads/

Choose the version that best suits your operating system and configuration.

Save eclipse to desire location and choose work-space.

Description:

Setting Up Eclipse
3 More Images

1) Once Eclipse is open, a blank screen should appear.

2) Create a New Java Project: Go to File > New > Java Project.

3) Name File ("Tic_Tac_Toe").

4) Create a New Java Class Inside created Java Project: Go to Tic_Tac_Toe > src > New > Class.

5) An empty class should open up.

Description:

Start adding code to your Tic_Tac_Toe class as shown in attached file.

High is the main logic of the program and how the class is organized:

Class TicTacToe
{

Board board = new char[2][2];

InitializeBoard();

while(true)

{

String userMove = getUserMove();

markMoveOnBoard('X', userMove);

gameStatus = getGameStatus('X', board);

if (!gameStatus.equals("InProgress"))

{

break;

}

String computerMove = getComputerMove();

markMoveOnBoard('O', computerMove);

gameStatus = getGameStatus('O', board);

if (!gameStatus.equals("InProgress"))

{

break;

}

}

}

Attachments

Description:

Run the program by clicking Run -> Run menu (or by Ctrl + F11) and start playing the Tic Tac Toe game.

You will be playing against the computer. When prompted for user move, provide your move in the format [row][column]

For example: [2][1] represents the third row and second column.

Keep playing until the game ends (User Wins, Computer Wins or Draw).

Description:

See attached image how the output looks while you play.

You have successfully learnt how to write a tic tac toe program in java!


YOU MIGHT ALSO LIKE