Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 20-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 20-02-2010
Member
 
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])){}
Reply With Quote
  #3  
Old 20-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,388
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])){

}
Reply With Quote
  #4  
Old 20-02-2010
Member
 
Join Date: May 2008
Posts: 2,378
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]))}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 20-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,844
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.
Reply With Quote
  #6  
Old 20-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,512
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in representing 3 x 3 array of char in c++"
Thread Thread Starter Forum Replies Last Post
Array of char and int Jensen Ackles Software Development 5 23-03-2010 09:50 AM
Comparing char array in c++ MAHAH Software Development 5 15-02-2010 07:19 PM
Randomaccessfile using char array TechGate Software Development 5 27-01-2010 10:19 AM
Size of a pointer to an array of char Zool Software Development 3 14-10-2009 12:05 PM
How do i clear char array in c++ B_Hodge Software Development 3 16-05-2009 09:35 AM


All times are GMT +5.5. The time now is 01:32 AM.