3. Tic-Tac-Toe You are to implement the classic Tic-Tac-Toe game. Your implement
ID: 3626686 • Letter: 3
Question
3. Tic-Tac-Toe
You are to implement the classic Tic-Tac-Toe game. Your implementation should consist of the following functions:
// Prints the Game Board with all pieces
void printBoard(char[][3] board);
// Marks the board at the specified location with the 'piece' specified.
void markBoard(char[][3] board, int row, int col, char piece);
// Returns 'X' if player X has won,
// 'O' if player O has won,
// 'D' if the game is a draw, and
// 'C' if the game can continue.
char findWinner(char[][3]);
Your program should randomly choose a player to begin the game.
Your application should print the board, and then prompt the current player for their move.
If a player specifies an invalid location you should prompt them again for a valid move.
Below is a partial, sample run:
| |
---------
| |
---------
| |
Player X, Enter a Row and Column (between 1-3): 1 2
| X |
---------
| |
---------
| |
Player O, Enter a Row and Column (between 1-3): 1 2
!!!Invalid Move!!!
| X |
---------
| |
---------
| |
Player O, Enter a Row and Column (between 1-3): 2 2
| X |
---------
| O |
---------
| |
Player X, Enter a Row and Column (between 1-3):
When a game is over (a player has won or there is a draw), your program should display a message announcing the winner.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void initializeBoard(char board[][3]);
void instructions();
void displayBoard(char board[][3]);/*gets input from user returns character number of location chosen by user*/
void getInput(char board[][3], char marker,int& x,int& y);/* mark character marker of player on chosen character pos of board*/
void markBoard(char board[][3],int x,int y, char marker);/*checks to see if someone has won returns true or false*/
bool gameOver(char board[][3]); /*checks to see if someone won on rows of board*/
bool checkHorizontal(char board[][3], char marker);/*checks to see if someone won on columns of board*/
bool checkVertical(char board[][3], char marker);/*checks to see if someone won on diagonal of board*/
bool checkDiagonal(char board[][3], char marker);/*checks to see if players have tied*/
bool checkTie(char board[][3]);/*prints winner as marker or ties*/
void printWinner(char marker);/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/
bool validMove(char board[][3], int x,int y);
int main()
{ srand(time(0));
int start=rand()%2;
char board[3][3];
char exit;
char f,s;
int x,y;
if(start==0)
{f='X';
s='O';
}
else
{f='O';
s='X';
}
do
{instructions();
initializeBoard(board);
displayBoard(board);
while(true)
{
//player 1's turn
getInput(board,f,x,y);
markBoard(board, x,y, f);
displayBoard(board);
if (gameOver(board)) //if player 1 wins break out of loop break
break;
//player 2's turn
getInput(board,s,x,y);
markBoard(board, x,y, s);
displayBoard(board);
if (gameOver(board)) //if player 2 wins break out of loop
break;
}
cout << "Would you like to play again (Y or N): ";
cin >> exit;
}while((exit != 'N') && (exit != 'n'));
return 0;
}
void initializeBoard(char board[][3])
{
for (int i = 0; i<3; i++)
{ for (int j = 0; j<3; j++)
{
board[i][j]= ' ';
}
}
}
/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/
bool validMove(char board[][3], int x, int y)
{if(x<0||x>2||y<0||y>2)
return false;
if(board[x][y]=='X'||board[x][y]=='O')
return false;
else
return true;
}
void instructions()
{cout<<"Welcome to Tic-Tac-Toe. ";
}
void displayBoard(char board[][3])
{ int t;
for(t=0; t<3; t++) {
cout<<" "<<board[t][0]<<" | "<< board[t][1]<<" | "<< board[t][2];
if(t!=2) cout<<" ---|---|--- ";
}
cout<<" ";
}
/*gets input from user returns character number of location chosen by user*/
void getInput(char board[][3], char marker,int& x,int& y)
{
for(;;)
{cout << "Player "<< marker << " Enter a Row and Column (between 1-3): ";
cin >> x>>y;
x--;
y--;
if (validMove(board,x,y))
return;
cout << "Invalid Move: Please Try Again ";
}
}
/*checks to see if someone has won returns true or false*/
void markBoard(char board[][3], int x,int y, char marker)
{
board[x][y]=marker;
}
/*checks to see if someone won on rows of board*/
bool gameOver(char board[][3])
{
if (checkHorizontal(board,'X'))
{
printWinner('X'); return true;
}
if (checkVertical(board, 'X'))
{
printWinner('X');
return true;
}
if (checkDiagonal(board, 'X'))
{
printWinner('X');
return true;
}
if (checkHorizontal(board,'O'))
{
printWinner('O');
return true;
}
if (checkVertical(board, 'O'))
{
printWinner('O');
return true;
}
if (checkDiagonal(board, 'O'))
{
printWinner('O');
return true;
}
if (checkTie(board))
{
printWinner('T');
return true;
}
return false;
}
/*checks to see if someone won on rows of board*/
bool checkHorizontal(char board[][3], char marker)
{int i,j,count;
for(i=0; i<3; i++)
{count=0;
for(j=0;j<3;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 checkVertical(char board[][3], char marker)
{int i,j,count;
for(i=0; i<3; i++)
{count=0;
for(j=0;j<3;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 checkDiagonal(char board[][3], 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 checkTie(char board[][3])
{int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(board[i][j]==' ')
return false;
return true;
}
/*prints winner as marker or ties*/
void printWinner(char marker)
{ if(marker=='T')
cout<<"TIE GAME! ";
else
cout<<"The winner is "<<marker<<"!!! ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.