Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: Define a class TicTacToe with the necessary data and functions to play

ID: 3735106 • Letter: Q

Question

Question: Define a class TicTacToe with the necessary data and functions to play the game. Use a 2-D char a...

Define a class TicTacToe with the necessary data and functions to play the game. Use a 2-D char array with three rows and three columns as the game board. The class should also have the following functions:

A constructor to initialize the board. I suggest either asterisk ('*') or characters '1' through '9'.

A function called printBoard to fill in the board with either an 'X' or 'O' given row and column number (1-3) or position number (1-9), for example.

A function called computerTurn to play the computer's turn. This function will determine the row and column number to be filled in. You can use a simple algorithm such as look for the first space available, or random number generation. Complex AI algorithms will only be accepted with an in-person explanation of its behavior.

A function called playerTurn to play the user's turn. This function should ask the user for row (1-3) and column (1-3), or position (1-9) number. Note that position number is easier for the user to play but will have to be broken down into row and column number by your program.

A function called winVerify to verify if there is a win. This can be broken down by calling 3 separate private functions that can verify column win, row win or diagonal win. If there is no winner, let the user know there is a tie.

The main function should create a TicTacToe object and call the appropriate member functions to implement the logic of the game. For example, I expect you to have a loop that allows the user and computer to take turns until a win is detected.

Note that all member functions that you need to call in main through the TicTacToe object should be public, but functions needed only inside your class implementation (called only by other functions in your class) should be private.

You can decide who will have 'X' or 'O'. Remember this is a one player game only. The program will be the user vs the computer. Turn in a main, class cpp and class h files

Explanation / Answer

Answer :

#include <stdio.h>

void init_board(void);

void draw_board(void);

int user_first(void);

void play_game(int,char []);

int play_again(void);

void computer_move(void);

void player_move(void);

int find_win(char);

int middle_open(void);

int find_corner(void);

int find_side(void);

int symbol_won(char);

int square_valid(int,int);

char board[3][3];
char computer, user;

int main(void)
{
char name[30],ch;
int k;
printf("########### WELCOME TO TIC-TAC-TOE ########### ");
printf(" what is your name ?");
scanf("%s",name);
while(1)
{
k=user_first();
if (k)
   {
   printf("Do you want to play X ? ( y / n )");
   do
{
   ch = getchar();
} while ((ch != 'y') && (ch != 'Y') &&
   (ch != 'n') && (ch != 'N'));
   if(ch=='y'||ch=='Y')
   {
   computer = 'O';
   user = 'X';
   }
  
else
   {
   computer = 'X';
   user = 'O';
   }
  
   }
   init_board();
play_game(k,name);
if (!play_again())
   break;
}

return 0;
}

//first the board empty

void init_board(void)
{
int row, col;

printf(" ");
for (row = 0; row < 3; row++)
{
for (col = 0; col < 3; col++)
board[row][col] = ' ';
printf(" |---|---|---| ");
printf(" | | | | ");
}
printf(" |---|---|---| ");
}

/* Display the board as given. */
void draw_board(void)
{
int row, col;

printf(" ");
for (row = 0; row < 3; row++)
{
printf(" |---|---|---| ");
printf(" | %c | %c | %c | ", board[row][0], board[row][1], board[row][2]);
  
}
printf(" |---|---|---| ");
}

/* Ask the user wants to go first or not. Returns 1 if yes, 0 if no. */
int user_first(void)
{
char response;

printf("Do you want to go first? ( y / n )");
do
{
   response = getchar();
} while ((response != 'y') && (response != 'Y')&&
   (response != 'n') && (response != 'N'));

if ((response == 'y') || (response == 'Y'))
return 1;
else
return 0;
}

/* Loop iterates 9 turns or until somebody wins. */
void play_game(int tr,char name[30])
{
int turn;

for (turn = 1; turn <= 9; turn++)
{
if(tr==1)
{
printf(" * * %s's turn * * ",name);
player_move();
}
else
  
/* Check if tr is 0 or 1 to determine which player should move. */
   {
   printf(" * * computer's turn * * ");
   computer_move();
   }
   tr=(tr+1)%2;
  
draw_board();

if (symbol_won(computer)) {
   printf(" Computer WIN!!! ");
   return;
}
else if (symbol_won(user)) {
   printf(" Congratulations, you win! ");
   return;
}
}

printf(" The game is a draw. ");
return;
}

/* Ask if user wants to play again. Returns 1 if yes, 0 if no. */
int play_again(void)
{
char response;

printf("Do you want to play again? (y/n) ");
do
{
response = getchar();
} while ((response != 'y') && (response != 'Y')&&
   (response != 'n') && (response != 'N'));

if ((response == 'y') || (response == 'Y'))
return 1;
else
return 0;
}

/* Choose a move for the computer. */
void computer_move(void)
{
int square;
int row, col;

/* Use first strategy rule that returns valid square */
square = find_win(computer);
if (!square)
square = find_win(user);
if (!square)
square = middle_open();
if (!square)
square = find_corner();
if (!square)
square = find_side();

row = (square - 1) / 3;
col = (square - 1) % 3;

board[row][col] = computer;

}

/*
Find a win,  If a winning square exists, return the square;
Otherwise, return 0;
*/
int find_win(char symbol)
{
int square, row, col;
int result = 0;

for (square = 1; square <= 9; square++)
{
row = (square - 1) / 3;
col = (square - 1) % 3;

if (board[row][col] == ' ')
   {
   board[row][col] = symbol;
   if (symbol_won(symbol))
   result = square;
   board[row][col] = ' ';
   }
}

return result;
}

/* If middle square is empty, return 5; Otherwise return 0. */
int middle_open(void)
{
if (board[1][1] == ' ')
return 5;
else
return 0;
}

/* Return the number of an empty corner, if one exists; Otherwise return 0. */
int find_corner(void)
{
if (board[0][0] == ' ')
return 1;
if (board[0][2] == ' ')
return 3;
if (board[2][0] == ' ')
return 7;
if (board[2][2] == ' ')
return 9;

return 0;
}

int find_side(void)
{
if (board[0][1] == ' ')
return 2;
if (board[1][0] == ' ')
return 4;
if (board[1][2] == ' ')
return 6;
if (board[2][1] == ' ')
return 8;

return 0;
}

/* Check if the given symbol has already one the game. */
int symbol_won(char symbol)
{
int row, col;

for (row = 0; row < 3; row++)
{
if ((board[row][0] == symbol) && (board[row][1] == symbol)&& (board[row][2] == symbol))
   return 1;
}

for (col = 0; col < 3; col++)
{
if ((board[0][col] == symbol) && (board[1][col] == symbol)&& (board[2][col] == symbol))
   return 1;
}

if ((board[0][0] == symbol) && (board[1][1] == symbol)&& (board[2][2] == symbol))
return 1;

if ((board[0][2] == symbol) && (board[1][1] == symbol)&& (board[2][0] == symbol))
return 1;

return 0;
}

void player_move(void)
{
int square;
int row, col;

do
{
printf(" What row do you wat to play?(1 to 3)");
scanf("%d", &square);
row = (square - 1);
printf(" What column do you wat to play?(1 to 3)");
scanf("%d", &square);
col = (square - 1);
} while (!square_valid(row,col));

board[row][col] = user;

}

/* Check if the given square is valid and empty. */
int square_valid(int row,int col)
{
if (board[row][col] == ' ')
   return 1;
  

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote