Code:
int genBoard(int input)
{
int topL = (rand() % 2);
int topC = (rand() % 2);
int topR = (rand() % 2);
int cenL = (rand() % 2);
int cenC = (rand() % 2);
int cenR = (rand() % 2);
int botL = (rand() % 2);
int botC = (rand() % 2);
int botR = (rand() % 2);
if(topL == 1)
printf("X");
if(topL == 0)
printf("O");
if(topC == 1)
printf("X");
if(topC == 0)
printf("O");
if(topR == 1)
printf("X");
if(topR == 0)
printf("O");
if(cenL == 1)
printf("X");
if(cenL == 0)
printf("O");
if(cenC == 1)
printf("X");
if(cenC == 0)
printf("O");
if(cenR == 1)
printf("X");
if(cenR == 0)
printf("O");
if(botL == 1)
printf("X");
if(botL == 0)
printf("O");
if(botC == 1)
printf("X");
if(botC == 0)
printf("O");
if(botR == 1)
printf("X");
if(botR == 0)
printf("O");
}
void printBoard(int topL, int topC, int topR, int cenL, int cenC,
int cenR, int botL, int botC, int botR)
{
printf(" %c | %c | %c \n", topL, topC, topR);
printf(" %c | %c | %c \n", cenL, cenC, cenR);
printf(" %c | %c | %c \n", botL, botC, botR);
}
bool ifValid(int topL, int topC, int topR, int cenL,
int cenC, int cenR, int botL, int botC, int botR)
{
int total = topL + topC + topR + cenL + cenC+ cenR + botL + botC + botR;
if(total !=4 && total !=5)
return false;
}
int winner(int topL, int topC, int topR, int cenL, int cenC,
int cenR, int botL, int botC, int botR)
{
int win;
int topRow, midRow, botRow;
int lCol, midCol, rCol;
int rDiag, lDiag;
if(topL == topC == topR)
{
win == topRow;
}
printf("\nThe winning pattern is the top row.");
if(cenL == cenC == cenR)
{
win == midRow;
}
if(botL == botC == botR)
{
win == botRow;
}
if(topL == cenL == botL)
{
win == lCol;
}
if(topC == cenC == botC)
{
win == midCol;
}
if(topL == cenC == botR)
{
win == rDiag;
}
if(botL == cenC == topR)
{
win == lDiag;
}
return 1;
}
void display(int input, int topRow, int midRow, int bottomRow, int lCol,
int midCol, int rCol, int lDiag, int rDiag)
{
if(topRow == 'X')
printf("Congratulations, X! You are the winner.");
if(topRow == 'O')
printf("Congratulations, O! You are the winner.");
}