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;
}

Firstly, you seem to have a cool chick name; therefore, I will have to ask ye' a/s/l.

Secondly, (without having to look at your code) tie detection in tic-tac-toe can be as simple as testing a turn counter... when a player has reached 5 turns, and none of ye' win functions have returned true, then the only possible scenario a tie.

Firstly, you seem to have a cool chick name; therefore, I will have to ask ye' a/s/l.

Secondly, (without having to look at your code) tie detection in tic-tac-toe can be as simple as testing a turn counter... when a player has reached 5 turns, and none of ye' win functions have returned true, then the only possible scenario a tie.

There is a counter in this part of the code...


void TicTacToe::Play()
{
for (int i = 1; i <= 9; i++)
{
if (!getMove()) i--; //stay on move

displayBoard();

if (winner()) break;
if (i == 9) break;


however since it's a two player game and not one against the computer, i'm pretty sure it should be nine...I tried putting five in also and it didn't work. Is there something wrong with the syntax as to why this isn't working? When the program compiles and it's a tie...it just goes on forever like an infinite loop instead of breaking, but yet it works properly if someone wins. Since they we're pretty much both done the same way using the for loop, I expected them to either both work or not work simultaneously. And as far as asl...female and i'm most likely too young..

Here are the number of turns each player can take:

|     |
p1(1)|p2(1)|p1(2)  
-----------------
     |     |
p2(2)|p1(3)|p2(3)
-----------------
     |     |
p1(4)|p2(4)|p1(5) 
     |     |

So we can conclude that there are 9 maxumum total moves that can be made, the person who goes first has a potential to make a max. of 5 moves; the person who goes second can only make a max. of 4 moves.

So you can keep track of your turn counter by player, or by total moves... it doesn't matter. Once ye' reach your magic number of max. moves that can possibly be made in a game of tictactoe and none of your win functions have tested true, the only possible scenario is a tie.

//If you reached max. number of possible turns
if(turns == 9)
{
     //And no winner is detected...
     if(!columns_check() && !rows_check() && !diags_check())
     {
          //Then you must have a tie.
          tie();
     }
}

Crystalll: As an aside, in the future, please put CODE tags around your code samples. The forum software does not retain formatting by default, so your code loses it's indentation unless it is in code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.