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

 

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

Video Tutorial of Tic-Tac-Toe Game Playing – Program 1 in AI

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:

IndexCurrent Board positionNew Board position
0000000000000010000
1000000001020000001
2000000002000100002
3000000010002000010

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.

Let’s start with an empty board

Step 1: Now our board looks like 000 000 000 (ternary number) convert it into decimal no. The decimal no is 0

Step 2: Use the computed number i.e. 0 as an index into the move table and access the vector stored in New Board Position. The new board position is 000 010 000

Step 3: The vector selected in step 2 (000 010 000 ) represents the way the board will look after the move that should be made. So set the board equal to that vector. After completing the 3rd step your board looks like

Flowchart of Tic-Tac-Toe Game Playing Program 1 in AI

Summary:

This article discusses Tic-Tac-Toe Game Playing – Program (Approach) 1 in Artificial Intelligence. If you like the material share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *