Code:
#include <stdio.h>
#include <stdbool.h>
//input parameters
bool getSeed(int* pSeed);
int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL,
int* pCenC, int* pCenR, int* pBotL, int* pBotC, int* pbotR);
void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR,
int botL, int botC, int botR);
bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR,
int botL, int botC, int botR);
bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int
cenR, int botL, int botC, int botR);
void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int
cenR, int botL, int botC, int botR);
int main(void)
{
int seed;
int topL, topC, topR;
int cenL, cenC, cenR;
int botL, botC, botR;
getSeed(&seed);
genBoard(seed, &topL, &topC, &topR, &cenL, &cenC, &cenR, &botL,
&botC, &botR);
prtBoard(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
ifValid(topL, topC, topR, cenL, cenC, cenR, botL, botC, botR);
detWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC,
botR);
prtWinner(topL, topC, topR, cenL, cenC, cenR, botL, botC,
botR);
return 0;
}
bool getSeed(int* pSeed)
{
bool success = false;
int scanRes;
printf("\nName: Clayton Cushway");
printf("\nPlease enter a nonnegative integer seed: ");
scanRes = scanf("%d", pSeed);
if (scanRes == 1 && "%d" > 0)
success = true;
else if (scanRes == 0)
printf("\nNot an integer.");
else
printf("\nRan out of data.");
if (!success);
printf("\n\n");
return success;
}
int genBoard(int seed, int* pTopL, int* pTopC , int* pTopR, int* pCenL,
int* pCenC, int* pCenR, int* pBotL, int* pBotC,
int* pBotR)
{
int topL = *pTopL;
int topC = *pTopC;
int topR = *pTopR;
int cenL = *pCenL;
int cenC = *pCenC;
int cenR = *pCenR;
int botL = *pBotL;
int botC = *pBotC;
int botR = *pBotR;
*pTopL = (rand() % 2) ? 'X' : 'O';
*pTopC = (rand() % 2) ? 'X' : 'O';
*pTopR = (rand() % 2) ? 'X' : 'O';
*pCenL = (rand() % 2) ? 'X' : 'O';
*pCenC = (rand() % 2) ? 'X' : 'O';
*pCenR = (rand() % 2) ? 'X' : 'O';
*pBotL = (rand() % 2) ? 'X' : 'O';
*pBotC = (rand() % 2) ? 'X' : 'O';
*pBotR = (rand() % 2) ? 'X' : 'O';
}
void prtBoard(int topL, int topC, int topR, int cenL, int cenC, int cenR,
int botL, int botC, int botR)
{
printf("\n%c|%c|%c", topL, topC, topR);
printf("\n%c|%c|%c", cenL, cenC, cenR);
printf("\n%c|%c|%c", botL, botC, botR);
}
bool ifValid(int topL, int topC, int topR, int cenL, int cenC, int cenR,
int botL, int botC, int botR)
{
bool success = false;
int total = 0;
if (topL == 'X')
total++;
if (topC =='X')
total++;
if (topR =='X')
total++;
if (cenL == 'X')
total++;
if (cenC == 'X')
total++;
if (cenR == 'X')
total++;
if (botL == 'X')
total++;
if (botC == 'X')
total++;
if (botR == 'X')
total++;
if (total > 5)
printf("\nInvalid Board. There are too many X's");
if ( total < 4)
printf("\nInvalid Board. There are too many O's");
else
success = true;
(!success);
printf("\n\n");
return success;
}
bool detWinner(int topL, int topC, int topR, int cenL, int cenC, int
cenR, int botL, int botC, int botR)
{
int top, cen, bot, colL, colC, colR, diagL, diagR;
if (topL == 'X')
topL = 1;
else
topL = 0;
if (topC == 'X')
topC = 1;
else
topC = 0;
if (topR == 'X')
topR = 1;
else
topR = 0;
if (cenL == 'X')
cenL = 1;
else
cenL = 0;
if (cenC == 'X')
cenC = 1;
else
cenC = 0;
if (cenR == 'X')
cenR = 1;
else
cenR = 0;
if (botL == 'X')
botL = 1;
else
botL = 0;
if (botC == 'X')
botC = 1;
else
botC = 0;
if (botR == 'X')
botR = 1;
else
botR = 0;
top = topL + topC + topR;
cen = cenL + cenC + cenR;
bot = botL + botC + botR;
colL = topL + cenL + botL;
colC = topC + cenC + botC;
colR = topR + cenR + botR;
diagL = topL + cenC + botR;
diagR = topR + cenC + botL;
if(top == 3 || cen == 3 || bot == 3 || colL == 3 || colC == 3 ||
colR == 3 || diagL == 3 || diagR == 3)
{
printf("\nX's are the winner");
}
if(top == 0 || cen == 0 || bot == 0 || colL == 0 || colC == 0 ||
colR == 0 || diagL == 0 || diagR == 0)
{
printf("\nO's are the winner");
}
}
void prtWinner(int topL, int topC, int topR, int cenL, int cenC, int cenR,
int botL, int botC, int botR)
{
}