# Tic-Tac-Toe Game using Magic Square – Program 2 in AI

[wptelegram-join-channel]

## Tic-Tac-Toe Game Playing using Magic Square – Program 2 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,

The complexity of the approach

Use of generalization of the approach

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 using Magic Square – Program 2 in AI

Assume,

Player 1 – uses X and

Player 2 – use O

So, a player who gets 3 consecutive marks first, the player 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.

## Magic Square

Here, we assign board positions to vector elements.

The Sum of all rows, columns, and diagonals must be 15.

### Algorithm – Tic-Tac-Toe Game Playing using Magic Square – Program 2 in AI

The first machine will check, the chance to win,

• If the difference between 15 and the sum of the two squares, If this difference is not positive or if it is greater than 9, then the original two squares were not collinear and so can be ignored.

or it will check the opponent of winning and block the chances of winning.

Here, the objective is machine will win the maximum number of matches.

Since no player can have more than four squares at a time, there will be many fewer squares examined using this scheme than there were using the more straightforward approach of Program 2.

This shows how the choice of representation can have a major impact on the efficiency of a problem-solving program.

### Comments on Tic-Tac-Toe Game Playing using Magic Square – Program 2 in AI

This comparison raises an interesting question about the relationship between the way people solve problems and the way computers do.

Why do people find the row-scan approach easier while the number-counting approach is more efficient for a computer?

We do not know enough about how people work to answer that question completely.

One part of the answer is that people are parallel processors and can look at several parts of the board at once, whereas the conventional computer must look at the squares one at a time.

Sometimes an investigation of how people solve problems sheds great light on how computers should do so.

At other times, the differences in the hardware of the two seem so great that different strategies seem best.

As we learn more about problem-solving both by people and by machines, we may know better whether the same representations and algorithms are best for both people and machines.

## Example:

Now, the computer will check its possibility of winning the game.

First, calculate the difference between the 15 and the sum of two positions.

Diff = 15 – (5+4) = 6

6 is not empty, hence Computer can’t win the game.

Now, the computer checks the possibility of opponents winning the match. If the opponent is winning black it.

Diff = 15 – (8+6) = 1

1 is empty, hence the human can win the game.

Hence Computer Blocks it.

Computer – go to 1

Now, it’s Player Human Turn,

Now, the computer will check its possibility of winning the game.

Diff = 15 – (5+4) = 6

6 is not empty, hence Computer can’t win the game.

Diff = 15 – (1+4) = 10

10 is greater than 9, hence Computer can’t win the game.

Diff = 15 – (1+5) = 9

9 is empty, hence Computer can win the game. Computer – go to 9