Hey guys, im building a Tic Tac Toe game and im checking for a winner via this function(code below), Im juct wondering if there is a better way of doing this then how im doing it. if so could you point me in the right direction thanks :)
The array to begin with is initialized with an asterisk for each of its elements.
int checkBoard(const char array[][COLSIZE], const int rowSize) {
//variables
int i = 0;
char temp;
int winner = 5;
for (i = 0; i < rowSize; i++) { // checking the rows
if (array[i][0] == array[i][1] && array[i][0] == array[i][2]) {
temp = array[i][0];
}
}
for (i = 0; i < COLSIZE; i++) { // checking the columns
if (array[0][i] == array[1][i] && array[0][i] == array[2][i]) {
temp = array[0][i];
}
}
// This will check diagonals
if (array[0][0] == array[1][1] && array[1][1] == array[2][2]) {
temp = array[0][0];
}
if (array[0][2] == array[1][1] && array[1][1] == array[2][0]) {
temp = array[0][2];
}
if (temp == 'X') {
//player one wins
winner = 1;
} else if (temp == 'O') {
//player 2 wins
winner = 2;
} else if (temp != 'X' && temp != 'O') {
//check if board is completed
if (array[0][0] != '*' && array[0][1] != '*' && array[0][2] != '*' &&
array[1][0] != '*' && array[1][1] != '*' && array[1][2] != '*' &&
array[2][0] != '*' && array[2][1] != '*' && array[2][2] != '*') {
//game is complete
winner = 3;
} else {
//game not complete
winner = -1;
}
}
return winner;
}