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

Create a class TicTacToe that will enable you to write a program to play Tic-Tac

ID: 3851545 • Letter: C

Question

Create a class TicTacToe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY.

Allow two human players. Wherever the first player moves, place an X in the specified square, and place an O wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it’s a draw.

It should be console application and then Modify your submission for Assignment 1 to include Graphical User Interface.

Explanation / Answer

The code for two players is as follows:


#include<iostream.h>

enum values { X,O,EMPTY};

class TicTacToe {
private:
     values tictactoe[3][3];

public:
     TicTacToe(){
       for (int i = 0; i<3; i++)
          for (int j = 0; j<3; j++)
              tictactoe[i][j] = EMPTY;

     }
     values check_game(){ // Checks for winners
        int count1,count2;
        values val1 = X;
        values val2 = O;
        values val3 = EMPTY;

        for (int i = 0; i<3; i++){ //Checking rows
            count1 = 0;
            count2 = 0;
            for (int j = 0; j<3; j++){
                if (tictactoe[i][j] == val1)
                   count1++;
                if (tictactoe[i][j] == val2)
                   count2++;
            }
            if (count1 == 3) {
               return val1
            }
            if (count2 == 3) {
               return val2
           }
                
        }
        for (int i = 0; i<3; i++){ //Checking columns
            count1 = 0;
            count2 = 0;
            for (int j = 0; j<3; j++){
                if (tictactoe[j][i] == val1)
                   count1++;
                if (tictactoe[j][i] == val2)
                   count2++;
            }
            if (count1 == 3) {
               return val1
            }
            if (count2 == 3) {
               return val2
            }
                
        }
        // Checking Diagonls

        if (tictactoe[0][0] == val1 && tictactoe[1][1] == val1 && tictactoe[2][2] == val1)
           return val1
        if (tictactoe[0][2] == val1 && tictactoe[1][1] == val1 && tictactoe[2][0] == val1)
           return val1
        if (tictactoe[0][0] == val2 && tictactoe[1][1] == val2 && tictactoe[2][2] == val2)
           return val2
        if (tictactoe[0][2] == val2 && tictactoe[1][1] == val2 && tictactoe[2][0] == val2)
          return val2
        return val3;   // This means no winner
     }
     void disp(){   // displaying the game
         for (int i = 0; i<3; i++) {
             for (int j = 0; j<3; j++)
                cout << tictactoe[i][j] << " ";
             cout << endl;
         }
     }
};


void main(){

   int count;
   values result;
   int valid;
   TicTacToe game;

   count = 0;
   result = game.check_game();   //checking for winners
   game.disp();
   while (result != X && result != O && count < 10){
      
       if (count % 2 == 0) {
          valid = 0;
          while (valid == 0){
              cout << "First Player: Enter row and column:" << endl;   // X is an entry for First Player
              cin >> i >> j;
              if ( (i>2 && i < 0) || (j>2 && j < 0) || (tictactoe[i][j] != EMPTY))
                  cout << "Invalid data" << endl;
              else
                 valid = 1;
          }
          tictactoe[i][j] = X;
       }
       else {
          valid = 0;
          while (valid == 0){
              cout << "Second Player: Enter row and column:" << endl; //O is an entry for second Player
              cin >> i >> j;
              if ( (i>2 && i < 0) || (j>2 && j < 0) || (tictactoe[i][j] != EMPTY))
                  cout << "Invalid data" << endl;
              else
                 valid = 1;
          }
          tictactoe[i][j] = O;
       }
       game.disp();
       result = game.check_game();
       count++;
   }
   if (result == X)
     cout << "First Player is the winner" << endl;

   if (result == O)
     cout << "Second Player is the winner" << endl;

   if (result != X && result != O && count == 10)
     cout << "It is a draw" << endl;
  
}

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