shah123

Hi, I am developing TicTacToe in which i finished working functionlity where 2 people will play.

Now i want to add AI to my game what can be the best algorithm to do that. One more thing any piece of algorithm or any starting code would be very nice.

Now just a real question lets say

Player selected one box. Now its Computer turn. It has 8 possible moves how would you implement that can you give any idea in algorithm shape or Pseudocode please.

I would probably not write code to evaluate each state individually, I'd write a state evaluator that checked for winning moves, blocking moves, and then potential moves based on difficulty.

thanks

### Re: Visual C# General Need to implement AI in TicTacToe

IsshouFuuraibou

I would probably convert the board into a md array/matrix such that an empty board is:
[0,0,0]
[0,0,0]
[0,0,0]

After a move, ( using 1 for X and 2 for O )
[1,0,0]
[0,0,0]
[0,0,0]

evaluate the board by looking for the possible win and blocks first. Here you'll introduce AI difficulty. You don't want an Easy AI to always block. In this first case there is no block or winning move. Use some logic to determine where to play.

You'd always want to check for a possible win first and make that move, it should be fairly easy, it is similar to checking for and selecting blocking moves (which is shown later on in the post)

If you disassemble how to play tic-tac-toe, it should result in a good set of logical moves.
For example,
Code Snippet

if ( Corner )
SelectCenter();
else if ( Side )
SelectOpposingEdge();
else // center selected
SelectCorner();

That is just a general first round type of move.

To look at blocking, you just need to check each winning possibility,
Code Snippet

for (int i = 0; i < 3; i++)
{
if ( RowWin(i) )
{
BlockRow(i);
return; // move has been made
}
else if ( ColWin(i) )
{
BlockCol(i);
return; // move has been made
}
}
if ( DiagWinA() )
{
BlockDiagWinA();
return;
}
else if ( DiagWinB() )
{
BlockDiagWinB();
return;
}
// no move has been made, make a move

Each block call would judge whether it should block or not based on random chance with a distribution based on difficulty, it should also determing itself which location it needs to select to block, and select it.

You could generalize the check for win to accept which player to check for and use it in both win/block situation.

You should probably evaluate each move slightly differently, though you may still be able to generalize it enough and make profiles of the md array to respond to.

A perfect AI in tic-tac-toe will always be able to win or cause a tie, there's a logical way of playing that will cause this. Actually two ways, based on who is going first. A win or tie can almost always be decieded on the defending ( O ) player's first or second move, if using the right logic to play from.

### Re: Visual C# General Need to implement AI in TicTacToe

shah123

Hi Eric,
Code Snippet

Two things

firstly my two players playing methods are as following:

public int player = 1;
private PictureBox[] pics;
private Label[] lbls;
private int userLoggedIn = 0;
public int computer=1;
private List<int>[] moves = new List<int>[2] { new List<int>(), new List<int>() };
private void box1_Click(object sender, EventArgs e)
{
if (userLoggedIn == 2)
{
box1.SizeMode = PictureBoxSizeMode.StretchImage;
box1.Image = pics[player - 1].Image;
moves[player - 1].Add(1);
++this.movenum;
box1.Enabled = false;
switchPlayers();
}
}

Which means when 2 users are logged in they can select only then picture box to start play so i found the way that when user want to play against PC he will just need to type pc on the textbox and image will be selected by default by the program and then second user can login or vice versa and start playing.

The logic will be if user entered first then he will select the box first if pc is login first then PC will start playing. And if they tie then user will select first if anybody win will do the game first.
(Still need to implement)

Secondly the logic which you explained can you show me some more coding way to do it as i want computer to be very expert against my moves.