Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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

#include

#include

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

cout

return true;

}

/* One round of a play.

*/

bool play()

{

int grid[GRID_SIZE][GRID_SIZE];

// Take inputs:

for (int row = 1; row

{

int input = -1;

while (input ROW_MAX)

{

cout

cin >> input;

}

// Fill the row with marks:

for (int column = 1; column

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

}

// Output the grid with marks:

cout

print_grid(grid);

cout

// Validy the game play:

if (count(grid, X_MARK)

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

else

{

cout

cout

}

// Ask if want to play once more:

cout

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

cout

for (int row = 1; row

{

cout

for (int column = 1; column

{

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

cout

else cout

}

cout

}

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 #include 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 > 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 > row1; } while (row1 7); b1->row1 = row1;

// ROW2 // ==== do { cout > row2; } while (row2 7); b1->row2 = row2;

// ROW3 // ==== do { cout > row3; } while (row3 7); b1->row3 = row3; };

void display_board(Board *b1) { cout row1) row2) row3)

/** * 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

for (int col = 0; 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions