Sunday, August 23, 2009

Tic Tac Toe Artificial Intelligence

In this post I'm gonna include some of the codes I used personally to construct the computer AI.

It is mainly consist of if-else condition and it is not that relieable. If I were given a 2nd chance, I'll write a better AI codes that this one such as using switch/map/binary tree/etc .

If you are a student searching for an AI reference, I strongly recommend you to use your own creativity and understanding and please dont copy mine 100% . Because by copying my codes,
you'll not be able to gain the satisfaction feeling after you have comlpeted the codes.

Oh , one more thing! my AI is only about 70% intelligent I guess . I ensure you that you can't beat my AI if you are the 2nd player . Enjoy!

//-------AI codes begin ..............

if( (board[0]==human)&&(board[1]==human)&&(board[2]=='c') ) // blocks human moves 0 , 1 , 2 { move = 2 ; cout << "c" << endl ; return move ; } if( (board[0]==human)&&(board[2]==human)&&(board[1]=='b') ) // blocks human moves { move = 1 ; cout << "b" << endl ; return move ; } if( (board[2]==human)&&(board[1]==human)&&(board[0]=='a') ) // blocks human moves { move = 0 ; cout << "a" << endl ; return move ; }
if( (board[3]==human)&&(board[4]==human)&&(board[5]=='f') ) // blocks human moves 3 , 4 , 5 { move = 5 ; cout << "f" << endl ; return move ; }
if( (board[4]==human)&&(board[5]==human)&&(board[3]=='d') ) // blocks human moves { move = 3 ; cout << "d" << endl ; return move ; }
if( (board[6]==human)&&(board[7]==human)&&(board[8]=='i') ) // blocks human moves 6 , 7 , 8 { move = 8 ; cout << "i" << endl ; return move ; } if( (board[8]==human)&&(board[6]==human)&&(board[7]=='h') ) // blocks human moves { move = 7 ; cout << "h" << endl ; return move ; } if( (board[8]==human)&&(board[7]==human)&&(board[6]=='g') ) // blocks human moves { move = 6 ; cout << "g" << endl ; return move ; }
if( (board[0]==human)&&(board[3]==human)&&(board[6]=='g') ) // blocks human moves 0 , 3 , 6 { move = 6 ; cout << "g" << endl ; return move ; } if( (board[6]==human)&&(board[3]==human)&&(board[0]=='a') ) // blocks human moves { move = 0 ; cout << "a" << endl ; return move ; } if( (board[6]==human)&&(board[0]==human)&&(board[3]=='d') ) // blocks human moves { move = 3 ; cout << "d" << endl ; return move ; }
if( (board[1]==human)&&(board[4]==human)&&(board[7]=='h') ) // blocks human moves 1 , 4 , 7 { move = 7 ; cout << "h" << endl ; return move ; }
if( (board[4]==human)&&(board[7]==human)&&(board[1]=='b') ) // blocks human moves { move = 1 ; cout << "b" << endl ; return move ; }
if( (board[2]==human)&&(board[5]==human)&&(board[8]=='i') ) // blocks human moves 2 , 5 , 8 { move = 8 ; cout << "i" << endl ; return move ; } if( (board[8]==human)&&(board[2]==human)&&(board[5]=='f') ) // blocks human moves { move = 5 ; cout << "f" << endl ; return move ; } if( (board[5]==human)&&(board[8]==human)&&(board[2]=='c') ) // blocks human moves { move = 2 ; cout << "c" << endl ; return move ; }
if( (board[2]==human)&&(board[4]==human)&&(board[6]=='g') ) // blocks human moves 2 , 4 , 6 { move = 6 ; cout << "g" << endl ; return move ; }
if( (board[4]==human)&&(board[6]==human)&&(board[2]=='c') ) // blocks human moves { move = 2 ; cout << "c" << endl ; return move ; }
if(board[4] == 'e') { move = 4 ; cout << " e " << endl ; return move ; } if(board[0]=='a') { move = 0 ; cout << " a " << endl ; return move ; } if(board[2]=='c') { move = 2 ; cout << " c " << endl ; return move ; } if(board[6]=='f') { move = 6 ; cout << " g " << endl ; return move ; } if(board[8]=='i') { move = 8 ; cout << " i " << endl ; return move ; } if(board[1]=='b') { move = 1 ; cout << " b " << endl ; return move ; } if(board[3]=='d') { move = 3 ; cout << " d " << endl ; return move ; } if(board[5]=='f') { move = 5 ; cout << " f " << endl ; return move ; } if(board[7]=='h') { move = 7 ; cout << " h " << endl ; return move ; }

No comments:

Post a Comment