any 1 can help put on this..?i cant determine who is the winner when 3 x or 3 0 meet..

/*
Tic-Tac-Toe
     This program will allow 2 players to play a game of tic-tac-toe.
     Input: Interactive keyboard input from the players.
     Output: The tic-tac-toe board   
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int SIZE = 3;                         //size of tic-tac-toe board array
const int SIZE2 = 2;                        //size of tic-tac-toe piece array
void display_board(const char[][SIZE]);     //displays board
void get_location(int&, int&,const int, const char[]); 
                                            //gets location from player
bool check_valid(const char[][SIZE], int, int);        
                                            //checks for valid location
bool check_win(const char[][SIZE],const int, const char[]);
                                            //checks if players wins
bool check_row(const char[][SIZE],const int, const char[]);
                                            //checks for win on each row
bool check_column(const char[][SIZE],const int, const char[]);
                                            //checks for win on each column
void check_diagonal();
                                            //checks for on both diaganols
int main()
{
    char board[SIZE][SIZE] = {'*','*','*','*','*','*','*','*','*'},//tic-tac-toe board
         mark[SIZE2] = {'X', 'O'};          //both tic-tac-toe pieces
    int count = 0,                          //move counter
        r,                                  //row
        c,                                  //column
        p;                                  //player
    bool valid,                             //valid move variable
         winner = false;                    //winner variable

    while(!winner && count < 9)
    {
        for(p = 1; p < SIZE; p++)
        {
            if(count < 9)
            {
                count++;
                display_board(board);
                get_location(r, c, p, mark);
                valid = check_valid(board, r, c);
                while(!valid)
                {
                    cout << "Sorry, please choose another row and column."
                         << endl << "Row(1-3): ";
                    cin >> r;
                    cout << endl << "Column(1-3): ";
                    cin >> c;
                    valid = check_valid(board, r, c);
                }

                board[r - 1][c - 1] = mark[p - 1];
                winner = check_win(board, p, mark);
                if(winner) 
                {
                    display_board(board);
                    break;
                }
            }

        }
    }

    if(winner)
        {
            cout << "Winner is Player " << p << "!" << endl << endl;
        }
        else if(count == 9)
        {
            display_board(board);
            cout << "Players Tied!" << endl << endl;
        }

    system("PAUSE");
    return 0;
}
//*****************************************************************************
//function display_board
//parameters: board(char array)
//returns: void
//*****************************************************************************  
void display_board(const char board[SIZE][SIZE])
{
     cout << endl << "    1   2   3" << endl <<endl
          << " 1  " << board[0][0] << " | " << board[0][1] << " | " 
          << board[0][2] << endl
          << "   ---|---|---" << endl
          << " 2  " << board[1][0] << " | " << board[1][1] << " | " 
          << board[1][2] << endl
          << "   ---|---|---" << endl
          << " 3  " << board[2][0] << " | " << board[2][1] << " | " 
          << board[2][2] << endl << endl;
}
//*****************************************************************************
//function get_location
//parameters: row(int), column(int), player(int), and mark(char array)
//returns: row(int) and column(int)
//*****************************************************************************  
void get_location(int& r, int& c,const int p, const char mark[SIZE2])
{
     cout << "Player " << p << " (" << mark[p - 1] << ") choose the row "
          << "and column of your next move."
          << endl << "Row(1-3): ";
     cin >> r;
     cout << endl << "Column(1-3): ";
     cin >> c;
}     
//*****************************************************************************
//function check_valid
//parameters: board(char array), row(int), and column(int)
//returns: bool(true or false)
//*****************************************************************************  
bool check_valid(const char board[SIZE][SIZE], int r, int c)
{
     if(r >= 1 && r <= SIZE && c >= 1 && c <= SIZE &&
        board[r - 1][c - 1] != 'X' && board[r - 1][c - 1] != 'O')    
     {
         return true;
     }
     else
     {
         return false;
     }
}
//*****************************************************************************
//function check_win
//parameters: board(char array), player(int), and mark(char array)
//returns: bool(true or false)
//*****************************************************************************  
bool check_win(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])
{
     bool win;                              //local variable
     win = check_row(board, p, mark);
     if(win)
     {
          return true;
     }
     win = check_column(board, p, mark);
     if(win)
     {
          return true;
     }
     win = check_diagonal();
     if(win)
     {
          return true;
     }
     return false;

}    
//*****************************************************************************
//function check_row
//parameters: board(char array), player(int), and mark(char array)
//returns: bool(true or false)
//*****************************************************************************  
bool check_row(const char board[SIZE][SIZE],const int p, const char mark[SIZE2])     
{
     for(int r = 0; r < SIZE; r++)
     {
          if(board[r][0] == mark[p - 1] &&
             board[r][1] == mark[p - 1] &&
             board[r][2] == mark[p - 1])
          {
               return true;
          }
          else
          {
               return false;
          }
     }
}
//*****************************************************************************
//function check_column
//parameters: board(char array), player(int), and mark(char array)
//returns: bool(true or false)
//*****************************************************************************  
bool check_column(const char board[SIZE][SIZE],const int p, 
                  const char mark[SIZE2])     
{
     for(int c = 0; c < SIZE; c++)
     {
          if(board[0][c] == mark[p - 1] && 
             board[1][c] == mark[p - 1] &&
             board[2][c] == mark[p - 1])
          {
               return true;
          }
          else
          {
               return false;
          }
     } 
}
//*****************************************************************************
//function check_diagonal
//parameters: board(char array), player(int), and mark(char array)
//returns: bool(true or false)
//*****************************************************************************  
void check_diagonal()
{
     if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != 0)
     {
         p = board[0][0];
         return;
     }

Recommended Answers

All 7 Replies

You have 'const' p in your formal parameters of your check_diagnal function. I don't know if that's the only reason because my compiler has thrown up a few other errors.

Actually, i put that problem in myself. Sorry

Then, what should we correct it in order to run nicely?

I was just trying to install a compiler that i prefer....

Looking at the first problem i would say that your check_diagonals should return a bool.

Then i would say that within that function the 2D array needs to be passed in like the other two check functions.

Then remove the 'const' from const int p in that functions parameters. Now it compiles.

can u write out the code here..not very get..wat u means..

I ran it once and it played ok.

bool check_diagonal(const char board[SIZE][SIZE],int p, const char mark[SIZE2])
{
    cout << "Check Diagonal" << endl;

    if(board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != 0)
    {
        p = board[0][0];
        return true;
    }
   else
   {
        return false;
   }
}

Actually you don't need that p =[0][0] assignment so the const can stay in.

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.