Question: C++ (Tic-Tac-Toe) Write a program that allows two players to play tic-tac-toe game. Your program must contain the class ticTacToe to implement a ticTacToe object.Include

C++ (Tic-Tac-Toe) Write a program that allows two players to play tic-tac-toe game. Your program must contain the class ticTacToe to implement a ticTacToe object.Include a 3-by-3 two-dimensional array, as a private member variable, to create the board.If needed, include additional member variables.Some of the operations on a ticTacToe object are printing the current board, getting a move, checking if a move is valid, and determining the winner after each move.Add additional operations as needed.

Have most of the program built having issues with my checkMove function, regardless of row or column input it returns invalid function. here is what I have so far

//headerfile

#include

using namespace std;

class ticTactor

{

public:

ticTactor();

void initialize();

void printBoard();

bool determineWinner();

void getMove();

bool checkMove();

void play();

private:

char g[ 3 ][ 3 ];

int count;

int col, row;

};

ticTactor :: ticTactor()

{

initialize();

}

void ticTactor :: initialize()

{

count = 0;

for ( int i = 0; i < 3; i++ )

for ( int j = 0; j < 3; j++ )

g[ i ][ j ] = '*';

}

void ticTactor::printBoard()

{

cout << " ";

for ( int i = 0; i < 3; i++ )

{

for ( int j = 0; j < 3; j++)

cout << " "<< g[ i ][ j ];

cout << " ";

}

}

void ticTactor::getMove()

{

cout << " \tPlayer" << ((count % 2) + 1 )

<< " \tEnter row : ";

cin >> row;

cout << "\tEnter column : ";

cin >> col;

}

bool ticTactor::checkMove()

{

if ( ( row < 1 || row > 3 ) ||

( col < 1 || col > 3) ||

( g[ row - 1][ col - 1] != '*') )

{

cout << "Invalid Entry";

return false;

}

return true;

}

bool ticTactor::determineWinner()

{

char ch;

if( count % 2 == 0)

ch = 'X';

else

ch = 'O';

if ( checkMove())

{

g[row - 1] [col - 1] = ch;

count++;

}

if (

( (g[0][0]== ch) && (g[0][1]==ch) && (g[0][2]==ch) )||

( (g[1][0]== ch) && (g[1][1]==ch) && (g[1][2]==ch) )||

( (g[2][0]== ch) && (g[2][1]==ch) && (g[2][2]==ch) )||

( (g[0][0]== ch) && (g[1][0]==ch) && (g[2][0]==ch) )||

( (g[0][1]== ch) && (g[1][1]==ch) && (g[2][1]==ch) )||

( (g[0][2]== ch) && (g[1][2]==ch) && (g[2][2]==ch) )||

( (g[0][0]== ch) && (g[1][1]==ch) && (g[2][2]==ch) )||

( (g[0][2]== ch) && (g[1][1]==ch) && (g[2][0]==ch) ) )

return true;

else

if ( count == 9)

{

printBoard();

cout << "n \tReintializing the game";

initialize();

}

return false;

}

void ticTactor::play()

{

initialize();

do

{

getMove();

if ( determineWinner() )

{

cout << " \tPlayer" << (count % 2)

<< "Won the game";

break ;

}

else

printBoard();

} while ( true );

}

//main.cpp

#include

using namespace std;

#include "ticTactor.h"

int main()

{

ticTactor t;

cout << "Tic-Tac-Toe";

t.play();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!