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();
}
Solution:
Here is the complete implemented code with main() function.
Code:
#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()
{
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
boardConfig[i][j]=' ';
}
}
}
//put an 'X' character at the given location
bool placeX(int x, int y)
{
boardConfig[x][y] ='X';
}
//put an 'O' character at the given location
bool placeO(int x, int y)
{
boardConfig[x][y] ='O';
}
//set all positions to character ' '.
void clear()
{
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;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()
{
//Checking rows for X
if(boardConfig[0][0] == 'X' && boardConfig[0][1] == 'X' && boardConfig[0][3] == 'X' )
return true;
if(boardConfig[1][0] == 'X' && boardConfig[1][1] == 'X' && boardConfig[1][3] == 'X' )
return true;
if(boardConfig[2][0] == 'X' && boardConfig[2][1] == 'X' && boardConfig[2][3] == 'X' )
return true;
//Checking columns for X
if(boardConfig[0][0] == 'X' && boardConfig[1][0] == 'X' && boardConfig[2][0] == 'X' )
return true;
if(boardConfig[0][1] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][1] == 'X' )
return true;
if(boardConfig[0][2] == 'X' && boardConfig[1][2] == 'X' && boardConfig[2][2] == 'X' )
return true;
//Checking diagonals for X
if(boardConfig[0][0] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][2] == 'X' )
return true;
if(boardConfig[0][2] == 'X' && boardConfig[1][1] == 'X' && boardConfig[2][0] == '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()
{
//Checking rows for O
if(boardConfig[0][0] == 'O' && boardConfig[0][1] == 'O' && boardConfig[0][3] == 'O' )
return true;
if(boardConfig[1][0] == 'O' && boardConfig[1][1] == 'O' && boardConfig[1][3] == 'O' )
return true;
if(boardConfig[2][0] == 'O' && boardConfig[2][1] == 'O' && boardConfig[2][3] == 'O' )
return true;
//Checking columns for X
if(boardConfig[0][0] == 'O' && boardConfig[1][0] == 'O' && boardConfig[2][0] == 'O' )
return true;
if(boardConfig[0][1] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][1] == 'O' )
return true;
if(boardConfig[0][2] == 'O' && boardConfig[1][2] == 'O' && boardConfig[2][2] == 'O' )
return true;
//Checking diagonals for X
if(boardConfig[0][0] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][2] == 'O' )
return true;
if(boardConfig[0][2] == 'O' && boardConfig[1][1] == 'O' && boardConfig[2][0] == '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()
{
//Checking rows
if(boardConfig[0][0] == boardConfig[0][1] && boardConfig[0][1] == boardConfig[0][3] && boardConfig[0][0]!=' ')
return true;
if(boardConfig[1][0] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[1][3] && boardConfig[1][0]!=' ')
return true;
if(boardConfig[2][0] == boardConfig[2][1] && boardConfig[2][1] == boardConfig[2][3] && boardConfig[2][0]!=' ')
return true;
//Checking columns for X
if(boardConfig[0][0] == boardConfig[1][0] && boardConfig[1][0] == boardConfig[2][0] && boardConfig[0][0]!=' ')
return true;
if(boardConfig[0][1] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][1] && boardConfig[0][1]!=' ')
return true;
if(boardConfig[0][2] == boardConfig[1][2] && boardConfig[1][2] == boardConfig[2][2] && boardConfig[0][2]!=' ')
return true;
//Checking diagonals for X
if(boardConfig[0][0] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][2] && boardConfig[0][0]!=' ')
return true;
if(boardConfig[0][2] == boardConfig[1][1] && boardConfig[1][1] == boardConfig[2][0] && boardConfig[0][2]!=' ')
return true;
return false;
}
// cout a nice looking picture of the board configuration
void display()
{
cout << "-------------" << endl;
int i,j;
for (i = 0; i < 3; i++)
{
cout << "| ";
for (j = 0; j < 3; j++)
{
cout << boardConfig[i][j] << " | ";
}
cout << endl;
cout << "-------------" << endl;
}
}
};
//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();
}
Output obtained:
