In: Computer Science
C++please add menu choice on main.cpp with these function below.
output :
Hello! let's
play a game
a.Exit
b.Set Player's Names
c.Play in 2 Player Mode
d.Play in 1 Player Mode (vs. Computer)
//////////////////HEADER //////////////////////
#ifndef MYHEADER_H_
#define MYHEADER_H_
#include<iostream>
#include<iomanip>
#include<cstdlib>
using namespace std;
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
int sum;
void OutputInstruct()
{
cout << "\nPlay the game\n"<<endl;
}
/******************************************************************************
* InitBoard
* This function initializes each spot in the board to a space '
'.
* RETURNS: Board initialized with all spaces
*****************************************************************************/
void InitBoard(char board[][3]) // tic tac toe board – OUT
{
for(int i = 0; i <= NUM_ROWS; i++)
for(int j = 0; j <= 3;
j++)
board[i][j] = '
';
}
/******************************************************************************
* DisplayBoard
* This function outputs the tic tac toe board including the
tokens
* played in the proper format (as described below).
1 2
3
1[1][1]. [1][2]. [1][3]
2[2][1]. [2][2]. [2][3]
3[3][1]. [3][2]. [3][3]
* RETURNS: nothing
* Outputs the current state of the board
*****************************************************************************/
void DisplayBoard(const char board[][3]) // tic tac toe board
{
cout << setw(10) << "1" << setw(8)
<< "2" << setw(9) << "3\n";
for (int i=0; i < 3; i++)
{
cout << setw(7)<< "["
<< i+1 << "][1] | " << "[" << i+1;
cout << "][2] | " <<
"[" << i+1 << "][3]" << endl;
cout << setw(14)<< "|"
<< setw(9) << "|" << endl;
for (int j = 0; j < 3;
j++)
{
switch(j)
{
case 0: cout << i + 1 << setw(9)
<< board[i][j];
cout << setw(4) << "|";
break;
case 1: cout << setw(4) <<
board[i][j];
cout << setw(5) << "|";
break;
case 2: cout << setw(4) <<
board[i][j] << endl;
break;
default: cout <<"ERROR!\n\n";
}
}
cout << setw(14)<< "|"
<< setw(10) << "|\n";
if (i != 2)
{
cout << setw(32) <<
"--------------------------\n";
}
}
cout << endl << endl;
}
/******************************************************************************
* GetPlayers
* This function prompts the user and gets the input for the
players’ names.
* player1 contains the name of the player that is using the X
token.
* player2 contains the name of the player that is using the O
token.
*
* RETURNS: the token opposite of the one in which it
receives.
*****************************************************************************/
void GetPlayers(string& player1, string& player2)
{
cout << "\nEnter the name of player 1(X) :
";
getline (cin, player1);
cout << "Enter the name of player 2(O) :
";
getline (cin, player2);
}
void GetAndCheckInp(char board[][3], char token, string player1,
string player2)
{
int row;
int col;
bool valid;
do
{
if (token == 'X')
{
cout <<
player1 << "'s turn What's your play? : "<<endl;
cout <<
"Row? : "<<endl;
cin >>
row;
cout <<
"Column? : "<<endl;
cin >>
col;
}
else if (token == 'O')
{
cout <<
player2 << "'s turn: What's your play?"<<endl;
cout <<
"Row? : "<<endl;
cin >>
row;
cout <<
"Column? : "<<endl;
cin >>
col;
}
row--;
col--;
if ((row > NUM_ROWS || row <
0) || (col > 3 || col < 0))
{
valid =
false;
cout <<
"Please chose a space on the board." << endl;
}
else if (board[row][col] != '
')
{
valid =
false;
cout <<
"That space is already taken, choose another"<<endl;
}
else
{
valid =
true;
board[row][col]
= token;
sum++;
}
} while (!valid);
}
/******************************************************************************
* SwitchToken
*
* RETURNS: the token opposite of the one in which it
receives.
*****************************************************************************/
char SwitchToken(char token) // current player’s token ('X' or 'O')
- IN
{
if (toupper(token) == 'X')
{
token = 'O';
}
else if (toupper(token) == 'O')
{
token = 'X';
}
return token;
}
/******************************************************************************
* CheckWin
* This function checks to see if either player has won.
* RETURNS the character value of the player that won or T if it was
a tie
* or N if no won
*****************************************************************************/
char CheckWin(const char board[][3]) // tic tac toe board -
IN
{
char win = ' ';
// Checks for three same value horizental
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1]
&& board[i][1] == board[i][2]) return board[i][0];
// Checks for three same value vertical
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i]
&& board[1][i] == board[2][i]) return board[0][i];
// Checks for three same value diagnal
if ((board[0][0] == board[1][1] && board[1][1]
== board[2][2]) || (board[0][2] == board[1][1] &&
board[1][1] == board[2][0]))
return board[1][1];
// Checks if there is any square left
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3;
j++)
if (board[i][j]
== ' ')
return ' ';
// returns draw
return 'T';
}
/******************************************************************************
* OutputWinner
*
* RETURNS: nothing
* Displays the winner’s name
*****************************************************************************/
void OutputWinner(char whoWon, string player1, string
player2)
{
if (whoWon == 'X')
{
cout << player1 << "
has won the game"<<endl;
}
else if (whoWon == 'O')
{
cout << player2 << "
has won the game"<<endl;;
}
else if (whoWon == 'T')
{
cout << player1 << "
and " << player2 << " have tied"<<endl;;
}
}
#endif
>>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>>
#include "myheader.h"
int main()
{
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
// Variables
string player1;
string player2;
GetPlayers(player1, player2);
bool again;
do
{
// Variables
bool won;
char
board[NUM_ROWS][NUM_COLS];
char token;
char input;
won = false;
again = true;
InitBoard(board);
OutputInstruct();
token = 'O';
while(!won)
{
token =
SwitchToken(token);
GetAndCheckInp(board, token, player1, player2);
if
(CheckWin(board) != ' ')
{
won = true;
}
DisplayBoard(board);
}
OutputWinner(CheckWin(board), player1, player2);
//asking user to play again
cout<<"\nPlay again? (Y/N) :
";
cin>>input;
again = toupper(input)=='Y';
} while(again);
return 0;
}
Screenshot
Program
myheader.h
#ifndef MYHEADER_H_
#define MYHEADER_H_
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
int sum;
void OutputInstruct()
{
cout << "\nPlay the game\n" << endl;
}
/******************************************************************************
* InitBoard
* This function initializes each spot in the board to a space '
'.
* RETURNS: Board initialized with all spaces
*****************************************************************************/
void InitBoard(char board[][3]) // tic tac toe board – OUT
{
for (int i = 0; i <= NUM_ROWS; i++)
for (int j = 0; j <= 3;
j++)
board[i][j] = '
';
}
/******************************************************************************
* DisplayBoard
* This function outputs the tic tac toe board including the
tokens
* played in the proper format (as described below).
1
2 3
1[1][1]. [1][2]. [1][3]
2[2][1]. [2][2]. [2][3]
3[3][1]. [3][2]. [3][3]
* RETURNS: nothing
* Outputs the current state of the board
*****************************************************************************/
void DisplayBoard(const char board[][3]) // tic tac toe
board
{
cout << setw(10) << "1" << setw(8)
<< "2" << setw(9) << "3\n";
for (int i = 0; i < 3; i++)
{
cout << setw(7) << "["
<< i + 1 << "][1] | " << "[" << i +
1;
cout << "][2] | " <<
"[" << i + 1 << "][3]" << endl;
cout << setw(14) << "|"
<< setw(9) << "|" << endl;
for (int j = 0; j < 3;
j++)
{
switch (j)
{
case 0: cout
<< i + 1 << setw(9) << board[i][j];
cout << setw(4) << "|";
break;
case 1: cout
<< setw(4) << board[i][j];
cout << setw(5) << "|";
break;
case 2: cout
<< setw(4) << board[i][j] << endl;
break;
default: cout
<< "ERROR!\n\n";
}
}
cout << setw(14) <<
"|" << setw(10) << "|\n";
if (i != 2)
{
cout <<
setw(32) << "--------------------------\n";
}
}
cout << endl << endl;
}
/******************************************************************************
* GetPlayers
* This function prompts the user and gets the input for the
players’ names.
* player1 contains the name of the player that is using the X
token.
* player2 contains the name of the player that is using the O
token.
*
* RETURNS: the token opposite of the one in which it
receives.
*****************************************************************************/
void GetPlayers(string& player1, string& player2)
{
cin.ignore();
cout << "\nEnter the name of player 1(X) :
";
getline(cin, player1);
cout << "Enter the name of player 2(O) :
";
getline(cin, player2);
}
void GetAndCheckInp(char board[][3], char token, string player1,
string player2)
{
int row;
int col;
bool valid;
if (player2 == "Computer") {
do
{
if (token ==
'X')
{
cout << player1 << "'s turn What's
your play? : " << endl;
cout << "Row? : " << endl;
cin >> row;
cout << "Column? : " << endl;
cin >> col;
}
else if
(token == 'O')
{
row = rand() % 3 + 1;
col = rand() % 3 + 1;
}
row--;
col--;
if ((row >
NUM_ROWS || row < 0) || (col > 3 || col < 0))
{
valid = false;
cout << "Please chose a space on the
board." << endl;
}
else if
(board[row][col] != ' ')
{
valid = false;
cout << "That space is already taken,
choose another" << endl;
}
else
{
valid = true;
board[row][col] = token;
sum++;
}
} while (!valid);
}
else {
do
{
if (token ==
'X')
{
cout << player1 << "'s turn What's
your play? : " << endl;
cout << "Row? : " << endl;
cin >> row;
cout << "Column? : " << endl;
cin >> col;
}
else if
(token == 'O')
{
cout << player2 << "'s turn: What's
your play?" << endl;
cout << "Row? : " << endl;
cin >> row;
cout << "Column? : " << endl;
cin >> col;
}
row--;
col--;
if ((row >
NUM_ROWS || row < 0) || (col > 3 || col < 0))
{
valid = false;
cout << "Please chose a space on the
board." << endl;
}
else if
(board[row][col] != ' ')
{
valid = false;
cout << "That space is already taken,
choose another" << endl;
}
else
{
valid = true;
board[row][col] = token;
sum++;
}
} while (!valid);
}
}
/******************************************************************************
* SwitchToken
*
* RETURNS: the token opposite of the one in which it
receives.
*****************************************************************************/
char SwitchToken(char token) // current player’s token ('X' or
'O') - IN
{
if (toupper(token) == 'X')
{
token = 'O';
}
else if (toupper(token) == 'O')
{
token = 'X';
}
return token;
}
/******************************************************************************
* CheckWin
* This function checks to see if either player has won.
* RETURNS the character value of the player that won or T if it was
a tie
* or N if no won
*****************************************************************************/
char CheckWin(const char board[][3]) // tic tac toe board -
IN
{
char win = ' ';
// Checks for three same value horizental
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1]
&& board[i][1] == board[i][2]) return board[i][0];
// Checks for three same value vertical
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i]
&& board[1][i] == board[2][i]) return board[0][i];
// Checks for three same value diagnal
if ((board[0][0] == board[1][1] && board[1][1]
== board[2][2]) || (board[0][2] == board[1][1] &&
board[1][1] == board[2][0]))
return board[1][1];
// Checks if there is any square left
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3;
j++)
if (board[i][j]
== ' ')
return ' ';
// returns draw
return 'T';
}
/******************************************************************************
* OutputWinner
*
* RETURNS: nothing
* Displays the winner’s name
*****************************************************************************/
void OutputWinner(char whoWon, string player1, string
player2)
{
if (whoWon == 'X')
{
cout << player1 << "
has won the game" << endl;
}
else if (whoWon == 'O')
{
cout << player2 << "
has won the game" << endl;;
}
else if (whoWon == 'T')
{
cout << player1 << "
and " << player2 << " have tied" << endl;;
}
}
#endif
main.cpp
#include "myheader.h"
//Function prototypes
char menu();
void twoPlayer(string player1, string player2);
void onePlayer(string player1);
int main()
{
//Row and columns constants
const int NUM_ROWS = 3;
const int NUM_COLS = 3;
// Variables
string player1;
string player2;
char ch = menu();
while (toupper(ch) != 'A') {
if (toupper(ch) == 'B') {
GetPlayers(player1, player2);
}
else if (toupper(ch) == 'C')
{
twoPlayer(player1, player2);
}
else if (toupper(ch) == 'D')
{
onePlayer(player1);
}
cout << endl;
ch = menu();
}
return 0;
}
//Method to get menu return user choice
char menu() {
char ch;
cout<<"Hello!let's play a game\n"
<<"a.Exit\nb.Set Player's
Names\n"
<<"c.Play in 2 Player
Mode\nd.Play in 1 Player Mode(vs.Computer)"<<endl;
cin >> ch;
return ch;
}
//2 player function
void twoPlayer(string player1,string player2) {
// Variables
bool won;
char board[NUM_ROWS][NUM_COLS];
char token;
char input;
won = false;
InitBoard(board);
OutputInstruct();
token = 'O';
while (!won)
{
token = SwitchToken(token);
GetAndCheckInp(board, token,
player1, player2);
if (CheckWin(board) != '
')
{
won =
true;
}
DisplayBoard(board);
}
OutputWinner(CheckWin(board), player1,
player2);
}
//Single player game
void onePlayer(string player1) {
// Variables
bool won;
char board[NUM_ROWS][NUM_COLS];
char token;
char input;
won = false;
InitBoard(board);
OutputInstruct();
token = 'O';
while (!won)
{
token = SwitchToken(token);
GetAndCheckInp(board, token,
player1,"Computer");
if (CheckWin(board) != '
')
{
won =
true;
}
DisplayBoard(board);
}
OutputWinner(CheckWin(board), player1,
"Computer");
}
----------------------------------------------------------------------------------
Output
Hello!let's play a game
a.Exit
b.Set Player's Names
c.Play in 2 Player Mode
d.Play in 1 Player Mode(vs.Computer)
b
Enter the name of player 1(X) : Babila
Enter the name of player 2(O) : Michael
Hello!let's play a game
a.Exit
b.Set Player's Names
c.Play in 2 Player Mode
d.Play in 1 Player Mode(vs.Computer)
c
Play the game
Babila's turn What's your play? :
Row? :
1
Column? :
2
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
Michael's turn: What's your play?
Row? :
2
Column? :
1
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
Babila's turn What's your play? :
Row? :
1
Column? :
3
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
Michael's turn: What's your play?
Row? :
2
Column? :
2
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| O |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
Babila's turn What's your play? :
Row? :
2
Column? :
3
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| O | X
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
Michael's turn: What's your play?
Row? :
3
Column? :
1
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| O | X
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3 O
| |
| |
Babila's turn What's your play? :
Row? :
3
Column? :
3
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2 O
| O | X
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3 O
| | X
| |
Babila has won the game
Hello!let's play a game
a.Exit
b.Set Player's Names
c.Play in 2 Player Mode
d.Play in 1 Player Mode(vs.Computer)
d
Play the game
Babila's turn What's your play? :
Row? :
1
Column? :
2
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| |
| |
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| | O
| |
Babila's turn What's your play? :
Row? :
2
Column? :
2
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| X |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| | O
| |
That space is already taken, choose another
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X |
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| X |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| O | O
| |
Babila's turn What's your play? :
Row? :
3
Column? :
3
That space is already taken, choose another
Babila's turn What's your play? :
Row? :
1
Column? :
3
1
2 3
[1][1] | [1][2] | [1][3]
| |
1
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| X |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| O | O
| |
1
2 3
[1][1] | [1][2] | [1][3]
| |
1 O
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| X |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3
| O | O
| |
Babila's turn What's your play? :
Row? :
3
Column? :
1
1
2 3
[1][1] | [1][2] | [1][3]
| |
1 O
| X | X
| |
--------------------------
[2][1] | [2][2] | [2][3]
| |
2
| X |
| |
--------------------------
[3][1] | [3][2] | [3][3]
| |
3 X
| O | O
| |
Babila has won the game
Hello!let's play a game
a.Exit
b.Set Player's Names
c.Play in 2 Player Mode
d.Play in 1 Player Mode(vs.Computer)
a