HI, I'm having trouble making a victory checker that scales up or down with the table size automatically. It's suppose to have 4 check one on the horizontal, one on the vertical, one in the upper diagonal, and one in the lower diagonal. I can't seem tho get the checks right if anyone could help it would be much appreciated.
Edit: win requirement is 3 because you need 3 X's or O's in a row to win.
int VictoryCheck(char checkMe[ROWS][COLS], int winRequirement) {
int blankSeen = FALSE;
int XPresents = FALSE;
int OPresents = FALSE;
int counterOOB = 0;
int counterX = 0;
int counterO = 0;
// Is there a blank space on the board?
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (checkMe[i][j] == BLANK)
blankSeen = TRUE;
}
}
// Check to see if X or O present victory.
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
int n = 1;
// To check and see if one of the tokens is presenting
// a victory, write a loop that moves along the
// spots indicated by the direction of the scan. As
// you move to each spot, make sure it is logically
// legal on the board. If you see the same symbol
// at each point in the scan and the symbol isn't a
// blank space, you can conclude that token has
// presented victory and the appropriate flag
// (XPresents, OPresents) can be set to TRUE.
// Scan in a "horizontal" direction.
for (int x = 0; x <= checkMe[i][x]; x++) {
//scans the row for either X or O
if (checkMe[i][x] == MARKONE) {
counterX += 1;
}//resets the counter if the marker is different or a space is detected
elseif (checkMe[i][x] == BLANK || checkMe[i][x] == MARKTWO && counterX > 0) {
counterX = 0;
}
//checks for O
if (checkMe[i][x] == MARKTWO) {
counterO += 1;
}//resets the counter if the marker is different or a space is detected
elseif (checkMe[i][x] == BLANK || checkMe[i][x] == MARKONE && counterO > 0) {
counterO = 0;
}
n += 1;
}
//check results
if (counterX == winRequirement) {
XPresents = TRUE;
}
elseif (counterO == winRequirement) {
OPresents = TRUE;
}
//prints message
if (XPresents == TRUE) {
printf("\nX IS PRESENTING");
}
elseif (OPresents == TRUE) {
printf("\nO IS PRESENTING");
}
// Scan in a "vertical" direction.
for (int y = 0; y <= checkMe[y+n][j]; y++) {
if (checkMe[i][y + n] == MARKONE) {
counterX += 1;
}//resets the counter if the marker is different or a space is detected
elseif (checkMe[i][y + n] != MARKONE && counterX > 0) {
counterX = 0;
}
//checks for O
if (checkMe[i][y + n] == MARKTWO) {
counterO += 1;
}//resets the counter if the marker is different or a space is detected
elseif (checkMe[i][y + n] != MARKTWO && counterO > 0) {
counterO = 0;
}
n += 1;
}
//check results
if (counterX == winRequirement) {
XPresents = TRUE;
}
elseif (counterO == winRequirement) {
OPresents = TRUE;
}
//prints message
if (XPresents == TRUE) {
printf("\nX IS PRESENTING");
}
elseif (OPresents == TRUE) {
printf("\nO IS PRESENTING");
}
// Scan in a "diagonal-up" direction.
// Scan in a "diagonal-down" direction.
}
}
return 0;
}
It seems overly complicated (what is a "win requirement?"). Here's a simple example of checking for a horizontal win.
1 2 3 4 5 6 7 8 9 10 11 12
// Check board for a (horz.) win. Returns winning char or '-'.
char check(char b[][COLS]) {
int r, c;
for (r = 0; r < size; r++) {
for (c = 1; c < size; c++)
if (b[r][c] != b[r][0])
break;
if (c == size)
return b[r][0];
}
return'-'; // tie
}