You are on page 1of 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
Teacher’s 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. } else .h> int make2(int board[]) { if(board[4]==2) { return 4. } else { if(board[1]==2) { return 1.

int player) { int i. if(player==1) { win=50. } else if(board[2]==2) { return 2. } } else if(board[6]*board[7]*board[8]==win) { if(board[6]==2) { . } else if(board[7]==2) { return 3. int win=18. } else if(board[5]==2) { return 5. } else if(board[5]==2) { return 5. } if(board[0]*board[1]*board[2]==win) { if(board[0]==2) { return 0. if(board[3]==2) { return 3. } else if(board[1]==2) { return 1. } } else if(board[3]*board[4]*board[5]==win) { if(board[3]==2) { return 3. } else if(board[4]==2) { return 4. } int posswin(int board[]. } } return -1.

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

} else if(board[i]==5) . } else if(board[8]==2) { return 8. } else if(board[4]==2) { return 7.i<9. } int go(int pos. } else { board[pos]=5. } else if(board[4]==2) { return 7. } else if(board[i]==3) { printf("X").int turn. } else if(board[6]==2) { return 8. } } int printboard(int board[]) { int i. if(board[i]==2) { printf("%d". } } return -1. } } else if(board[2]*board[4]*board[6]==win) { if(board[2]==2) { return 6.i++) { if(i%3==0) { printf("\n").i+1). if(board[0]==2) { return 6.j.int board[]) { if(turn%2==0) { board[pos]=3. } printf("|"). for(i=0.

for(i=0.board).j. int player. } printf("|").2. int pos.2. } printf("\n"). int move. else if(board[1]*board[4]*board[7]==win) return 1.2. } int checkwin(int board[]. else if(board[0]*board[3]*board[6]==win) return 1. else if(board[2]*board[5]*board[8]==win) return 1. int win=27. else if(board[3]*board[4]*board[5]==win) return 1. } go(pos-1. continue. int i.2.2. if(board[pos-1]!=2 ) { printf("Invalid move\n"). i--.2.i++) { if(i%2==0) { printf("Enter a position"). return -1.i<9. int board[9]={2.2}. else if(board[2]*board[4]*board[6]==win) return 1. return 0. } int main() { int turn.i. . printboard(board).&pos). board[pos-1]=3. { printf("O"). if(player==1) { win=125. else if(board[6]*board[7]*board[8]==win) return 1. } if(board[0]*board[1]*board[2]==win) return 1.move1. scanf("%d". else if(board[0]*board[4]*board[8]==win) return 1. int player) { int i.2.

board).1). } return 0. break.0)==1) { printf("\nCongratulations.board). if(checkwin(board.board). break.i. if(move!=-1) { go(move. if(make2(board)!=-1) { go(move1. } } } } } if(checkwin(board.i. } else { move=posswin(board.i. you win!"). } else { for(j=0. if(move!=-1) { go(move.i.j++) { if(board[j]==2) { go(j. } } else { move=posswin(board.board).1)==1) { printf("\nNice try. } else { move1=make2(board). } } printboard(board).0). opponent wins"). } .j<9.

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. .