in C TicTacToe also called Xs and Os Noughts and Crosses and

in C++

Tic-Tac-Toe, also called X\'s and O\'s, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. Lines may be horizontal, vertical, or diagonal.

You will implement a Board class to represent the 3x3 grid. This class will have functions to determine which symbol, if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board to standard output. The board should appear as below:

Implement a Board class with the following functionality:

Default constructor, copy constructor, destructor, assignment operator.

Determine the symbol at the specified grid position.

Change the symbol at the specified grid position.

Determine the winner of the game, if any. You must distinguish between the case where there is no winner because the game is not over yet, and the case where there is no winner because the game ended in a tie.

Create four constants for the four game states and return one of them.

Find a way to pass the position in the grid as a single value. This can be done by creating a record (struct) to store the row and column, or by representing both values using a single integer.

Print the board to standard output.

USE C++ PLEASE

Solution

#include<iostream>
#include<string>

using namespace std;

struct position
{
int i;
int j;
};

class Board
{
private:
char o = \'O\';
char x = \'X\';
string x_win = \"X WIN\";
string y_win = \"Y WIN\";
string draw = \"DRAW\";
string result_pending = \"PENDING\";

public:
char board[3][3];
Board()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = \'\\0\';
}
}
}

// copy constructor
Board(const Board& b)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = b.board[i][j];
}
}
}

char getCharAtPos(struct position p)
{
return board[p.i][p.j];
}

void setCharAtPos(struct position p, char c)
{
board[p.i][p.j] = c;
}

string winner()
{
char win = \'\\0\';
if(board[0][0] == board[1][0] && board[2][0] == board[1][0])
win = board[0][0];
else if (board[0][1] == board[1][1] && board[2][1] == board[1][1])
win = board[0][1];
else if (board[0][2] == board[1][2] && board[2][2] == board[1][2])
win = board[0][2];
else if (board[0][0] == board[0][1] && board[0][2] == board[0][1])
win = board[0][0];
else if (board[1][0] == board[1][1] && board[1][2] == board[1][1])
win = board[1][0];
else if (board[2][0] == board[2][1] && board[2][2] == board[2][1])
win = board[2][0];
else if (board[0][0] == board[1][1] && board[2][2] == board[1][1])
win = board[0][0];
else if (board[0][2] == board[1][1] && board[2][0] == board[1][1])
win = board[0][2];


if (win != \'\\0\')
{
if (win == \'X\')
return x_win;
else
return y_win;
}

for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(board[i][j] == \'\\0\')
{
return result_pending;
}
}
}
return draw;
}
};


int main()
{
Board b;

struct position p;

p.i = 1;
p.j = 1;
b.setCharAtPos(p, \'X\');

p.i = 0;
p.j = 2;
b.setCharAtPos(p, \'O\');

p.i = 0;
p.j = 0;
b.setCharAtPos(p, \'X\');

cout << b.winner() << endl;

p.i = 1;
p.j = 2;
b.setCharAtPos(p, \'O\');

p.i = 2;
p.j = 2;
b.setCharAtPos(p, \'X\');

cout << b.winner() << endl;
}

in C++ Tic-Tac-Toe, also called X\'s and O\'s, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. Lines may be ho
in C++ Tic-Tac-Toe, also called X\'s and O\'s, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. Lines may be ho
in C++ Tic-Tac-Toe, also called X\'s and O\'s, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. Lines may be ho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site