I think I've got something wrong. My program runs but when I enter the column and row number the X and O dont appear. Was wondering if someone can help here.

#include<iostream>
#include<iomanip>
using namespace std;

void drawBoard(char board[][3]);
char checkWinner3by3(char board[][3]);

int main()

{
    char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
    char player = 'X';
    char winner = ' ';

    int row;
    int column;

    bool is_move;
    bool is_row;
    bool is_column;

    cout<<"  TIC TAC TOE  "<<endl;
    while(winner == ' ')
    {
        is_move = false; 
        is_row = false;
        is_column = false;

        drawBoard(board);
        cout << "Player ";
        if(player == 'X')
        {
            cout << 1;
        }
        else
        {
            cout << 2;
        }
         cout <<":"<< endl;

        is_move = false;
        while(!is_move)
        {
            is_row = false;
            while(!is_row)
            {
                cout << "Row: ";
                cin >> row;
                if(row == 1 || row == 2 || row == 3)
                {
                    is_row = true;
                }
                else
                {
                    cout << endl << "Wrong row!" << endl;
                }
            }

            is_column = false;
            while(!is_column)
            {
                cout << "Column: ";
                cin >> column;
                if(column == 1 || column == 2 || column == 3)
                {
                    is_column = true;
                }
                else
                {
                    cout << endl << "Wrong column! Try again" << endl;
                }
            }

            if(board[row-1][column-1] == ' ')
            {
                board[row-1][column-1] = player;
                is_move = true;

                if(player='X')
                {
                    player = 'O';
                }
                else
                {
                    player = 'X';
                }
            } 
            else
            {
                cout << "Taken, try again: " << endl << endl;
                drawBoard(board);
            }
        }
        cout<<endl; 


        winner=checkWinner3by3(board);
        if(winner=='X' || winner =='O')
        {
            drawBoard(board);
            cout<<"Congrats!! to Player ";
            if(winner == 'X')
            {
                cout << 1;
            }
            else
            {
                cout << 2;
            }
            cout<<" you are the winner!"<<endl;
        }
        else if(winner == 'T')
        {
            drawBoard(board);
            cout << "It's a tie!" << endl;
        }
    }
    return 0;
}


//Displaying the board
void drawBoard(char board[][3])
{
    char print[][3] = {{' ',' ',' '},
                        {' ',' ',' '},
                        {' ',' ',' '}};

    cout << "     1   2   3" << endl;

    cout << " 1" << " * " << print[0][0] << " * " << print[0][1]<< " * " << print[0][2] << " * " << endl;
    cout << endl;

    cout << " 2" << " * " << print[1][0] << " * " << print[1][1] << " * "<< print[1][2] << " * " << endl;
    cout << endl;

    cout << " 3" << " * " << print[2][0] << " * " << print[2][1] << " * "<< print[2][2] << " * " << endl;
}

//Calculating the board for winner
char checkWinner3by3(char board[][3])
{
    for(int i=0; i<3; i++)  
    {
        if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
        {
            return board[i][0];
        }
    }

    for(int i=0; i<3; i++) 
    {
        if(board[0][i]==board[1][i] && board[0][i]==board[2][i]) 
        {
            return board[0][i];
        }
    }

    if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
    {
        return board[0][0];
    }

    if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
    {
       return board[0][2];
    }   

    return ' ';
}

Hi,

Your main issue is the drawBoard function as you have it. You are printing what you wanted, not what is in the array.

Something like so, could do better

    for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 3; j++)
           cout << board[i][j] << "\t";
       cout << endl;
    }

By so doing, you are printing what is in the array not what you wanted. You might have to format that in a way you wanted.

Check also line 79, in the OP.. That should be if(player == 'X') NOT if(player='X') like you have it.

Lastly, please comment your code. It not beautiful as it is. In some few days time, you might not know what is which.

So your saying I have to change the //Displaying board function into for instead of print?

What are doing with the 2D Array Print in your drawBoard function? What is the relation between the 2D array print and that of the parameters board?

All I want is to get the program to show X and O in the spot the users pick. As of right now it does not appear on the board.

The array you are printing is not doing that. It is an empty array. Use the array you passed, then use two for loops because you are using a 2D array, just like I showed in my first post. You should have what you wanted.

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.