Hey guys and gals. I'm looking for a little help here. I am throughly lost as to how to do this program. Keep in mind I am not asking for someone to do it.... although the though is crossing my mind lol. I'm just looking for pushes in the right direction. I have to write a tic tac toe program that uses a two dimesnsional array. It's a two player game, no computer choices invovled. So I've got the program started, but I have no clue how to get it working. I'm stuck on when the first player enters the row and column number. I dont know how to convert it to placing the X in that spot on the array. Then I know I'm already lost on how to make a function that can tell if someone wins loses or ties. Any help is appricated.

1) You MUST use a function to see if someone has won. Your function MUST take as an argument the 2-dimensional character array and return a number 1 if player 1 has won or a 2 if player 2 has won.
2) You MUST use a function that takes as an argument the 2-dimensional character array and displays the contents of the board array.

This is what I got so far.

//This is a simple program to play the game Tic-Tac-Toe. 
//This program will not keep score or stats.
//This is a two player game.
#include <iostream>
#include <iomanip>
using namespace std;

void displayBoard();

int main()
{

  cout << "Let's play Tic Tac Toe!\n";
  displayBoard();
  cout << "Player One will be X's and Player Two will be O's.\n";
  cout << "Each player will enter a set of coordinates for thier move.\n";
  cout << "First the Row number followed by column number.\n";
  cout << "Rows and columns start with 0 and end with 2\n";
  cout << "starting with on the left.\n";
  cout << "Player One. Please enter row number followed by column number.\n";
  cin >> board[ROW][COL];
  return 0;
}

void displayBoard()
  {

    const int ROW = 3;
    const int COL = 3;
    char board[ROW][COL] =  {{'*', '*', '*'},
                            {'*', '*', '*'},
                            {'*', '*', '*'}};

    board[0][0] = '*';
    board[0][1] = '*';
    board[0][2] = '*';
    board[1][0] = '*';
    board[1][1] = '*';
    board[1][2] = '*';
    board[2][0] = '*';
    board[2][1] = '*';
    board[2][2] = '*';

      cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << "\n";
      cout << "__|___|___\n";
      cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << "\n";
      cout << "__|___|___\n";
      cout <<  board[2][0] << " | " << board[2][1] << " | " << board[2][2] <<"\n";
      cout << "  |   |   \n";
  }

What you would do is
1) Prompt them for the row number via cin (in the range of 1-3 if you're going for easy human interface, or 0-2 if you're lazy :P )
2) Validate the input to make sure it's within the acceptable range, and repeat step 1 if the input is invalid.
3) Repeat steps 1 and 2 for column.
4) Change the symbol at the given row and column to either an X or an O, depending on whose turn it is.

From there, it's just turning your thought process into actual code. Is there some concept in C++ that you don't quite understand how to do, that's preventing you from doing this?

The most I am having trouble with is the array issue. Im not understanding how to use them for this situation. I've been trying the cin and all I get from compliler is that's the wrong type. Plus How, once I get the coordinates do I change it to X and O?

Ok so here's what i got so far. But I'm having some issues. One, I cant figure out how to tell if there is a diagonal win. And second I want to make the output prettier. If there a O or X then i want an asterisk in the space, mainly because the teacher wants it that way. I would also like to include the lines like a regualr tic tac toe board has. Once again any help is appricated.

//This program is made to play a two player game of Tic Tac Toe.
//This program does not keep score or stats.
#include <iostream>
using namespace std;


// Global variables

const int ROW = 3;
const int COL = 3;

//Function Prototypes

void displayBoard(char[][COL], int, int);
void gameBoard(char[][COL], int, int);
bool gameEnd(char[][COL], int, int);

int main()
{
  char board[ROW][COL];
  int row, col;
  char player = 'X';

  gameBoard(board, ROW, COL);
  displayBoard(board, ROW, COL);

    cout << "Let's play Tic Tac Toe!\n";
    cout << "Player one will be X's and player two will be O's.\n";

    do {
        cout << "Player " << player << endl;
          do {
              cout << "Enter row: ";
              cin >> row;
              cout << "Enter column: ";
              cin >> col;
             } while (board[row][col] != ' ');

  board[row][col] = player;
  displayBoard(board, ROW, COL);

    if (player == 'X')
        player = 'O';
    else
      player = 'X';

    } while (!gameEnd(board, ROW, COL));

    cout << "Game over!" << endl;
  }


void gameBoard(char space[][COL], int rowsize, int colsize)
{
    for (int row = 0; row < rowsize; row++)
    for (int col = 0; col < colsize; col++)
    space[row][col] = '*';
}
void displayBoard(char space[][COL], int rowsize, int colsize)
{
    for (int row = 0; row < rowsize; row++)
    {
      cout << row;

      for (int col = 0; col < colsize; col++)
        cout << "\t" << space[row][col];
        cout << endl;
    }
        for (int col = 0; col < colsize; col++)
        cout << "\t" << col;
        cout << endl;
}

bool gameEnd(char space[][COL], int rowsize, int colsize)
{
  int notUsed = 0;
  int Xs;
  int Os;

// This section is to check for a tie.

      for (int row = 0; row < rowsize; row++)
      for (int col = 0; col < colsize; col++)
      if (space[row][col] == ' ') notUsed++;
      if (notUsed == 0)

      return true;

// This section is to check each row for winning line. 

      for (int row = 0; row < rowsize; row++)
      {
        Xs = 0;
        Os = 0;

        for (int col = 0; col < colsize; col++)
        {
          if (space[row][col] == 'X')
            Xs++;

          else if (space[row][col] == 'O')
            Os++;
        }

          if (Xs == colsize   ||   Os == colsize)

          return true;
      }

// This section is to check each column for winning line. 

      for (int col = 0; col < colsize; col++)
      {
        Xs = 0;
        Os = 0;
 for (int row = 0; row < rowsize; row++)
        {
          if (space[row][col] == 'X')
            Xs++;

          else if (space[row][col] == 'O')
            Os++;
        }

          if (Xs == colsize   ||   Os == colsize)
          return true;
      }

          return false;
}

Let's start off with the obvious:

do {
    cout << "Player " << player << endl;
      do {
          cout << "Enter row: ";
          cin >> row;
          cout << "Enter column: ";
          cin >> col;
         } while (board[row][col] != ' ');

This piece of code is unreliable and may invoke undefined behavior. You are checking board[row][col] and you don't actually know if row or col are actually in the array's bounds. If the user enters -1 for both answers, it will check against board[-1][-1], and you're just reading junk data at that point so it could be anything. Make sure you validate the user's chosen coordinates before you start reading and writing against board[row][col].

One, I cant figure out how to tell if there is a diagonal win.

Think it out logically. There are only two ways to win diagonally. A corner, the middle, and the opposite corner have to be matching AND they have to not be empty spaces.

And second I want to make the output prettier. If there a O or X then i want an asterisk in the space,

If you want spaces to be printed as asterisks, modify your displayBoard() function so that when it detects a space, it prints an asterisk instead.

I would also like to include the lines like a regualr tic tac toe board has

You'll pretty much just have to ASCII-art that out simliar to what you did in the first post, and print the appropriate characters at the appropriate positions.

Also, please read your assignment carefully.

Your function MUST take as an argument the 2-dimensional character array and return a number 1 if player 1 has won or a 2 if player 2 has won.

This means your gameEnd() function probably needs to return int, not bool.

Ok almost got it. One last problem. Trying to get the program to end correctly and have it show who the winner is. I had it one way and it always said Player X was the winner. Now it just wont end. Please help. Thanks again.

#include <iostream>
    using namespace std;


    // Global variables

    const int ROW = 3;
    const int COL = 3;

    //Function Prototypes

    void displayBoard(char[][COL], int, int);
    void gameBoard(char[][COL], int, int);
    int gameEnd(char[][COL], int, int);

    int main()
    {
      char board[ROW][COL];
      int row, col, win;
      char player = 'X';

      gameBoard(board, ROW, COL);
      displayBoard(board, ROW, COL);

        cout << "Let's play Tic Tac Toe!\n";
        cout << "Player one will be X's and player two will be O's.\n";

        do {
            cout << "Player " << player << endl;
              do {
                  do {
                  cout << "Enter row: ";
                  cin >> row;
                  }while (row < 0 or row > 2);

                  do {
                  cout << "Enter column: ";
                  cin >> col;
                     }while (col < 0 or col > 2);

                 } while (board[row][col] != ' ');

      board[row][col] = player;
      displayBoard(board, ROW, COL);

        if (player == 'X')
            player = 'O';
        else
          player = 'X';

           } while (gameEnd(board, ROW, COL));

                if (win == 1)
                  cout << "Player X wins!\n";
                else if (win == 2)
                  cout << "Player O wins!\n";


                cout << "Game over!" << endl;
    }


    void gameBoard(char space[][COL], int rowsize, int colsize)
    {
        for (int row = 0; row < rowsize; row++)
        for (int col = 0; col < colsize; col++)
        space[row][col] = ' ';
    }

    void displayBoard(char space[][COL], int rowsize, int colsize)
    {
        for (int row = 0; row < rowsize; row++)
        {
          cout << row;

          for (int col = 0; col < colsize; col++)
            cout << " \t" << space[row][col];
            cout << endl;
        }
            for (int col = 0; col < colsize; col++)
            cout << "\t" << col;
            cout << endl;
    }

    int gameEnd(char space[][COL], int rowsize, int colsize)
    {
      int notUsed = 0;
      int Xs, Os, win;

    // This section is to check for a tie.

          for (int row = 0; row < rowsize; row++)
          for (int col = 0; col < colsize; col++)
          if (space[row][col] == ' ') notUsed++;
          if (notUsed == 0)

          return true;

    // This section is to check each row for winning line. 

          for (int row = 0; row < rowsize; row++)
          {
            Xs = 0;
            Os = 0;

            for (int col = 0; col < colsize; col++)
            {
              if (space[row][col] == 'X')
                Xs++;

              else if (space[row][col] == 'O')
                Os++;
            }

              if (Xs == colsize)
                win = 1;

     else if (Os == colsize)
                win = 2;

              return win;
          }

    // This section is to check each column for winning line. 

          for (int col = 0; col < colsize; col++)
          {
            Xs = 0;
            Os = 0;

            for (int row = 0; row < rowsize; row++)
            {
              if (space[row][col] == 'X')
                Xs++;

              else if (space[row][col] == 'O')
                Os++;
            }

              if (Xs == colsize)
                win = 1;

              else if (Os == colsize)
                win = 2;

              return win;
          }

    // This section is to check for a win in diagonal line.

          for (int row = 0; row < rowsize; row++)
          {
            Xs = 0;
            Os = 0;

            for (int col = 0; col < colsize; col++)
            {
              if (space[0][0] == 'X' && space[1][1] == 'X' && space[2][2] == 'X')
              Xs++;

            else if (space[0][0] == 'O' && space[1][1] == 'O' && space[2][2] == 'O')
              Os++;
            }

            if (Xs == colsize)
              win = 1;

            else if (Os == colsize)
              win = 2;

            return win;

          }

          for (int row = 0; row < rowsize; row++)
     for (int row = 0; row < rowsize; row++)
          {   
              Xs = 0;
              Os = 0;

          for (int col = 0; col < colsize; col++)
            {
              if (space[0][2] == 'X' && space[1][1] == 'X' && space[2][0] == 'X')
              Xs++;

              else if (space[0][2] == 'O' && space[1][1] == 'O' && space[2][0] == 'O')
              Os++;
            }

            if (Xs == colsize)
              win = 1;

            else if (Os == colsize)
              win = 2;

            return win;

          }
        return false;
    }

You were making it more complicated than it needed to be. Each winning line has to include the outside row or col, and for each spot you only need to compare it to 2 others. Therefore for each row or col you don't need to loop through the other just use a long conditional to check the other 2 possiblities. for instance to check the rows for a winning line:

// This section is to check each row for winning line.
for (int row = 0; row < ROW; row++)
{
    if(space[row][0] == space[row][1] && space[row][0] == space[row][2] && space[row][0] != '*')
    {
        if(space[row][0] == 'X')
            return 1;
        else
            return 2;
    }
}

Since there are only 2 possiblities for the diagonal you won't even need a loop.

Also you had 'win' declared in main and gameend. But you weren't setting 'win' in main to the one in gameend, so the game wouldn't end properly.

Here's some code to look at with a few other changes:

#include <iostream>
using namespace std;
// Global variables
const int ROW = 3;
const int COL = 3;
//Function Prototypes
void displayBoard(char[][COL]);
void gameBoard(char[][COL]);
int gameEnd(char[][COL]);
int ReturnWinner(char[][COL],int, int);
int main()
{
    char board[ROW][COL];
    int row = 0;
    int col = 0;
    int win = 0;
    char player = 'X';
    gameBoard(board);
    displayBoard(board);
    cout << "Let's play Tic Tac Toe!\n";
    cout << "Player one will be X's and player two will be O's.\n";
    do {
        cout << "Player " << player << endl;
        do {
            do {
                cout << "Enter row(1-3): ";
                cin >> row;
            }while (row-1 < 0 || row-1 > 2);
            do {
                cout << "Enter column(1-3): ";
                cin >> col;
            }while (col-1 < 0 || col-1 > 2);
        } while (board[row-1][col-1] != '*');
        board[row-1][col-1] = player;
        displayBoard(board);
        if (player == 'X')
            player = 'O';
        else
            player = 'X';
        win = gameEnd(board);
    } while (win < 1);
    if (win == 1)
        cout << "Player X wins!\n";
    else if (win == 2)
        cout << "Player O wins!\n";
    else if (win == 3)
        cout << "It's a draw!\n";
    cout << "Game over!" << endl;
    cin.ignore();
    cin.get();
}
void gameBoard(char space[][COL])
{
    for (int row = 0; row < ROW; row++)
        for (int col = 0; col < COL; col++)
            space[row][col] = '*';
}
void displayBoard(char space[][COL])
{
    for (int col = 0; col < COL; col++)
        cout << "\t" << col+1;
    cout << endl << endl;
    for (int row = 0; row < ROW; row++)
    {
        cout << row+1;
        for (int col = 0; col < COL; col++)
            cout << " \t" << space[row][col];
        cout << endl;
    }
    cout << endl;
}
int gameEnd(char space[][COL])
{
    // This section is to check each row for winning line.
    for (int row = 0; row < ROW; row++)
    {
        if(space[row][0] == space[row][1] && space[row][0] == space[row][2] && space[row][0] != '*')
        {
            return ReturnWinner(space,row,0);
        }
    }
    // This section is to check each column for winning line.
    for (int col = 0; col < COL; col++)
    {
        if(space[0][col] == space[1][col] && space[0][col] == space[2][col] && space[0][col] != '*')
        {
            return ReturnWinner(space,0,col);
        }
    }
    // This section is to check for a win in diagonal line.
    if (space[0][0] == space[1][1] && space[0][0] == space[2][2] && space[0][0] != '*')
        return ReturnWinner(space,0,0);
    if(space[3][0] == space[1][1] && space[3][0] == space[0][3] && space[3][0] != '*')
        return ReturnWinner(space,3,0);
    // This section is to check for a tie.
    for (int row = 0; row < ROW; row++)
    {
        for (int col = 0; col < COL; col++)
        {
            if (space[row][col] == '*') return 0;
        }
    }
    return 3;
}
int ReturnWinner(char space[][COL],int row, int col)
{
        if(space[row][col] == 'X')
            return 1;
        else
            return 2;
}
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.