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

C++ problems 1 0 1 0 0 0 Binary Match Representation Representation We then assi

ID: 3844398 • Letter: C

Question

C++ problems

1 0 1 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 5 Row 2 Row 3 o o o 0 0 0 Binary Match Representation Representation We have Row 1: The row containing the binary representation 101. Row 2: he row containing the binary representation e11. Row 3: The row containing the binary representation eee. You might have noticed that our Tic-Tac-Toe uses three bits per row to represent a match. This means that each row can be represented by an integer number between 0 and 7 The picture below shows all of these numbers and their binary representation. 0 0 1 0 1 0 Each of the rows above can be easily represented with a variable that holds an integer number. An appropriate data type for this task would be unsigned short or unsigned int A variable of type unsigned short can hold up to 2 bytes of information. A variable of type unsigned int can hold up to 4 bytes of information. Because we only need three bits per row we can use three unsigned short variables.

Explanation / Answer

#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;
   }
};

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