Results 1 to 6 of 6

Thread: Problem in representing 3 x 3 array of char in c++

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    Problem in representing 3 x 3 array of char in c++

    Hello to all,
    I recently started learning c++ language. I have to represent a tic-tac-toe board using 3 x 3 array of char. I tried following code, but it is not working.
    Code:
    if ((boards[0][0] == boards[0][1]) && (boards[0][1] == boards[0][2])) { ... }
    I don't know what is wrong in that. I got problem in representing 3 x 3 array of char. Please help me to fix this problem.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in representing 3 x 3 array of char in c++

    From your information it seems that you have 9 values, three states each. To do this you have to use int data type. You have to define variable in following ways:
    Code:
    lmr = lastMoveRow
    lmc = lastMoveCol
    In the following there is no need to check blank with last move known.
    Code:
    if (boards[0][lmc] == boards[1][lmc] && boards[0][lmc] == boards[2][lmc] ||
        boards[lmr][0] == boards[lmr][1] && boards[lmr][0] == boards[lmr][2]){
          }
    After this you have to check diagonals like this:
    Code:
    if (boards[1][1] != blank &&
       (boards[0][0] == boards[1][1] && boards[0][0] == boards[2][2] ||
        boards[2][0] == boards[1][1] && boards[2][0] == boards[0][2])){}

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem in representing 3 x 3 array of char in c++

    You have to check all states to get rid out of this problem. I have written following code for you. Just try to understand this code. In the following code I have use for loop to execute counter for three times.
    In the following code I have check horizontals and verticals at once.
    Code:
    for (int k = 0; k < 3; ++k){
     
        if (boards[0][k] != blank && boards[0][k] == boards[1][k] && boards[0][k] == boards[2][k] ||
            boards[k][0] != blank && boards[k][0] == boards[k][1] && boards[k][0] == boards[k][2]){
    
        }
    }
    //Now check diagonals
    
    if (boards[1][1] != blank &&
       (boards[0][0] == boards[1][1] && boards[0][0] == boards[2][2] ||
        boards[2][0] == boards[1][1] && boards[2][0] == boards[0][2])){
    
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem in representing 3 x 3 array of char in c++

    You have written wrong code and that's why you are getting such type of problem. You have use parenthesis in wrong way and that's why you are getting such type of problem. In this case you have to remove parenthesis, because "&&" has lower priority than "=="
    Code:
    if (boards[0][0] == boards[0][1] && boards[0][1] == board[0][2])
    You also have to define an inline function to determine equality.
    Code:
    inline bools are_equal(int as, int bs, int cs) {
      return as == bs && bs == cs;
    if (are_equal(boards[0][0], boards[0][1], boards[0][2]))}

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problem in representing 3 x 3 array of char in c++

    You have to use if loop to fix this problem. I have written following code for you. Just try to understand this. In the following code I have use for loop to execute matrix for three times. In the following code I have check check all of the rows:
    Code:
    for(int k = 0; k < 3; k++){
        if((board[k][0]==board[k][1]) && (board[k][1]==board[k][2])){
            ....
        }
    }
    In such way you have to do for columns.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem in representing 3 x 3 array of char in c++

    As per my knowledge you have written wrong code and that's why you are getting such type of problem. I have written working program for you. Just try to understand this. It is very simple code. In the following code I have use two for loop to get solution.
    Code:
    bool theresIssAsLines(char matrixs[3][3])
    {
        char cs;
        
        for(int k = 0; k < 3; k++)
        {
            cs = matrix[k][0];
            if (cs == empty)
                break;
            if (cs == matrix[k][1] && c == matrix[k][2])
                return true;
        }
    
        for(int k = 0; k < 3; k++)
        {
            cs = matrix[0][k];
            if (cs == empty)
                break;
            if (cs == matrix[1][k] && cs == matrix[2][k])
                return true;
        }
      
        cs = matrix[1][1];
        if (cs == empty) return false;
        if (cs == matrix[0][2] && cs == matrix[2][0] )
            return true;
        if (cs == matrix[0][0] && c == matrix[2][2] )
            return true;
        return false;
    }

Similar Threads

  1. Array of char and int
    By Jensen Ackles in forum Software Development
    Replies: 5
    Last Post: 23-03-2010, 09:50 AM
  2. Comparing char array in c++
    By MAHAH in forum Software Development
    Replies: 5
    Last Post: 15-02-2010, 08:19 PM
  3. Randomaccessfile using char array
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 11:19 AM
  4. Size of a pointer to an array of char
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 12:05 PM
  5. How do i clear char array in c++
    By B_Hodge in forum Software Development
    Replies: 3
    Last Post: 16-05-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,578,503.65511 seconds with 17 queries