#include int board [4] [4]; void initailize() { int i,j; for(i=1;i<=3;i++) for(j=1;j<=3;j++) board [i][j]=2; printf("\nThe board is initialized"); } void print_board(){ printf("\n\n"); int i,j; for(i=1;i<=3;i++) { for(j=1;j<=3;j++) if(j!=3) { if (board [i][j]==2) printf(" |"); else if( board [i][j]==3) printf(" X |"); else printf(" 0 |"); } else{ if (board [i][j]==2) printf(" "); else if( board [i][j]==3) printf(" X "); else printf(" 0 "); } if(i!=3){ printf("\n-----------\n"); } } printf("\n\n"); } int Make2(){ if (board[2][2]==2) return 5; else if (board[1][2]==2) return 2; else if (board[2][1]==2) return 4; else if (board[2][3]==2) return 6; else return 8; } void Go(int player, int position){ if(position==1) board[1][1]=player; else if(position==2) board[1][2]=player; else if(position==3) board[1][3]=player; else if(position==4) board[2][1]=player; else if(position==5) board[2][2]=player; else if(position==6) board[2][3]=player; else if(position==7) board[3][1]=player; else if(position==8) board[3][2]=player; else if(position==9) board[3][3]=player; } int PossWinX() { if(board[1][1]*board[1][2]*board[1][3]==18) { if(board[1][1]==2) return 1; else if(board[1][2]==2) return 2; else return 3; } else if(board[2][1]*board[2][2]*board[2][3]==18) { if(board[2][1]==2) return 4; else if(board[2][2]==2) return 5; else return 6; } else if(board[3][1]*board[3][2]*board[3][3]==18) { if(board[3][1]==2) return 7; else if(board[3][2]==2) return 8; else return 9; } else if(board[1][1]*board[2][1]*board[3][1]==18) { if(board[1][1]==2) return 1; else if(board[2][1]==2) return 4; else return 7; } else if(board[1][2]*board[2][2]*board[3][2]==18) { if(board[1][2]==2) return 2; else if(board[2][2]==2) return 5; else return 8; } else if(board[1][3]*board[2][3]*board[3][3]==18) { if(board[1][3]==2) return 3; else if(board[2][3]==2) return 6; else return 9; } else if(board[1][1]*board[2][2]*board[3][3]==18) { if(board[1][1]) return 1; else if(board[2][2]==2) return 5; else return 9; } else if(board[1][3]*board[2][2]*board[3][1]==18) { if(board[1][1]==2) return 3; else if(board[2][2]==2) return 5; else return 7; } else return 0; } int PossWinO() { if(board[1][1]*board[1][2]*board[1][3]==50) { if(board[1][1]==2) return 1; else if(board[1][2]==2) return 2; else return 3; } else if(board[2][1]*board[2][2]*board[2][3]==50) { if(board[2][1]==2) return 4; else if(board[2][2]==2) return 5; else return 6; } else if(board[3][1]*board[3][2]*board[3][3]==50) { if(board[3][1]==2) return 7; else if(board[3][2]==2) return 8; else return 9; } else if(board[1][1]*board[2][1]*board[3][1]==50) { if(board[1][1]==2) return 1; else if(board[2][1]==2) return 4; else return 7; } else if(board[1][2]*board[2][2]*board[3][2]==50) { if(board[1][2]==2) return 2; else if(board[2][2]==2) return 5; else return 8; } else if(board[1][3]*board[2][3]*board[3][3]==50) { if(board[1][3]==2) return 3; else if(board[2][3]==2) return 6; else return 9; } else if(board[1][1]*board[2][2]*board[3][3]==50) { if(board[1][1]==2) return 1; else if(board[2][2]==2) return 5; else return 9; } else if(board[1][3]*board[2][2]*board[3][1]==50) { if(board[1][3]==2) return 3; else if(board[2][2]==2) return 5; else return 7; } else return 0; } int check() { if(board[1][1]*board[1][2]*board[1][3]==125) return 5; else if(board[2][1]*board[2][2]*board[2][3]==125) return 5; else if(board[3][1]*board[3][2]*board[3][3]==125) return 5; else if(board[1][1]*board[2][1]*board[3][1]==125) return 5; else if(board[1][2]*board[2][2]*board[3][2]==125) return 5; else if(board[1][3]*board[2][3]*board[3][3]==125) return 5; else if(board[1][1]*board[2][2]*board[3][3]==125) return 5; else if(board[1][3]*board[2][2]*board[3][1]==125) return 5; else if(board[1][1]*board[1][2]*board[1][3]==27) return 3; else if(board[2][1]*board[2][2]*board[2][3]==27) return 3; else if(board[3][1]*board[3][2]*board[3][3]==27) return 3; else if(board[1][1]*board[2][1]*board[3][1]==27) return 3; else if(board[1][2]*board[2][2]*board[3][2]==27) return 3; else if(board[1][3]*board[2][3]*board[3][3]==27) return 3; else if(board[1][1]*board[2][2]*board[3][3]==27) return 3; else if(board[1][3]*board[2][2]*board[3][1]==27) return 3; else return 0; } int anywhere(){ int i , j; for(i=1; i<=3; i++) for(j=1;j<=3;j++) if(board[i][j]==2) { return (3*(i-1)+j); break; break; } } int check_input(int position) { if(position==1) if(board[1][1]==2) return 0; else return 1; else if(position==2) if(board[1][2]==2) return 0; else return 1; else if(position==3) if(board[1][3]==2) return 0; else return 1; else if(position==4) if(board[2][1]==2) return 0; else return 1; else if(position==5) if(board[2][2]==2) return 0; else return 1; else if(position==6) if(board[2][3]==2) return 0; else return 1; else if(position==7) if(board[3][1]==2) return 0; else return 1; else if(position==8) if(board[3][2]==2) return 0; else return 1; else if(position==9) if(board[3][3]==2) return 0; else return 1; } void Play_X(){ int player,position,status; initailize(); //Turn1 print_board(); printf("\nEnter the position of your move :::"); scanf("%d",&position); Go(3,position); printf("\nAfter turn 1 the board is as below :::"); print_board(); //Turn2 if(board[2][2]==2) Go(5,5); else Go(5,1); printf("\nAfter turn 2 the board is as below ::: \n"); print_board(); //Turn 3 T3: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T3; } } Go(3,position); printf("\nAfter turn 3 the board is as below ::: \n"); print_board(); //Turn 4 if(PossWinX()!=0) Go(5,PossWinX()); else Go(5,Make2()); printf("\nAfter turn 4 the board is as below :::\n"); print_board(); //Turn 5 T5: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T5; } } Go(3,position); printf("\nAfter turn 5 the board is as below :::\n "); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 6 if(PossWinO()!=0) Go(5,PossWinO()); else if(PossWinX()!=0) Go(5,PossWinX()); else Go(5,Make2()); printf("\nAfter turn 6 the board is as below ::: \n"); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 7 T7: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T7; }} Go(3,position); printf("\nAfter turn 7 the board is as below :::\n "); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 8 if(PossWinO()!=0) Go(5,PossWinO()); else if(PossWinX()!=0) Go(5,PossWinX()); else Go(5,anywhere()); printf("\nAfter turn 8 the board is as below :::\n"); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 9 T9: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T9; } } Go(3,position); printf("\nAfter Turn 9 the board is as below :::\n"); print_board(); if(check()!=0) { status=check(); goto result; } result : if(status==3) printf("\nHurrey!!!! Player X(Human) Wins the game !!!!!\n"); else if(status==5) printf("\nAaaahhh!!! Player O(Computer) Wins the Game !!!!!\n"); else printf("\nThe game is Drawn !!!!!\n"); } void Play_O(){ int player,position,status; initailize(); //Turn1 print_board(); Go(3,1); printf("\nAfter turn 1 the board is as below ::: \n"); print_board(); //Turn2 T2: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken....Enter another position.\n"); goto T2; } } Go(5,position); printf("\nAfter turn 2 the board is as below :::\n"); print_board(); //Turn 3 if(board[3][3]==2) Go(3,9); else Go(3,3); printf("\nAfter turn 3 the board is as below ::: \n"); print_board(); //Turn 4 T4: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken....Enter another position.\n"); goto T4; } } Go(5,position); printf("\nAfter turn 4 the board is as below :::\n"); print_board(); //Turn 5 if(PossWinX()!=0) Go(3,PossWinX()); else if(PossWinO()!=0) Go(3,PossWinO()); else if(board[3][1]==2) Go(3,7); else Go(3,3); printf("\nAfter turn 5 the board is as below ::: \n"); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 6 T6: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T6; }} Go(5,position); printf("\nAfter turn 6 the board is as below :::\n "); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 7 if(PossWinX()!=0) Go(3,PossWinX()); else if(PossWinO()!=0) Go(3,PossWinO()); else Go(3,anywhere()); printf("\nAfter turn 7 the board is as below :::\n "); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 8 T8: { printf("\nEnter the position of your move :::"); scanf("%d",&position); if(check_input(position)==1) { printf("\nPosition is previously taken... Enter Another position.\n"); goto T8; } } Go(5,position); printf("\nAfter Turn 8 the board is as below :::\n"); print_board(); if(check()!=0) { status=check(); goto result; } //Turn 9 if(PossWinX()!=0) Go(3,PossWinX()); else if(PossWinO()!=0) Go(3,PossWinO()); else Go(3,anywhere()); printf("\nAfter turn 9 the board is as below :::\n "); print_board(); if(check()!=0) { status=check(); goto result; } result : if(status==3) printf("\nHurrey!!!! Player X(Human) Wins the game !!!!!\n"); else if(status==5) printf("\nAaaahhh!!! Player O(Computer) Wins the Game !!!!!\n"); else printf("\nThe game is Drawn !!!!!\n"); } int main(){ int choice; while(1){ printf("\nEnter 3 for play as X"); printf("\nEnter 5 for play as O"); printf("\nEnter 100 for exit"); printf("\nEnter your choice :::"); scanf("%d",&choice); if(choice==100) break; else if(choice==3) Play_X(); else if(choice==5) Play_O(); else printf("\n\n your choice is wrong!!\n"); } }