I have a code like
if (config_atual[6]==config_atual[7]==config_atual[8] ||
config_atual[1]==config_atual[4]==config_atual[7] ||
config_atual[2]==config_atual[4]==config_atual[6])
{
if (config_atual[7]=='X')
cout << "O Jogador ganhou!" << endl;
else if (config_atual[7]=='O')
cout << "O Computador ganhou!" << endl;
}
about a tic-tac-toe game, and whenever I try to compile this line of code, which verifies three of the win conditions (assuming indexes 0,1,2 for first line, 3,4,5 for second and 6,7,8 for last) I get this warning :
suggest parentheses around comparison in operand of '==' [-Wparentheses]
Which I don't understand. What I am doing wrong, config_atual
is a char array that contains the present configuration of the playing board.
What does this warning mean, and how can I correct it?
The expression config_atual[6]==config_atual[7]==config_atual[8]
is grouped as (config_atual[6]==config_atual[7])==config_atual[8]
. The part in parentheses is either true
or false
which is implicitly converted to an int
type (or the type of config_atual[8]
is that's wider than an int
) prior to a second comparison; i.e. config_atual[8]=={1, 0}
, where I've used what's in {}
to indicate the possibilities. That probably ends up as false
.
Occasionally the chaining of ==
in this manner is useful. But in your case it isn't, and you need to write the expression in a different way;
config_atual[6]==config_atual[7] && config_atual[7]==config_atual[8]
is one such way.