#include <iostream>
using namespace std;

const int ROWS = 3;
const int COLS = 3;

//char matrix[3][3];
void showBoard(char[][3]);
void pTurn(int&, int&);
void checkPlayerWin(bool&, char[][3]);
void checkCompWin(char[][3], bool&);
void checkForBlock(char[][3], bool& );
char compRandomTurn();
void compWinCheck();


// Main // Main// Main // Main// Main // Main// Main// Main// Main
int main(){
char matrix[3][3];    
//  variable ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bool cTurn = false;
bool playerTurn = true;
bool endGame = false;
char player = 'X';
char computer = 'O';
int i,j,sum=0;


// player turn ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
while(sum <= 9 && playerTurn == true && endGame){
        showBoard(matrix[][3]);  
        pTurn(i,j);
        checkPlayerWin(endGame, matrix[][3]);
        sum++; 
        playerTurn = false;
        cTurn = true;     
} 

//computer turn ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
while(sum <= 9 && cTurn == true && endGame){        
       showBoard(matrix[][3]); 
       compWinCheck(matrix[][3],endGame);
       checkForBlock(matrix[][3],cTurn);          
       checkCompWin(matrix[][3], endGame);
sum++;
cTurn = false;
playerTurn = true;
}

system ("pause");
return 0;
} // End main #########################################################

/*//Function to initialize and display board
void showBoard(boardMatrix[3][3])
{
        for(int col = 0; col <= COLS - 1; ++col)
      {
            for(int row = 0; row < ROWS; ++row)
           { 
            d[row][col] = '|'  ;                       
           cout << d[row][col] ;
           }
           cout <<endl;
      }    
}
*/
//the board ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void showBoard(char matrix[][3])
{
cout << "\n\n";
cout<<"\n\t\t                1   2   3\n"<<endl;
cout<<"\t\t             1  "<<matrix[0][0]<<" | "<<matrix[0][1]<<" | "<<matrix[0][2]<<endl;
cout<<"\t\t               ---|---|---\n";
cout<<"\t\t             2  "<<matrix[1][0]<<" | "<<matrix[1][1]<<" | "<<matrix[1][2]<<endl;
cout<<"\t\t               ---|---|---\n";
cout<<"\t\t             3  "<<matrix[2][0]<<" | "<<matrix[2][1]<<" | "<<matrix[2][2]<<"\n\n\n";
}

//player turn ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void pTurn(char matrix[][3],int& i, int& j, int& cTurn){
     cout<<"Player 1 is 'X': choose the row and column"<<endl;
     cout<<"Row : ";
     cin>>i;
     cout<<"Column : ";
     cin>>j;
for (;i>3 || i<1 || j>3 || j<1 ||('X'== matrix[i-1][j-1]||'O'== matrix[i-1][j-1]);){
    cout<<"Sorry, but you gotta choose another place.\n"
        <<"row : ";
    cin>>i;
    cout<<"column : ";
    cin>>j;
    }
 cTurn = true;
}

//computer turn ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
char compRandomTurn(int& sum, int& i, int& j, bool& cTurn){
    if(sum <= 9){
        i = rand()%3;
        j = rand()%3;
        cTurn = false;
    return 'O';
    
     }
}
   // check For  computer block Block ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void checkForBlock(char matrix[][3], bool& cTurn){     
     // check for block -row 0
     if(matrix[0][0]&&matrix[0][1]=='X'&&matrix[0][2]!='O') 
     { matrix[0][2] = 'O'; cTurn = false;} 
     
     else if(matrix[0][0]&&matrix[0][2]=='X'&&matrix[0][1]!='O')
     {matrix[0][1] = 'O'; cTurn = false;}
     
     else if(matrix[0][1]&&matrix[0][2]=='X'&&matrix[0][0]!='O')
     {matrix[0][0] = 'O'; cTurn = false;} 
     
     // check for block -col 0  
     else if(matrix[0][0]&&matrix[1][0]=='X'&&matrix[2][0]!='O')
     {matrix[2][0] = 'O'; cTurn = false;}   
     
     else if(matrix[0][0]&&matrix[2][0]=='X'&&matrix[1][0]!='O')
     {matrix[1][0] = 'O'; cTurn = false;}   
     
     else if(matrix[1][0]&&matrix[2][0]=='X'&&matrix[0][0]!='O')
     {matrix[0][0] = 'O'; cTurn = false;}
     
     // check for block -row 1
     else if(matrix[1][0]&&matrix[1][1]=='X'&&matrix[1][2]!='O')
     {matrix[1][2] = 'O'; cTurn = false;}
     
     else if(matrix[1][0]&&matrix[1][2]=='X'&&matrix[1][1]!='O')
     {matrix[1][1] = 'O'; cTurn = false;}
     
     else if(matrix[1][1]&&matrix[1][2]=='X'&&matrix[1][0]!='O')
     {matrix[1][0] = 'O'; cTurn = false;}
     
     // check for block -col 0
     else if(matrix[0][1]&&matrix[1][1]=='X'&&matrix[2][1]!='O')
     {matrix[2][1] = 'O'; cTurn = false;}
     
     else if(matrix[0][1]&&matrix[2][1]=='X'&&matrix[1][1]!='O')
     {matrix[1][1] = 'O'; cTurn = false;}
     
     else if(matrix[1][1]&&matrix[1][2]=='X'&&matrix[0][1]!='O')
     {matrix[0][1] = 'O'; cTurn = false;}
     
     // check for block -row 2
     else if(matrix[2][0]&&matrix[2][1]=='X'&&matrix[2][2]!='O')
     {matrix[2][2] = 'O'; cTurn = false;}
     
     else if(matrix[2][0]&&matrix[2][2]=='X'&&matrix[2][1]!='O')
     {matrix[2][1] = 'O'; cTurn = false;}
     else if(matrix[2][1]&&matrix[2][2]=='X'&&matrix[2][0]!='O')
     {matrix[2][0] = 'O'; cTurn = false;}
     // check for block -col2 0
     else if(matrix[0][2]&&matrix[1][2]=='X'&&matrix[2][2]!='O')
     {matrix[2][2] = 'O'; cTurn = false;}
     else if(matrix[0][2]&&matrix[2][2]=='X'&&matrix[1][2]!='O')
     {matrix[1][2] = 'O'; cTurn = false;}
     else if(matrix[1][2]&&matrix[2][2]=='X'&&matrix[0][2]!='O')
     {matrix[0][2] = 'O'; cTurn = false;}
     // check for block -diaginal
     else if(matrix[0][0]&&matrix[1][1]=='X'&&matrix[2][2]!='O')
     {matrix[2][2] = 'O'; cTurn = false;}
     else if(matrix[2][0]&&matrix[1][1]=='X'&&matrix[0][2]!='O')
     {matrix[0][2] = 'O'; cTurn = false;}
     
     else
         compRandomTurn();
}     

//check to see if comp can win ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void compWinCheck(char matrix[][3], bool& endGame){
          
     // check if comp can win -row 0
     if(matrix[0][0]&&matrix[0][1]=='O'&&matrix[0][2]!='X'){matrix[0][2] = 'O'; endGame = true;}
     else if(matrix[0][0]&&matrix[0][2]=='O'&&matrix[0][1]!='X'){matrix[0][1] = 'O'; endGame = true;}
     else if(matrix[0][1]&&matrix[0][2]=='O'&&matrix[0][0]!='X'){matrix[0][0] = 'O'; endGame = true;} 
     // check if comp can win  -col 0  
     else if(matrix[0][0]&&matrix[1][0]=='O'&&matrix[2][0]!='X'){matrix[2][0] = 'O'; endGame = true;}   
     else if(matrix[0][0]&&matrix[2][0]=='O'&&matrix[1][0]!='X'){matrix[1][0] = 'O'; endGame = true;}   
     else if(matrix[1][0]&&matrix[2][0]=='O'&&matrix[0][0]!='X'){matrix[0][0] = 'O'; endGame = true;}
     // check if comp can win -row 1
     else if(matrix[1][0]&&matrix[1][1]=='O'&&matrix[1][2]!='X'){matrix[1][2] = 'O'; endGame = true;}
     else if(matrix[1][0]&&matrix[1][2]=='O'&&matrix[1][1]!='X'){matrix[1][1] = 'O'; endGame = true;}
     else if(matrix[1][1]&&matrix[1][2]=='O'&&matrix[1][0]!='X'){matrix[1][0] = 'O'; endGame = true;}
     // check if comp can win -col 0
     else if(matrix[0][1]&&matrix[1][1]=='O'&&matrix[2][1]!='X'){matrix[2][1] = 'O'; endGame = true;}
     else if(matrix[0][1]&&matrix[2][1]=='O'&&matrix[1][1]!='X'){matrix[1][1] = 'O'; endGame = true;}
     else if(matrix[1][1]&&matrix[1][2]=='O'&&matrix[0][1]!='X'){matrix[0][1] = 'O'; endGame = true;}
     // check if comp can win -row 2
     else if(matrix[2][0]&&matrix[2][1]=='O'&&matrix[2][2]!='X'){matrix[2][2] = 'O'; endGame = true;}
     else if(matrix[2][0]&&matrix[2][2]=='O'&&matrix[2][1]!='X'){matrix[2][1] = 'O'; endGame = true;}
     else if(matrix[2][1]&&matrix[2][2]=='O'&&matrix[2][0]!='O'){matrix[2][0] = 'O'; endGame = true;}
     // check if comp can win -col 2 
     else if(matrix[0][2]&&matrix[1][2]=='O'&&matrix[2][2]!='X'){matrix[2][2] = 'O'; endGame = true;}
     else if(matrix[0][2]&&matrix[2][2]=='O'&&matrix[1][2]!='X'){matrix[1][2] = 'O'; endGame = true;}
     else if(matrix[1][2]&&matrix[2][2]=='O'&&matrix[0][2]!='X'){matrix[0][2] = 'O'; endGame = true;}
     // check if comp can win -diaginal
     else if(matrix[0][0]&&matrix[1][1]=='O'&&matrix[2][2]!='X'){matrix[2][2] = 'O'; endGame = true;}
     else if(matrix[2][0]&&matrix[1][1]=='O'&&matrix[0][2]!='X'){matrix[0][2] = 'O'; endGame = true;}
}     
// check to see if computer wins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void checkCompWin(char matrix[][3], bool& endGame){

    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[0][1]=='O' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[0][2]=='O' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[0][0]=='O' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[1][0]=='O' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
    if (matrix[2][0]=='O' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]){
           endGame = true;
                          cout<<"Computer wins"<<endl;}
}

//check player win ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void checkPlayerWin(bool& endGame, char matrix[][3]){
     if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[2][0]=='X' && matrix[2][0]==matrix[1][1] && matrix[1][1]==matrix[0][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[0][0]=='X' && matrix[0][0]==matrix[1][0] && matrix[1][0]==matrix[2][0]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[0][1]=='X' && matrix[0][1]==matrix[1][1] && matrix[1][1]==matrix[2][1]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[0][2]=='X' && matrix[0][2]==matrix[1][2] && matrix[1][2]==matrix[2][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[0][0]=='X' && matrix[0][0]==matrix[0][1] && matrix[0][1]==matrix[0][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[1][0]=='X' && matrix[1][0]==matrix[1][1] && matrix[1][1]==matrix[1][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
     if (matrix[2][0]=='X' && matrix[2][0]==matrix[2][1] && matrix[2][1]==matrix[2][2]){
            endGame = true;
                           cout<<"Player 1 wins"<<endl;}
}

Hello, I hope that I wraped the code correctly. I am trying to get this TTT game to work. I fixed all errors accept it says on lines
32, 34,42-45 "expected primary-expression before ']' token "
Help, hanks, VERMUNDr

Recommended Answers

All 7 Replies

Did you try using passing matrix as matrix[3][3] instead of matrix[][3]? I don't know if it would help or not, but it's worth a shot as I remember possibly encountering the same problem.

Yeah, now it says "initializing argument 2 of `void checkPlayerWin(bool&, char (*)[3])' "and"invalid conversion from `char' to `char (*)[3]' "

Simply pass the array in as matrix each time without any of the brackets. You've already designated that the function is taking an argument that's a matrix with 2 dimensions in the prototype and definition. When it comes to the definition of the function, the array being passed into a function degrades to a pointer. Passing in matrix[][N] gives a pointer to the array that holds each of the rows of the 2D matrix, not the entire thing.

Thanks much. I knew it was something silly & simple.
VERMUNDr

Simply pass the array in as matrix each time without any of the brackets. You've already designated that the function is taking an argument that's a matrix with 2 dimensions in the prototype and definition. When it comes to the definition of the function, the array being passed into a function degrades to a pointer. Passing in matrix[][N] gives a pointer to the array that holds each of the rows of the 2D matrix, not the entire thing.

The matrix works now. The board does not update and the computer can't play......

The matrix works now. The board does not update and the computer can't play......

shows move now, but still can't get it to go on to the computer's turn....... due soooon

update the code -,-? i tried it 5 minutes ago and it still has those compile errors

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.