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

Define a class TicTacToe with the necessary data and functions to play the game.

ID: 3734081 • Letter: D

Question

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

#include <cstdlib>
#include <iostream>
using namespace std;

class TicTacToe {
   char board[3][3];
public:
   TicTacToe() {
       for(int i = 0; i < 3; i++)
           for (int j = 1; j < 3; j++)
               board[i][j] = '0' + i*3 + j + 1;
   }

   void printBoard() {
       for(int i = 0; i < 3; i++) {
           for (int j = 0; j < 3; j++)
               if (j != 2)
                   cout << board[i][j] << "|";
               else
                   cout << board[i][j];
           cout << endl;
           for (int j = 0; j < 5 && i != 2; j++)
               cout << "-";
           cout << endl;
       }
   }

   void computerTurn(char ch) {
       if (ch != 'X' && ch != 'O') {
           cout << "INVALID SYMBOL" << endl;
           exit(1);
       }
       int i,j;
       do {
           i = rand() % 3;
           j = rand() % 3;
       } while (board[i][j] == 'O' || board[i][j] == 'X' );

       board[i][j] = ch;
   }

   void playerTurn(char ch) {
       if (ch != 'X' && ch != 'O') {
           cout << "INVALID SYMBOL" << endl;
           exit(1);
       }
       cout << "Enter a position: ";
       int pos;
       cin >> pos;
       int i = pos/3;
       int j = pos%3;
       while (i < 0 || i > 2) {
           cout << "Invalid position Enter a valid position: ";
           cin >> pos;
           i = pos/3;
           j = pos%3;
       }
       board[i][j] = ch;
   }

   char winVerify() {
       int i, j;

       for (i = 0; i < 3; i++) {
           for (j = 0; j < 3; j++)
               if (board[i][j] != 'X')
                   break;
           if (j == 3)
               return 'X';

           for (j = 0; j < 3; j++)
               if (board[i][j] != 'O')
                   break;
           if (j == 3)
               return 'O';


           for (j = 0; j < 3; j++)
               if (board[j][i] != 'X')
                   break;
           if (j == 3)
               return 'X';

           for (j = 0; j < 3; j++)
               if (board[j][i] != 'O')
                   break;
           if (j == 3)
               return 'O';

       }

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


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

       return 0;
   }
};


int main(int argc, char const *argv[])
{
   int choice;
   int count;
   char winner = 0;
   TicTacToe board1;
   do {
       cout << "0. Computer plays first" << endl;
       cout << "1. User plays first" << endl;
       cout << "Enter choice: " ;
       cin >> choice;
   } while(choice < 0 || choice > 1);

   char comp;
   char user;
   if (choice == 0) {
       comp = 'X';
       user = 'O';
   }
   else {
       comp = 'O';
       user = 'X';
   }

   for (count = 0; count < 9 && winner == 0; count ++) {
       board1.printBoard();
       if (count % 2 == choice)
           board1.computerTurn(comp);
       else
           board1.playerTurn(user);

       winner = board1.winVerify();
   }

   if (winner == 0) {
       cout << "DRAW" << endl;
   }
   else if (winner == comp) {
       cout << "Computer wins" << endl;
   }
   else {
       cout << "YOU won" << endl;
   }
   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