I'm just about done. Only issue is simplifying this, if possible, to make it look pretty for my professor's eyes.
Note: I've only briefly tested it. So, if you see something that may not work, then let me know.
BOARD_SQ_SIZE is a constant that is set to 3 since the board size is 3x3.
board is set to [3][3]. A two dimensional array.
playerTurn switches between X and O.
Return 1 if 3 blocks (equivalent to BOARD_SQ_SIZE) are found to be equal to playerTurn, indicating that that player won.
int CheckBoard(char board[][BOARD_SQ_SIZE], char (*playerTurn))
{
int rowCheck, //What row are we currently checking?
colCheck, //What column are we currently checking?
horChecks = 0, //How many blocks have we checked vertically or horizontally
vertChecks = 0,
diagRightChecks = 0, //How many blocks we have checked diagonally going to the right or to the left
diagRightVal = 0, //The starting point for the diagonal right value. We want to check the top left going to the bottom right for this one. So, start at 0 as an x value.
diagLeftChecks = 0,
diagLeftVal = BOARD_SQ_SIZE - 1; //The starting point for the diagonal right value. We want to check the bottom left going to the top right for this one. So, start at 2 as an x value.
for(rowCheck = 0; rowCheck < BOARD_SQ_SIZE; rowCheck++)
{
for(colCheck = 0; colCheck < BOARD_SQ_SIZE; colCheck++)
{
if(board[rowCheck][colCheck] == *playerTurn)
{
if(vertChecks + 1 < BOARD_SQ_SIZE)
{
vertChecks++;
}
else
{
return 1;
}
if(rowCheck == diagRightVal)
{
if(diagRightChecks + 1 < BOARD_SQ_SIZE)
{
diagRightVal++;
diagRightChecks++;
}
else
{
return 1;
}
}
if(rowCheck == diagLeftVal)
{
if(diagLeftChecks + 1 < BOARD_SQ_SIZE)
{
diagLeftVal++;
diagLeftChecks++;
}
else
{
return 1;
}
}
}
if(board[colCheck][rowCheck] == *playerTurn)
{
if(horChecks + 1 < BOARD_SQ_SIZE)
{
horChecks++;
}
else
{
return 1;
}
}
}
vertChecks = 0; //Reset because the column was checked and failed.
horChecks = 0; //Same
}
return 0;
}
Logical Error:
When calling the function, pass the character you want to check (in this example 'x')