0% found this document useful (0 votes)
39 views8 pages

Aritificial Intelligence EXP1

The document describes an experiment to implement an AI agent that can play tic-tac-toe. It includes the program code for the AI agent that will try to win by blocking opponent's moves or making winning moves where possible. The code implements functions for checking the board status, making moves on the board, and playing the full game between the AI agent and human player.

Uploaded by

sakharam_gawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

Aritificial Intelligence EXP1

The document describes an experiment to implement an AI agent that can play tic-tac-toe. It includes the program code for the AI agent that will try to win by blocking opponent's moves or making winning moves where possible. The code implements functions for checking the board status, making moves on the board, and playing the full game between the AI agent and human player.

Uploaded by

sakharam_gawade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF COMPUTER ENGINEERING Experiment No.

01
Semester B.E. Semester VII Computer Engineering
Subject Artificial Intelligence
Subject Professor In-charge Prof. Umesh Kulkarni
Assisting Teachers -
Laboratory 313A

Student Name Sakharam Gawade


Roll Number 14102C2015
Grade and Subject
Teachers Signature

Experiment Number 01

Experiment Title Tic-Tac-Toe Problem


Resources / Apparatus Hardware: Software:
Required IBM PC Compatible Computer GCC, Codeblocks
System

Objectives To understand and explore the mechanism of mind that enable intelligent
(Skill Set / Knowledge thought and action.
Tested / Imparted)

Theory of Operation
Program Code #include<stdio.h>
int make2(int board[])
{
if(board[4]==2)
{
return 4;
}
else
{
if(board[1]==2)
{
return 1;
}
else
if(board[3]==2)
{
return 3;
}
else
if(board[5]==2)
{
return 5;
}
else
if(board[7]==2)
{
return 3;
}
}
return -1;
}
int posswin(int board[], int player)
{
int i;
int win=18;
if(player==1)
{
win=50;
}

if(board[0]*board[1]*board[2]==win)
{
if(board[0]==2)
{
return 0;
}
else if(board[1]==2)
{
return 1;
}
else if(board[2]==2)
{
return 2;
}
}
else if(board[3]*board[4]*board[5]==win)
{
if(board[3]==2)
{
return 3;
}
else if(board[4]==2)
{
return 4;
}
else if(board[5]==2)
{
return 5;
}

}
else if(board[6]*board[7]*board[8]==win)
{
if(board[6]==2)
{
return 6;
}
else if(board[7]==2)
{
return 7;
}
else if(board[8]==2)
{
return 8;
}
}
else if(board[0]*board[3]*board[6]==win)
{
if(board[0]==2)
{
return 6;
}
else if(board[3]==2)
{
return 7;
}
else if(board[6]==2)
{
return 8;
}
}
else if(board[1]*board[4]*board[7]==win)
{
if(board[1]==2)
{
return 6;
}
else if(board[4]==2)
{
return 7;
}
else if(board[7]==2)
{
return 8;
}

}
else if(board[2]*board[5]*board[8]==win)
{
if(board[2]==2)
{
return 6;
}
else if(board[5]==2)
{
return 7;
}
else if(board[8]==2)
{
return 8;
}
}

else if(board[0]*board[4]*board[8]==win)
{
if(board[0]==2)
{
return 6;
}
else if(board[4]==2)
{
return 7;
}
else if(board[8]==2)
{
return 8;
}
}
else if(board[2]*board[4]*board[6]==win)
{
if(board[2]==2)
{
return 6;
}
else if(board[4]==2)
{
return 7;
}
else if(board[6]==2)
{
return 8;
}

}
return -1;
}
int go(int pos,int turn,int board[])
{
if(turn%2==0)
{
board[pos]=3;
}
else
{
board[pos]=5;
}
}
int printboard(int board[])
{
int i,j;
for(i=0;i<9;i++)
{

if(i%3==0)
{
printf("\n");
} printf("|");
if(board[i]==2)
{
printf("%d",i+1);
}
else if(board[i]==3)
{
printf("X");
}
else if(board[i]==5)
{
printf("O");
}
printf("|");
}
printf("\n");
return 0;
}
int checkwin(int board[], int player)
{
int i;
int win=27;
if(player==1)
{
win=125;
}

if(board[0]*board[1]*board[2]==win)
return 1;
else if(board[3]*board[4]*board[5]==win)
return 1;
else if(board[6]*board[7]*board[8]==win)
return 1;
else if(board[0]*board[3]*board[6]==win)
return 1;
else if(board[1]*board[4]*board[7]==win)
return 1;
else if(board[2]*board[5]*board[8]==win)
return 1;

else if(board[0]*board[4]*board[8]==win)
return 1;
else if(board[2]*board[4]*board[6]==win)
return 1;

return -1;
}
int main()
{
int turn;
int player;
int pos;
int move,move1;
int board[9]={2,2,2,2,2,2,2,2,2};
int i,j;
printboard(board);
for(i=0;i<9;i++)
{

if(i%2==0)
{
printf("Enter a position");
scanf("%d",&pos);
if(board[pos-1]!=2 )
{
printf("Invalid move\n");
i--;
continue;
}
go(pos-1,i,board);
board[pos-1]=3;
if(checkwin(board,0)==1)
{
printf("\nCongratulations,
you win!");
break;
}
}
else
{
move=posswin(board,0);
if(move!=-1)
{

go(move,i,board);
}
else
{
move=posswin(board,1);
if(move!=-1)
{
go(move,i,board);
}
else
{
move1=make2(board);
if(make2(board)!=-1)
{

go(move1,i,board);
}
else
{

for(j=0;j<9;j++)
{

if(board[j]==2)
{

go(j,i,board);
}
}
}
}
}
if(checkwin(board,1)==1)
{
printf("\nNice try, opponent
wins");
break;
}
}
printboard(board);
}
return 0;
}
Output

Conclusion A basic problem such as tic tac toe requires artificial intelligence so
that it could predict the best locations which could make the
opponent lose the match.

You might also like