visual studio c++ object oriented programming 1. Deisgn and implement a TicTacTo
ID: 3625325 • Letter: V
Question
visual studio c++object oriented programming
1. Deisgn and implement a TicTacToeGame class that HAS-A relatonship with TicTacToeBoard
a. The TicTacTeGame class should hae the following methods at a minimum
1. winner()---returns () when noone has won, 1 when player1 has won,and 2 when player2 has won, and 3
on a tie
2. instructions()---display the game's instructions
b. The TicTacToeGame class should hve a variable to represent whether itis player one or player two's turn. We
are assuming a two player game.
2. Write a main function that plays the game using the following algorithum:
Create an empty TicTacToeBoard;
Display the game instructions;
Display the board;
While nobody's won andit's not a tie
{
if i'ts player1's turn {
Get player1's move;
If player1's move is valid {
Update the board with player1's move;
}
}
Otherwise
{
Get player2's move;
If player2's moveis valid {
Update the board with player2's move;
}
}
Display the board;
Switch turns;
}
Congradulate the winner or declare a tie;
Explanation / Answer
please rate - thanks
debugged with DEV C++ so you made need to add an include
#include <iostream>
using namespace std;
class TicTacToe
{public:
TicTacToe();
void initializeBoard();
void PrintBoard();/*gets input from user returns character number of location chosen by user*/
void getInput( char ,size_t&,size_t&);/* mark character marker of player on chosen character pos of board*/
bool OccupySquare(size_t,size_t, char );/*checks to see if someone has won returns true or false*/
char CheckForWinner(); /*checks to see if someone won on rows of board*/
bool checkHorizontal( char );/*checks to see if someone won on columns of board*/
bool checkVertical( char );/*checks to see if someone won on diagonal of board*/
bool checkDiagonal( char );/*checks to see if players have tied*/
bool IsScratch();/*prints winner as marker or ties*/
void printWinner(char);/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/
bool IsValidLocation( size_t,size_t);
void InitializeBoard();
static const size_t rows = 3;
static const size_t cols = 3;
private:
char board[rows][cols];
};
int main()
{size_t r,c;
char winner;
TicTacToe a;
while(true)
{
//player 1's turn
a.getInput('X',r,c);
a.PrintBoard();
winner=a.CheckForWinner();
if (winner!=' ') //if player 1 wins break out of loop break
break;
//player 2's turn
a.getInput('O',r,c);
a.PrintBoard();
winner=a.CheckForWinner();
if (winner!=' ') //if player 2 wins break out of loop
break;
}
a.printWinner(winner);
system("pause");
return 0;
}
/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/
TicTacToe::TicTacToe()
{ InitializeBoard();
}
void TicTacToe::InitializeBoard()
{for (int i = 0; i<rows; i++)
{ for (int j = 0; j<cols; j++)
{
board[i][j]= ' ';
}
}
}
void TicTacToe::PrintBoard()
{
int t;
for(t=0; t<3; t++) {
cout<<" "<<board[t][0]<<" | "<< board[t][1]<<" | "<< board[t][2];
if(t!=2) cout<<" ---|---|--- ";
}
cout<<" ";
}
char TicTacToe:: CheckForWinner()
{
if (checkHorizontal('X'))
{
return 'X';
}
if (checkVertical( 'X'))
{
return 'X';
}
if (checkDiagonal( 'X'))
{
return 'X';
}
if (checkHorizontal('O'))
{
return 'O';
}
if (checkVertical( 'O'))
{
return 'O';
}
if (checkDiagonal( 'O'))
{
return 'O';
}
if (IsScratch())
{
return 'T';
}
return ' ';
}
/*checks to see if someone won on rows of board*/
/*checks to see if someone won on rows of board*/
bool TicTacToe::checkHorizontal( char marker)
{int i,j,count;
for(i=0; i<rows; i++)
{count=0;
for(j=0;j<cols;j++)
if(board[i][j]==marker)
count++;
if(count==3)
return true;
}
return false;
}
/*checks to see if someone won on columns of board*/
bool TicTacToe::checkVertical( char marker)
{int i,j,count;
for(i=0; i<rows; i++)
{count=0;
for(j=0;j<cols;j++)
if(board[j][i]==marker)
count++;
if(count==3)
return true;
}
return false;
}
/*checks to see if someone won on diagonal of board*/
bool TicTacToe::checkDiagonal( char marker)
{if(board[0][0]==board[1][1] && board[1][1]==board[2][2]&& board[0][0]==marker)
return true;
if(board[0][2]==board[1][1] && board[1][1]==board[2][0]&& board[0][2]==marker)
return true;
return false;
}
/*checks to see if players have tied*/
bool TicTacToe::IsScratch()
{int i,j;
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
if(board[i][j]==' ')
return false;
return true;
}
/*prints winner as marker or ties*/
void TicTacToe::printWinner(char marker)
{ if(marker=='T')
cout<<"TIE GAME! ";
else
cout<<"The winner is "<<marker<<"!!! ";
}
void TicTacToe::getInput(char marker,size_t &r,size_t &c)
{
for(;;)
{cout<<"Player "<<marker<<endl;
cout << "Enter move row(1-3): " ;
cin >> r;
cout << "Enter move column(1-3): " ;
cin >> c;
if (OccupySquare(r,c,marker) )
{
return ;
}
cout << "Invalid Move: Please Try Again ";
}
}
bool TicTacToe::IsValidLocation(size_t r,size_t c)
{
if(r<1||r>rows||c<0||c>cols)
return false;
if(board[r-1][c-1]!=' ')
return false;
else
return true;
}
bool TicTacToe::OccupySquare( size_t r,size_t c, char marker)
{if(IsValidLocation( r,c))
{board[r-1][c-1]=marker;
return true;
}
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.