Sei sulla pagina 1di 7

<SOURCE CODE>

#include<stdio.h>

// For printf() & scanf()

#include<conio.h>

// For getch() & clrscr()

char matrix[3][3];
int m,n,i,j,sum,ch;

void cou(void);
void introduction();

// The Play Box


// This Fn. Gives rules of the game

void main()
{clrscr();
introduction();
getch();
ch=1;
while(ch==1)
{clrscr();
for (m=0;m<3;m++)
for (n=0;n<3;n++)
matrix[m][n]='\0'; // Initialization of the matrix
sum=0;
while(sum<10)
{if(sum==0)
cou();
printf("Player 1 is 'X': choose the row and column\n\n");

printf("Row : ");
scanf("%d",&i);
printf("\nColumn : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1])||'O'==matrix[i-1][j-1];)
{printf("Sorry!!!, but you gotta choose another place.\n");
printf ("Row : ");
scanf("%d",&i);
printf("\nColumn : ");
scanf("%d",&j);
}
matrix[i-1][j-1]='X';
sum++;
cou();

//check if player1 wins


if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
{printf("\nPlayer 1 wins");
break;
}
if(matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] &&
matrix[1][1]==matrix[0][2])
{printf("\nPlayer 1 wins");
break;
}

if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] &&


matrix[1][0]==matrix[2][0])
{printf("\nPlayer 1 wins");
break;
}
if(matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] &&
matrix[1][1]==matrix[2][1])
{printf("\nPlayer 1 wins");
break;
}
if(matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] &&
matrix[1][2]==matrix[2][2])
{printf("\nPlayer 1 wins");
break;
}
if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] &&
matrix[0][1]==matrix[0][2])
{printf("\nPlayer 1 wins");
break;
}
if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] &&
matrix[1][1]==matrix[1][2])
{printf("\nPlayer 1 wins");
break;
}
if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] &&
matrix[2][1]==matrix[2][2])
{printf("\nPlayer 1 wins");

break;
}
if (sum==9)

//sum=9 coz there are only 9 boxes in the game


{printf("\n The game is over and no one wins, hahaha, you

both
stink!!!\n");
break;
}

//player 2's turn


printf("Player 2 is 'O': choose the row and column");
printf("\nRow : ");
scanf("%d",&i);
printf("\nColumn : ");
scanf("%d",&j);
for(;i>3||i<1||j>3||j<1||('X'==matrix[i-1][j-1])||'O'==matrix[i-1][j-1];)
{printf("Sorry!!!, but you gotta choose another place.\n");
printf ("Row : ");
scanf("%d",&i);
printf("\nColumn:");
scanf("%d",&j);
}
matrix[i-1][j-1]='O';
sum++;
cou();

//check if player2 win


if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
{printf("\nPlayer 2 wins");
break;
}
if(matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] &&
matrix[1][1]==matrix[0][2])
{printf("\nPlayer 2 wins");
break;
}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] &&
matrix[1][0]==matrix[2][0])
{printf("\nPlayer 2 wins");
break;
}
if(matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] &&
matrix[1][1]==matrix[2][1])
{printf("\nPlayer 2 wins");
break;
}
if(matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] &&
matrix[1][2]==matrix[2][2])
{printf("\nPlayer 2 wins");
break;
}
if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] &&

matrix[0][1]==matrix[0][2])
{printf("\nPlayer 2 wins");
break;
}
if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] &&
matrix[1][1]==matrix[1][2])
{printf("\nPlayer 2 wins");
break;
}
if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] &&
matrix[2][1]==matrix[2][2])
{printf("\nPlayer 2 wins");
break;
}
}

printf("\nWould you like to play again(PRESS 1 TO PLAY AGAIN ELSE


PRESS
2) ");
scanf("%d",&ch);
}
}

void cou()

//the play box

{printf("\n\t\t 1 2 3\n\n");
printf("\t\t1 %c | %c | %c\n", matrix[0][0],matrix[0][1],matrix[0][2]);
printf("\t\t2 %c | %c | %c\n", matrix[1][0],matrix[1][1],matrix[1][2]);
printf("\t\t3 %c | %c | %c\n\n\n", matrix[2][0],matrix[2][1],matrix[2][2]);
}

void introduction()
{clrscr();
printf("\n There is a 3 by 3 array of cells.First you have to choose one cell,
then after that the chance will be given to other player to choose an unmarked
cell.\nA person wins, if he covers any one of th entire row, or entire column
or entire diagonal cells. If one of the players wins, and if all the cells are
covered,then the game is draw.");
}

Potrebbero piacerti anche