(TicTacToe Class) Create a class TicTacToe that will enable you to write a complete program to play the game of tic-tac-toe. The class contains as private data a 3-by-3 two-dimensional array of integers. The constructor should initialize the empty board to all zeros. Allow two human players. Wherever the first player moves, place a X in the specified square. Place an O wherever the second player

moves. Each move must be to an empty square. After each move, determine whether the game has been won or is a draw. If you feel ambitious, modify your program so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop a program that will play three-dimen- sional tic-tac-toe on a 4-by-4-by-4 board. [Caution: This is an extremely challenging project that could take many weeks of effort!]

What will be an ideal response?


```
#ifndef TICTACTOE_H
#define TICTACTOE_H

class TicTacToe
{
private:
enum Status { WIN, DRAW, CONTINUE }; // enumeration constants
int board[ 3 ][ 3 ];
public:
TicTacToe(); // default constructor
void makeMove(); // make move
void printBoard(); // print board
bool validMove( int, int ); // validate move
bool xoMove( int ); // x o move
Status gameStatus(); // game status
}; // end class TicTacToe

#endif
```
```
// Member-function definitions for class TicTacToe.
#include
#include
#include "TicTacToe.h" // include definition of class TicTacToe
using namespace std;

TicTacToe::TicTacToe()
{
for ( int j = 0; j < 3; j++ ) // initialize board

for ( int k = 0; k < 3; k++ )
board[ j ][ k ] = ' ';
} // end TicTacToe constructor

bool TicTacToe::validMove( int r, int c )
{
return r >= 0 && r < 3 && c >= 0 && c < 3 && board[ r ][ c ] == ' ';
} // end function validMove

// must specify that type Status is part of the TicTacToe class.
TicTacToe::Status TicTacToe::gameStatus()
{
int a;

// check for a win on diagonals
if ( board[ 0 ][ 0 ] != ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ] &&
board[ 0 ][ 0 ] == board[ 2 ][ 2 ] )
return WIN;
else if ( board[ 2 ][ 0 ] != ' ' && board[ 2 ][ 0 ] ==
board[ 1 ][ 1 ] && board[ 2 ][ 0 ] == board[ 0 ][ 2 ] )
return WIN;

// check for win in rows
for ( a = 0; a < 3; ++a )

if ( board[ a ][ 0 ] != ' ' && board[ a ][ 0 ] ==
board[ a ][ 1 ] && board[ a ][ 0 ] == board[ a ][ 2 ] )
return WIN;

// check for win in columns
for ( a = 0; a < 3; ++a )

if ( board[ 0 ][ a ] != ' ' && board[ 0 ][ a ] ==
board[ 1 ][ a ] && board[ 0 ][ a ] == board[ 2 ][ a ] )
return WIN;

// check for a completed game
for ( int r = 0; r < 3; ++r )

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

if ( board[ r ][ c ] == ' ' )
return CONTINUE; // game is not finished

return DRAW; // game is a draw
} // end function gameStatus

void TicTacToe::printBoard()
{
cout << " 0 1 2\n\n";

for ( int r = 0; r < 3; ++r )
{
cout << r;

for ( int c = 0; c < 3; ++c )
{
cout << setw( 3 ) << static_cast< char > ( board[ r ][ c ] );

if ( c != 2 )
cout << " |";
} // end for

if ( r != 2 )
cout << "\n ____|____|____\n | | \n";
} // end for

cout << "\n\n";
} // end function printBoard

void TicTacToe::makeMove()
{
printBoard();

while ( true )
{
if ( xoMove( 'X' ) )
break;
else if ( xoMove( 'O' ) )
break;
} // end while structure
} // end function makeMove

bool TicTacToe::xoMove( int symbol )
{
int x;
int y;

do
{
cout << "Player " << static_cast< char >( symbol )
<< " enter move: ";
cin >> x >> y;
cout << '\n';
} while ( !validMove( x, y ) );

board[ x ][ y ] = symbol;
printBoard();
Status xoStatus = gameStatus();

if ( xoStatus == WIN )
{
cout << "Player " << static_cast< char >( symbol ) << " wins!\n";
return true;
} // end if
else if ( xoStatus == DRAW )
{
cout << "Game is a draw.\n";
return true;
} // end else if
else // CONTINUE
return false;
} // end function xoMove
```
```
#include "TicTacToe.h" // include definiton of class TicTacToe

int main()
{
TicTacToe g; // creates object g of class TicTacToe
g.makeMove(); // invokes function makeMove
} // end main
```
![14902|199x698](upload://a05e8rVMFl2GlIFyQhoUqibS6g.png)

Computer Science & Information Technology

You might also like to view...

A JavaFX app’s main class directly inherits from ________.

a. Main b. Application c. App d. Object

Computer Science & Information Technology

As shown in the accompanying figure, ____ is the technique photographers, designers, and artists use to create the illusion of three dimensions on a flat or two-dimensional surface.


a. resolution
b. layout
c. perspective
d. dimensionality

Computer Science & Information Technology

Which of the following statements creates a Slider with a range of 1 to 20 with a starting value of 1?

a. Slider slider = new Slider(0, 20, 1); b. Slider slider = new slider(0.0, 20.0, 1.0); c. Slider slider = new Slider(1.0, 20.0, 1.0); d. Slider slider = new Slider(1.0, 20);

Computer Science & Information Technology

When dealing with large multinational or global organizations, vendors often sell their software programs to the organizations but then outsource the technical support to local, third-party distributors.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology