# Tic-Tac-Toe Game Playing – Program 1 in AI

Video Tutorials
 Machine Learning Video Tutorial Artificial Intelligence Tutorial Machine Learning Projects Tutorial Python Video tutorial Computer Graphics Video Tutorial C++ Video Tutorial Big Data Analytics Video Tutorial Soft Computing Video Tutorial Placement Video Tutorial Java Video Tutorial

## Tic-Tac-Toe Game Playing – Program (Approach) 1 in AI

It’s a paper and pencil game of two players X and O, who choose to mark a space on a grid of 3×3.

The game is won by the player who succeeds in putting three of their marks in a horizontal line, vertical line, or diagonal line.

This game is played by two players – One is a human and the other is a computer.

The objective is to write a computer program in such a way that the computer wins most of the time.

Three approaches are presented to play this game which increases in

Complexity

Use of generalization

Clarity of their knowledge

Extensibility of their approach

These approaches will move towards being representations of what we will call AI techniques

## Tic-Tac-Toe Game Playing – Program 1 in AI

Assume ,

Player 1 – X

Player 2 – O

So, a player who gets 3 consecutive marks first, they will win the game.

Let’s have a discussion about how a board’s data structure looks and how the Tic Tac Toe algorithm works.

Board Data Structure

Each square is numbered, which starts from 1 to 9 like following image

### Data Structures: Board and Move Table

Consider a Board having nine elements vector. Each element will contain

• 0 for blank
• 1 indicating X player move
• 2 indicating O player move

The computer may play as an X or O player.

The first player is always X.

Move Table:

It is a vector of 39 elements, each element of which is a nine-element vector representing board position.

Total of 39(19683) elements in move table

Sample Move Table looks like as shown below:

Few move examples based on the current board state:

## Algorithm – Tic Tac Toe Program 1

To make a move, do the following:

Step 1: View the vector (board) as a ternary number and convert it to its corresponding decimal number.

Step 2: Use the computed number as an index in the move table and access the vector stored there.

Step 3: The vector selected in step 2 represents the way the board will look after the move that should be made. So set the board equal to that vector.