|
| ||||||||||
| Tags: c language, class, file handle, function, object |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Problem in representing 3 x 3 array of char in c++
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])) { ... } Thank you. |
|
#2
| ||||
| ||||
| 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 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]){
} 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
| ||||
| ||||
| 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])){
}
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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]) 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 |
|
#5
| ||||
| ||||
| 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])){
....
}
} |
|
#6
| ||||
| ||||
| 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;
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |