How can a place a loop in this program that asks the user if they want to play a
ID: 3691836 • Letter: H
Question
How can a place a loop in this program that asks the user if they want to play again.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
const int MYROWS = 3;
const int MYCOLS = 3;
char ttt[3][3] = //array ttt
{
{ '*','*','*' },
{ '*','*','*' },
{ '*','*','*' }
};
int movesTotal = 0;
void MyTicTacToe() //function MyTicTacToe
{
cout << "+-------+" << endl;
for (int rows = 0; rows < MYROWS; rows++)
{
cout << "|";
for (int cols = 0; cols < MYCOLS; cols++)
{
cout << " " << ttt[rows][cols];
}
cout << " |" << endl;
}
cout << "+-------+" << endl;
}
void PlayerMove(int playerNumber) //function for Players move
{
int rows = -1, cols = -1;
string lines;
stringstream myss;
for (;;)
{
cout << "Player #" << playerNumber << " -> (row column) : ";
getline(cin, lines, ' ');
myss.clear();
myss.str(lines);
myss >> rows >> cols;
if (rows < 1 || rows > 3 || cols < 1 || cols > 3)
{
cout << "Move Invalid!" << endl;
}
else if (ttt[rows - 1][cols - 1] != '*')
{
cout << "Place is taken! Try again!!" << endl;
}
else
{
break;
}
}
ttt[rows - 1][cols - 1] = ((playerNumber == 1) ? 'X' : 'O');
movesTotal++;
}
int VerifyRow(int rows) //function for verifying the row
{
if (ttt[rows][0] == ttt[rows][1] && ttt[rows][1] == ttt[rows][2])
{
return (ttt[rows][0] == 'X') ? 1 : ((ttt[rows][0] == 'O') ? 2 : 0);
}
return 0;
}
int VerifyColumn(int cols) //function for verifying the column
{
if (ttt[0][cols] == ttt[1][cols] && ttt[1][cols] == ttt[2][cols])
{
return (ttt[0][cols] == 'X') ? 1 : ((ttt[0][cols] == 'O') ? 2 : 0);
}
return 0;
}
int VerifyDiagonal() //function for verifying the diagonal
{
if ((ttt[0][0] == ttt[1][1] && ttt[1][1] == ttt[2][2]) || (ttt[2][0] == ttt[1][1] && ttt[1][1] == ttt[0][2]))
{
return (ttt[1][1] == 'X') ? 1 : ((ttt[1][1] == 'O') ? 2 : 0);
}
return 0;
}
/* function for displaying winner */
void DisplayWinner(int players) //function for displaying winner
{
cout << "Player #" << players << " wins!" << endl;
}
bool IsGameEnd() //function checking whether the game ends or not
{
int gamewinner;
for (int rows = 0; rows<MYROWS; rows++)
{
gamewinner = VerifyRow(rows);
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
}
for (int cols = 0; cols<MYCOLS; cols++)
{
gamewinner = VerifyColumn(cols);
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
}
gamewinner = VerifyDiagonal();
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
if (movesTotal == 9)
{
cout << "Tie" << endl;
return true;
}
return false;
}
int main() //main method
{
MyTicTacToe();
for (;;)
{
PlayerMove(1);
MyTicTacToe();
if (IsGameEnd())
break;
PlayerMove(2);
MyTicTacToe();
if (IsGameEnd())
break;
}
cout << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
const int MYROWS = 3;
const int MYCOLS = 3;
char ttt[3][3] = //array ttt
{
{ '*','*','*' },
{ '*','*','*' },
{ '*','*','*' }
};
int movesTotal = 0;
void MyTicTacToe() //function MyTicTacToe
{
cout << "+-------+" << endl;
for (int rows = 0; rows < MYROWS; rows++)
{
cout << "|";
for (int cols = 0; cols < MYCOLS; cols++)
{
cout << " " << ttt[rows][cols];
}
cout << " |" << endl;
}
cout << "+-------+" << endl;
}
void PlayerMove(int playerNumber) //function for Players move
{
int rows = -1, cols = -1;
string lines;
stringstream myss;
for (;;)
{
cout << "Player #" << playerNumber << " -> (row column) : ";
getline(cin, lines, ' ');
myss.clear();
myss.str(lines);
myss >> rows >> cols;
if (rows < 1 || rows > 3 || cols < 1 || cols > 3)
{
cout << "Move Invalid!" << endl;
}
else if (ttt[rows - 1][cols - 1] != '*')
{
cout << "Place is taken! Try again!!" << endl;
}
else
{
break;
}
}
ttt[rows - 1][cols - 1] = ((playerNumber == 1) ? 'X' : 'O');
movesTotal++;
}
int VerifyRow(int rows) //function for verifying the row
{
if (ttt[rows][0] == ttt[rows][1] && ttt[rows][1] == ttt[rows][2])
{
return (ttt[rows][0] == 'X') ? 1 : ((ttt[rows][0] == 'O') ? 2 : 0);
}
return 0;
}
int VerifyColumn(int cols) //function for verifying the column
{
if (ttt[0][cols] == ttt[1][cols] && ttt[1][cols] == ttt[2][cols])
{
return (ttt[0][cols] == 'X') ? 1 : ((ttt[0][cols] == 'O') ? 2 : 0);
}
return 0;
}
int VerifyDiagonal() //function for verifying the diagonal
{
if ((ttt[0][0] == ttt[1][1] && ttt[1][1] == ttt[2][2]) || (ttt[2][0] == ttt[1][1] && ttt[1][1] == ttt[0][2]))
{
return (ttt[1][1] == 'X') ? 1 : ((ttt[1][1] == 'O') ? 2 : 0);
}
return 0;
}
/* function for displaying winner */
void DisplayWinner(int players) //function for displaying winner
{
cout << "Player #" << players << " wins!" << endl;
}
bool IsGameEnd() //function checking whether the game ends or not
{
int gamewinner;
for (int rows = 0; rows<MYROWS; rows++)
{
gamewinner = VerifyRow(rows);
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
}
for (int cols = 0; cols<MYCOLS; cols++)
{
gamewinner = VerifyColumn(cols);
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
}
gamewinner = VerifyDiagonal();
if (gamewinner != 0)
{
DisplayWinner(gamewinner);
return true;
}
if (movesTotal == 9)
{
cout << "Tie" << endl;
return true;
}
return false;
}
int main() //main method
{ char again='Y';
while(again=='y'||again=='Y')
{
MyTicTacToe();
for (;;)
{
PlayerMove(1);
MyTicTacToe();
if (IsGameEnd())
break;
PlayerMove(2);
MyTicTacToe();
if (IsGameEnd())
break;
}
cout << endl;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.