Hi everyone I would like to ask if the code below can me modified to a 3x3 array if not I am ok with that too. The variable is board[3][3];
#include <stdio.h>
#include <stdlib.h>
/************************************
function prototype
***********************************/
void displayBoard();
int verifySelection(int, int);
void checkForWin();
/************************
gloobal variables
***********************/
char board[1][9];
char cWhoWon = ' ';
int iCurrentPlayer= 0;
/**************************************
Begin main function
************************************/
int main(int argc, const char * argv[])
{
int x;
int y;
int iSquareNum = 0;
for( x = 0; x < 1; x++){
for ( y = 0; y < 9; y++){
if ( x == 0){
board[x][y] = ' ';
}
}
}
displayBoard();
while (cWhoWon == ' '){
printf("\n%c\n", cWhoWon);
if (iCurrentPlayer == 1 || iCurrentPlayer == 0){
printf("\nPlayer X\n");
printf("Enter an available square number (1-9): ");
scanf("%d", &iSquareNum);
if( verifySelection(iSquareNum, iCurrentPlayer) == 1)
iCurrentPlayer = 1;
else
iCurrentPlayer = 2;
}
else {
printf("\nPlayer 0\n");
printf("Enter an available square number(1-9): ");
scanf("%d", &iSquareNum);
if(verifySelection(iSquareNum,iCurrentPlayer) == 1)
iCurrentPlayer = 2;
else
iCurrentPlayer = 1;
} //end if
displayBoard();
checkForWin();
} // end loop
return 0;
} //end main function
/****************************************************************************************
begin function definition
****************************************************************************************/
void displayBoard()
{
system("clear");
printf("\n\t\t|\t\t\t|\n");
printf("\t\t|\t\t\t|\n");
printf("\t%c\t|\t%c\t\t|\t%c\n", board[0][0], board[0][1], board[0][2]);
printf("------- | --------- | --------\n");
printf("\t\t|\t\t\t|\n");
printf("\t%c\t|\t%c\t\t|\t%c\n", board[0][3], board[0][4], board[0][5]);
printf("------- | --------- | --------\n");
printf("\t\t|\t\t\t|\n");
printf("\t%c\t|\t%c\t\t|\t%c\n", board[0][6], board[0][7], board[0][8]);
printf("\t\t|\t\t\t|\n");
} //end function definition
/****************************************************************************************
begin function definition
****************************************************************************************/
int verifySelection(int iSquare, int iPlayer){
if( board[0][iSquare -1] == ' ' && (iPlayer ==1 || iPlayer == 0) ){
board[0][iSquare - 1] = 'X';
return 0;
}
else if ( board[0][iSquare - 1] == ' ' && iPlayer == 2 ) {
board[0][iSquare - 1] = 'O';
return 0;
}
else
return 1;
}
void checkForWin()
{
int catTotal = 0;
int x;
int y;
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')
cWhoWon = 'X';
else if (board[0][3] == 'X' && board[0][4] == 'X' && board[0][5] == 'X')
cWhoWon = 'X';
else if (board[0][6] == 'X' && board[0][7] == 'X' && board[0][8] == 'X')
cWhoWon = 'X';
else if (board[0][0] == 'X' && board[0][3] == 'X' && board[0][6] == 'X')
cWhoWon = 'X';
else if (board[0][1] == 'X' && board[0][4] == 'X' && board[0][7] == 'X')
cWhoWon = 'X';
else if (board[0][2] == 'X' && board[0][5] == 'X' && board[0][8] == 'X')
cWhoWon = 'X';
else if (board[0][0] == 'X' && board[0][4] == 'X' && board[0][8] == 'X')
cWhoWon = 'X';
else if (board[0][2] == 'X' && board[0][4] == 'X' && board[0][6] == 'X')
cWhoWon = 'X';
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
cWhoWon = 'O';
else if (board[0][3] == 'O' && board[0][4] == 'O' && board[0][5] == 'O')
cWhoWon = 'O';
else if (board[0][6] == 'O' && board[0][7] == 'O' && board[0][8] == 'O')
cWhoWon = 'O';
else if (board[0][0] == 'O' && board[0][3] == 'O' && board[0][6] == 'O')
cWhoWon = 'O';
else if (board[0][1] == 'O' && board[0][4] == 'O' && board[0][7] == 'O')
cWhoWon = 'O';
else if (board[0][2] == 'O' && board[0][5] == 'O' && board[0][8] == 'O')
cWhoWon = 'O';
else if (board[0][0] == 'O' && board[0][4] == 'O' && board[0][8] == 'O')
cWhoWon = 'O';
else if (board[0][2] == 'O' && board[0][4] == 'O' && board[0][6] == 'O')
cWhoWon = 'O';
if (cWhoWon == 'X'){
printf("\nX Wins!!!\n");
return;
}
if (cWhoWon == 'O'){
printf("\nO Wins!!!\n");
return;
}
//check for CAT / draw game
for ( x = 0; x < 1; x++){
for(y = 0; y < 9; y++){
if (board[x][y] != ' ')
catTotal += 1;
}
}//end for loop
if ( catTotal == 9 ) {
cWhoWon = 'C';
printf("\nCAT Game!\n");
return;
}//end if
}//end function definition