In: Computer Science
c++
//Tic tac toe lab.  Topics:  2D arrays, classes.
// Part 1
//Implement the following specifications for a tic tac toe game object:
class tictactoeGame
{
    public:
        char boardConfig[3][3];   // two dimensional array stores current board configuration    
   
        // Constructor:
        // set  boardConfig[i][j] to be ' ' (the space character)
        // for 0<= i <= 2, 0<= j <= 2   
        tictactoeGame()
        {
                //fill this in
        }   
        //put an 'X' character at the given location          
        bool placeX(int x, int y)
        {
                //fill this in
        }
        //put an 'O' character at the given location         
        bool placeO(int x, int y)
        {
                //fill this in
        }
        //set all positions to character ' '.
        void clear()
        {
                //fill this in
        }
        // Return true if there are 3 'X' marks placed in a single
        // column, row, or diagnol.  Return false otherwise.
        bool xWins()
        {
                //fill this in
        }
        // Return true if there are 3 'O' marks placed in a single
        // column, row, or diagnol.  Return false otherwise.      
        bool oWins()
        {
                //fill this in
        }
        // Return true if there are either 3 'X' marks or 3 'O' marks
        // placed in a single column, row, or diagnol, or if the board is full.
        // Return false otherwise.
        bool gameOver()
        {
                //fill this in
        }
        // cout a nice looking picture of the board configuration 
        void display()
        {
                //fill this in
        }
};
//Here is an example main program to give an idea how the above class is supposed to work.
int main()
{
    tictactoeGame mygame;
    
    mygame.placeX(1,1);
    mygame.placeO(2,1);
    mygame.placeX(0,2);
    mygame.placeO(2,2);
    
    mygame.display();
}
// Part 2 Once you have implemented the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players (you and a friend) take turns placing X’s and O’s onto the board. After each turn, the current board configuration should be displayed, and once a player connects three of a kind, the game should end declaring that player the winner.
// C++ program to simulate TicTacToe between 2 players
#include <iostream>
using namespace std;
class tictactoeGame
{
public:
char boardConfig[3][3]; // two dimensional array stores current board configuration
// Constructor:
// set boardConfig[i][j] to be ' ' (the space character)
// for 0<= i <= 2, 0<= j <= 2
tictactoeGame()
{
clear();
}
//put an 'X' character at the given location
bool placeX(int x, int y)
{
if(x >=0 && x < 3 && y >= 0 && y
< 3 && boardConfig[x][y] == ' ')
{
boardConfig[x][y] = 'X';
return true;
}
return false;
}
//put an 'O' character at the given location
bool placeO(int x, int y)
{
if(x >=0 && x < 3 && y >= 0 && y
< 3 && boardConfig[x][y] == ' ')
{
boardConfig[x][y] = 'O';
return true;
}
return false;
}
//set all positions to character ' '.
void clear()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
boardConfig[i][j]= ' ';
}
// Return true if there are 3 'X' marks placed in a single
// column, row, or diagnol. Return false otherwise.
bool xWins()
{
// loop to in rows and columns
for(int i=0;i<3;i++)
{
// search in rows
if(boardConfig[i][0] == boardConfig[i][1] &&
boardConfig[i][0] == boardConfig[i][2] && boardConfig[i][0]
== 'X')
return true;
// search in columns
else if(boardConfig[0][i] == boardConfig[1][i] &&
boardConfig[0][i] == boardConfig[2][i] && boardConfig[0][i]
== 'X')
return true;
}
// search in main diagonal
if(boardConfig[0][0] == boardConfig[1][1] &&
boardConfig[1][1] == boardConfig[2][2] && boardConfig[1][1]
== 'X')
return true;
// search in other diagonal
if(boardConfig[0][2] == boardConfig[1][1] &&
boardConfig[1][1] == boardConfig[2][0] && boardConfig[1][1]
== 'X')
return true;
return false;
}
// Return true if there are 3 'O' marks placed in a single
// column, row, or diagnol. Return false otherwise.
bool oWins()
{
for(int i=0;i<3;i++)
{
if(boardConfig[i][0] == boardConfig[i][1] &&
boardConfig[i][0] == boardConfig[i][2] && boardConfig[i][0]
== 'O')
return true;
else if(boardConfig[0][i] == boardConfig[1][i] &&
boardConfig[0][i] == boardConfig[2][i] && boardConfig[0][i]
== 'O')
return true;
}
if(boardConfig[0][0] == boardConfig[1][1] &&
boardConfig[1][1] == boardConfig[2][2] && boardConfig[1][1]
== 'O')
return true;
if(boardConfig[0][2] == boardConfig[1][1] &&
boardConfig[1][1] == boardConfig[2][0] && boardConfig[1][1]
== 'O')
return true;
return false;
}
// Return true if there are either 3 'X' marks or 3 'O'
marks
// placed in a single column, row, or diagnol, or if the board is
full.
// Return false otherwise.
bool gameOver()
{
// x or o is a winner
if(xWins() || oWins())
return true;
// loop to check if all locations are occupied
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
if(boardConfig[i][j] == ' ')
return false;
}
return true;
}
// cout a nice looking picture of the board configuration
void display()
{
cout<<"Board:"<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<boardConfig[i][j]<<" | ";
}
if(i < 2)
cout<<endl<<string(15,'-')<<endl;
else
cout<<endl;
}
cout<<endl;
}
};
int main()
{
tictactoeGame mygame;
bool playerX = true;
int row, col;
// loop that continues until the game is over
while(!mygame.gameOver())
{
mygame.display(); // display the board
// player X turn
if(playerX)
{
cout<<"Player X, enter the row(1-3) and column(1-3) for the
location: ";
cin>>row>>col;
while(!mygame.placeX(row-1,col-1))
{
cout<<"Invalid location."<<endl;
cout<<"Player X, enter the row(1-3) and column(1-3) for the
location: ";
cin>>row>>col;
}
}
else // player O turn
{
cout<<"Player O, enter the row(1-3) and column(1-3) for the
location: ";
cin>>row>>col;
while(!mygame.placeO(row-1,col-1))
{
cout<<"Invalid location."<<endl;
cout<<"Player O, enter the row(1-3) and column(1-3) for the
location: ";
cin>>row>>col;
}
}
playerX = !playerX; // change the player
}
// display the final board
mygame.display();
// determine and display the result
if(mygame.oWins())
cout<<"Player O wins!!!"<<endl;
else if(mygame.xWins())
cout<<"Player X wins!!!"<<endl;
else
cout<<"It's a tie"<<endl;
}
//end of program
Output:

