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

I ask help with this c++ problem multiple times, but I got soluations that didn\

ID: 3846609 • Letter: I

Question

I ask help with this c++ problem multiple times, but I got soluations that didn't fillfull the requirements. Please MAKE SURE to use bitwise operators & read carefully the requirements

These two codes work, but the exeprts didn't include the requirements that should have been included. So please once again, read the requirements carefully

#include <cstdlib>

#include <iostream>

#include <string>

using namespace std;

#define GRID_SIZE   3

#define INVALID       -2

#define TIE           -1

#define O_MARK       0

#define X_MARK       1

#define ROW_MIN       0

#define ROW_MAX       7

bool not_valid();

bool play();

int extract_column(int row, int column);

void print_grid(int grid[][GRID_SIZE]);

int same_row(int grid[][GRID_SIZE], int row);

int same_column(int grid[][GRID_SIZE], int col);

int same_diagonal1(int grid[][GRID_SIZE]);

int same_diagonal2(int grid[][GRID_SIZE]);

int count(int grid[][GRID_SIZE], int mark);

/* Main function, it starts the program */

int main(int argc, char* argv[])

{

   bool play_more = true;

   while (play_more)

       play_more = play(); // Play until user decides to not continue.

   return 0;

}

/* Shows that the game was not valid and return true.

*/

bool not_valid()

{

   cout << "Not a valid Tic-Tac-Toe game!" << endl;

   cout << "Play again." << endl << endl;

   return true;

}

/* One round of a play.

*/

bool play()

{

   int grid[GRID_SIZE][GRID_SIZE];

   // Take inputs:

   for (int row = 1; row <= GRID_SIZE; row++)

   {

       int input = -1;

       while (input < ROW_MIN || input > ROW_MAX)

       {

           cout << "Input Row #" << row << " (Values between 0-7): ";

           cin >> input;

       }

       // Fill the row with marks:

       for (int column = 1; column <= GRID_SIZE; column++)

           grid[row - 1][column - 1] = extract_column(input, column);

   }

   // Output the grid with marks:

   cout << endl << "The following 3x3 Tic-Tac-Toe was given:" << endl << endl;

   print_grid(grid);

   cout << endl;

   // Validy the game play:

   if (count(grid, X_MARK) < (GRID_SIZE * GRID_SIZE / 2) || count(grid, O_MARK) < (GRID_SIZE * GRID_SIZE / 2))

       return not_valid(); // Play once more

   string output;

   int won = TIE;

   // Checks rows

   int res = same_row(grid, 1); // 1st Row

   if (res != TIE)

   {

       won = res;

       output = "First Row Same";

   }

   res = same_row(grid, 2); // 2nd Row

   if (res != TIE)

   {

       if (won != res)

           return not_valid();

       won = res;

       output = "Second Row Same";

   }

   res = same_row(grid, 3); // 3rd Row

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       output = "Third Row Same";

   }

   // Check columns

   res = same_column(grid, 1); // 1st Column

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       if (output == "")

           output = "First Column Same";

   }

   res = same_column(grid, 2); // 2nd Column

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       if (output == "")

           output = "Second Column Same";

   }

   res = same_column(grid, 3); // 3rd Column

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       if (output == "")

           output = "Third Column Same";

   }

   // Check diagonals

   res = same_diagonal1(grid); // 1st Diagonal

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       if (output == "")

           output = "Diagonal left up corner to right bottom corner Same";

   }

   res = same_diagonal2(grid); // 2nd Diagonal

   if (res != TIE)

   {

       if (won != TIE && won != res)

           return not_valid();

       won = res;

       if (output == "")

           output = "Diagonal right up corner to left bottom corner Same";

   }

   // Output result:

   if (won == TIE)

       cout << "It's a tie!" << endl << endl;

   else

   {

       cout << endl << output << endl;

       cout << "Who won?: " << (won == X_MARK ? "X" : "O") << endl << endl;

   }

   // Ask if want to play once more:

   cout << "Would You like to play again? (Y/N): ";

   cin >> output;

   if (output == "Y" || output == "y")

       return true;

   return false;

}

/* Extracts mark (X or O) from the row input.

* Arguments:

* row - Input, integer from 0 to 7 inclusively.

* column - Column number, either 1 or 2 or 3.

* Returns:

* Mark in column specified, X or O.

*/

int extract_column(int row, int column)

{

   return ((row >> (GRID_SIZE - column)) & 1);

}

/* Prints Tic Tac Toe board with marks.

* Arguments:

* grid - Board.

*/

void print_grid(int grid[][GRID_SIZE])

{

   cout << " C1 C2 C3" << endl;

   cout << " +---+---+---+" << endl;

   for (int row = 1; row <= GRID_SIZE; row++)

   {

       cout << "R" << row << " |";

       for (int column = 1; column <= GRID_SIZE; column++)

       {

           if (grid[row-1][column-1] == O_MARK)

               cout << " O |";

           else cout << " X |";

       }

       cout << endl << " +---+---+---+" << endl;

   }

   return;

}

/* Checks if the row specified in the grid is the same.

* Arguments:

* grid - The board.

* row - The row number, integer either 1 or 2 or 3.

* Returns:

* O if three Os in a row, X if three Xs in a row, -1 otherwise.

*/

int same_row(int grid[][GRID_SIZE], int row)

{

   if (grid[row - 1][0] == grid[row - 1][1] && grid[row - 1][0] == grid[row - 1][2])

       return grid[row - 1][0];

   return TIE;

}

/* Checks if the column specified in the grid is the same.

* Arguments:

* grid - The board.

* col - The column number, integer either 1 or 2 or 3.

* Returns:

* O if three Os in a column, X if three Xs in a column, -1 otherwise.

*/

int same_column(int grid[][GRID_SIZE], int col)

{

   if (grid[0][col - 1] == grid[1][col - 1] && grid[0][col - 1] == grid[2][col - 1])

       return grid[0][col - 1];

   return TIE;

}

/* Checks if the diagonal from left upper conner is the same.

* Arguments:

* grid - The board.

* Returns:

* O if three Os in a diagonal, X if three Xs in a diagonal, -1 otherwise.

*/

int same_diagonal1(int grid[][GRID_SIZE])

{

   if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2])

       return grid[1][1];

   return TIE;

}

The oder code is

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


struct Board
{
   unsigned short row1;
   unsigned short row2;
   unsigned short row3;
};


void get_rows(Board *b1);
void display_board(Board *b1);
string return_row(unsigned short row);
bool check_validity(Board *b1);
int num_ones(unsigned short row);


int main()
{
   bool isInValid{};
   char playAgain{};
   Board *b1 = new Board{};
   do
   {
       get_rows(b1);
       display_board(b1);
       isInValid = check_validity(b1);
       if (isInValid)
           cerr << "Not a valid Tic-Tac-Toe game! Play again. " << endl;
       else
       {
           cout << "Would you like to play again? (Y / N): ";
           cin >> playAgain;
       }
   }
   while ( isInValid || playAgain == 'Y' || playAgain == 'y' );
  
   return 0;
}


/**
* Gets the input from user and saves data to the board.
*/
void get_rows(Board *b1)
{
   unsigned short row1, row2, row3;

   // ROW1
   // ====
   do
   {
       cout << "Input Row #1 (Values between 0-7): ";
       cin >> row1;
   }
   while (row1 < 0 || row1 > 7);
   b1->row1 = row1;

   // ROW2
   // ====
   do
   {
       cout << "Input Row #2 (Values between 0-7): ";
       cin >> row2;
   }
   while (row2 < 0 || row2 > 7);  
   b1->row2 = row2;

   // ROW3
   // ====
   do
   {
       cout << "Input Row #3 (Values between 0-7): ";
       cin >> row3;
   }
   while (row3 < 0 || row3 > 7);  
   b1->row3 = row3;
};


void display_board(Board *b1)
{
   cout << "     C1   C2 C3 " << endl;
   cout << "    +---+---+---+" << endl;
   cout << " R1 " << return_row(b1->row1) << endl;
   cout << "    +---+---+---+" << endl;
   cout << " R2 " << return_row(b1->row2) << endl;
   cout << "    +---+---+---+" << endl;
   cout << " R3 " << return_row(b1->row3) << endl;
   cout << "    +---+---+---+" << endl;
};


/**
* Returns a string respective to the given input.
*/
string return_row(unsigned short row)
{
   switch(row)
   {
       case 0:
           return "| O | O | O |";
       case 1:
           return "| O | O | X |";
       case 2:
           return "| O | X | 0 |";
       case 3:
           return "| O | X | X |";
       case 4:
           return "| X | O | 0 |";
       case 5:
           return "| X | O | X |";
       case 6:
           return "| X | X | 0 |";
       case 7:
           return "| X | X | X |";
   };
};


bool check_validity(Board *b1)
{
   int tot = num_ones(b1->row1) + num_ones(b1->row2) + num_ones(b1->row3);
   return tot != 5 && tot != 4;
};


int num_ones(unsigned short row)
{
   switch (row) {
       case 0:
           return 0;
       case 1:
           return 1;
       case 2:
           return 1;
       case 3:
           return 2;
       case 4:
           return 1;
       case 5:
           return 2;
       case 6:
           return 2;
       case 7:
           return 3;
   }
};

/* Checks if the diagonal from right upper conner is the same.

* Arguments:

* grid - The board.

* Returns:

* O if three Os in a diagonal, X if three Xs in a diagonal, -1 otherwise.

*/

int same_diagonal2(int grid[][GRID_SIZE])

{

   if (grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0])

       return grid[1][1];

   return TIE;

}

/* Counts number of marks in the grid.

* Arguments:

* grid - The board.

* mark - The mark, either X (1) or O (0).

* Returns:

* The number of that mark in the grid.

*/

int count(int grid[][GRID_SIZE], int mark)

{

   int i = 0;

   for (int row = 0; row < GRID_SIZE; row++)

       for (int col = 0; col < GRID_SIZE; col++)

           if (grid[row][col] == mark)

               i++;

   return i;

}

Make use of control and repetition structures for input validation. Make use of bitwise operators to handle the underlying bits in a variable. Apply formatting rules to console output. Replicate the gameplay of the Tic-Tac oe game Introduction to Tic-Tac-Toe Tic-Tac-Toe is a kid's game consisting of a 3x3 grid and two players. The players, X and O, take turns marking a spot in the grid. The player who places three marks in a horizontal, vertical, or diagonal row wins the game. It is also possible for the game to end in a tie. See an example below. If you wish to play the game click this link. Lab Task Your task in this session is to write a program that takes a Tic-Tac-Toe game and determines who won. Let's assume we have the match below. From the left grid it is clear that player O won the match. The grid on the right is the match's binary representation. We are using a one to represent X's marks and a zero to represent O's marks. o o o 0 0 0 Binary Match Representation Representation We then assign a label to each row and column to make it easier to locate where the players' marks Row 1 X X 1 0 1 Row 2 Row 3 o o o 0 0 0 Binary Match Representation Representation

Explanation / Answer

#include <iostream>
using namespace std;
class Tic
{
public:
Tic();
int plmov(int i);
void nxtpl();
void winchk();
void Drawbd();
private:
int bd[3][3];
int turn; // pl1 == 1, pl2 == 2
int gmover;
void plgm();
};

Tic::Tic()
{
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
bd[i][j] = 0;// 0 means empty
turn = 1; // pl1
gmover = 0;
Drawbd();
plgm();
}
int Tic::plmov(int i)
{
int x = (i - 1)/3;
int y = ((i + 2) % 3);
int returnVal = bd[x][y];
if (returnVal == 0)
{
bd[x][y] = turn;
winchk();
if (!gmover)
nxtpl();
}
else
cout << "Invalid move, try again. ";
Drawbd();
return returnVal;
}

void Tic::nxtpl()
{
if (turn == 1)
turn = 2;
else
turn = 1;
}

void Tic::winchk()
{
if ((bd[0][0] == turn) && (bd[1][0] == turn) && (bd[2][0] == turn))
gmover = turn;
else
if ((bd[0][1] == turn) && (bd[1][1] == turn) && (bd[2][1] == turn))
gmover = turn;
else
if ((bd[0][2] == turn) && (bd[1][2] == turn) && (bd[2][2] == turn))
gmover = turn;
else
if ((bd[0][0] == turn) && (bd[0][1] == turn) && (bd[0][2] == turn))
gmover = turn;
else
if ((bd[1][0] == turn) && (bd[1][1] == turn) && (bd[1][2] == turn))
gmover = turn;
else
if ((bd[2][0] == turn) && (bd[2][1] == turn) && (bd[2][2] == turn))
gmover = turn;
else
if ((bd[0][0] == turn) && (bd[1][1] == turn) && (bd[2][2] == turn))
gmover = turn;
else
if ((bd[0][2] == turn) && (bd[1][1] == turn) && (bd[2][0] == turn))
gmover = turn;
}

void Tic::plgm()
{
int i;
while (gmover!=turn)
{
//Drawbd();
cout << "Player[" << turn << "] Please enter move: ";
cin >> i;
plmov(i);
}
cout << "Player[" << turn << "] Wins!" << endl;
}

void Tic::Drawbd()
{
int temp[9];
int k = 0;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if (bd[i][j] == 0)
temp[k] = k+49;
else
{
if (bd[i][j] == 1)
temp[k] = 88;
else
temp[k] = 79;
}
k++;
}
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[0] << " | " << (char)temp[1] << " | " << (char)temp[2] << " | ";
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[3] << " | " << (char)temp[4] << " | " << (char)temp[5] << " | ";
cout << "+---+---+---+ ";
cout <<"| " << (char)temp[6] << " | " << (char)temp[7] << " | " << (char)temp[8] << " | ";
cout << "+---+---+---+ ";
}
int main()
{
Tic Game;
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