how would I get this code to recognize a tie in the game?
/* TicTacToe Game */
#include <iostream>
#include <sstream>
#include <string>
namespace Games
{
class TicTacToe
{
public:
TicTacToe(void);
void Play(void);
void Reset(void);
private:
char board[9];
char player;
static int wins[24];
void displayBoard(void);
bool getMove(void);
void initializeBoard(void);
void swapPlayers(void);
bool winner(void);
};
}
using namespace std;
using namespace Games;
int main()
{
cout << "Welcome to the TicTacToe Game" << endl << endl;
TicTacToe game;
string answer;
return 0;
}
/* TicTacToe Class Public Members */
TicTacToe::TicTacToe()
{
player = 'X';
initializeBoard();
}
void TicTacToe::Play()
{
for (int i = 1; i <= 9; i++)
{
if (!getMove()) i--; //stay on move
displayBoard();
if (winner()) break;
if (i == 9) break;
swapPlayers();
}
cout << endl << player << " wins!" << endl;
if (
}
void TicTacToe::Reset()
{
initializeBoard();
}
/* TicTacToe Class Private Members */
int TicTacToe::wins[24] = {0, 1, 2, 0, 3, 6, 0, 4, 8, 1, 4, 7, 2, 4, 6, 2, 5, 8, 3, 4, 5, 6, 7, 8};
void TicTacToe::displayBoard()
{
cout << endl;
for (int i = 1; i <= 9; i++)
{
if (i % 3 == 0 )
cout << board[i - 1] << endl;
else
cout << board[i - 1] << " | ";
}
}
bool TicTacToe::getMove(void)
{
cout << endl << "Select a square for " << player << ": ";
int square;
string data;
stringstream in;
getline(cin, data);
in << data;
in >> square;
if (square < 1 || 9 < square)
{
cout << "Invalid square" << endl;
swapPlayers(); //keep same player on move
//return;
}
else if (board[square - 1] == 'X' || board[square - 1] == 'O')
{
cout << "Square already occupied" << endl;
swapPlayers(); //keep same player on move
//return;
}
else
{
board[square - 1] = player;
}
}
void TicTacToe::initializeBoard()
{
cout << endl;
for (int i = 1; i <= 9; i++)
board[i - 1] = ' ';
for (int i = 1; i <= 9; i++)
{
if (i % 3 == 0 )
cout << i << endl;
else
cout << i << " | ";
}
}
void TicTacToe::swapPlayers()
{
if (player == 'X') player = 'O';
else player = 'X';
}
bool TicTacToe::winner()
{
for (int i = 0; i < 21; i += 3)
{
bool A = board[wins[i]] != ' ';
bool B = board[wins[i]] == board[wins[i + 1]];
bool C = board[wins[i]] == board[wins[i + 2]];
if (A && B && C)
return true;
}
return false;
}