Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int board[4][4];
- void initialize()
- {
- int i,j;
- for(i=1;i<=3;i++)
- for(j=1;j<=3;j++)
- board[i][j]=2;
- printf("\nThe board is initialized.\n");
- }
- 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(" O |");
- }
- else
- {
- if(board[i][j] == 2)
- printf(" ");
- else if (board[i][j]==3)
- printf(" X ");
- else
- printf(" O ");
- }
- 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,position;
- printf("\nEnter 3 for player X");
- printf(" Enter 5 for player O ::: ");
- scanf("%d",&player);
- printf("\nEnter the position ::: ");
- scanf("%d",&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]==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]==18)
- {
- if(board[1][3]==2)
- return 3;
- else if(board[2][2]==2)
- return 5;
- else
- return 7;
- }
- else
- return 0;
- }
- int PossWin0()
- {
- 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 checkin(){
- 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 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
- return 0;
- }
- main()
- {
- int choice,position,win;
- while(1)
- {
- printf("\nEnter 1 for initialization");
- printf("\nEnter 2 for Make2");
- printf("\nEnter 3 for Go");
- printf("\nEnter 4 for PossWinX");
- printf("\nEnter 5 for PossWin0");
- printf("\nEnter 6 for check in:");
- printf("\nEnter 10 for Print Board");
- printf("\nEnter 100 for Exit");
- printf("\nEnter your Choice ::: ");
- scanf("%d",&choice);
- if(choice==100)
- break;
- switch(choice)
- {
- case 1: initialize(); break;
- case 2: position=Make2();
- printf("\nMake2 Returns %d\n",position);
- break;
- case 3: Go();break;
- case 4: position=PossWinX();
- printf("\nPossWinX() Returns %d\n",position);
- break;
- case 5: position=PossWin0();
- printf("\n posswin0() returns %d\n",position);
- break;
- case 6 : win=checkin();
- printf("\n win: %d \n",win);
- break;
- case 10: print_board();break;
- default: printf("\nYour choice is wrong !!!\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement