Flxibit
Flxibit

Reputation: 39

Compare Char in an Array

For some HomeWork, I need to make a Tic-Tac-Toe game. I make the game with no problems, but now I need to control if a player as win. For that purpose, I need to compare some Char present in an array. So I set up a test with an "if", but it sends me an error (CS0019 in Visual Studio) that says I cannot compare char and expect a bool output. How do I circumvent that?

if ((casesMorpion[0, 0]) != (casesMorpion[1, 0]) != (casesMorpion[2, 0]))
{
  V1 = true;
}

Upvotes: 0

Views: 63

Answers (2)

Nachiappan Kumarappan
Nachiappan Kumarappan

Reputation: 291

I agree with @Corentin Pane answer. He answered just few seconds before me

Some thing like below could help

var expectedChar = casesMorpion[0, 0]; 
if ((expectedChar != casesMorpion[1, 0]) || (expectedChar != casesMorpion[2, 0]))
{
    V1 = true;
}

!= operation between two operands give a bool. so now it will become bool != char this will give a compilation error.

Upvotes: 1

Corentin Pane
Corentin Pane

Reputation: 4943

You cannot have (casesMorpion[0, 0]) != (casesMorpion[1, 0]) != (casesMorpion[2, 0]) on a single line and expect it to work.

You get your error because (casesMorpion[0, 0]) != (casesMorpion[1, 0]) is a bool, and you try to compare it to (casesMorpion[2, 0]) which is a char.

You should split it into two conditions with a logical and &&:

if (casesMorpion[0, 0] != casesMorpion[1, 0] && casesMorpion[1, 0] != (casesMorpion[2, 0]))
{
    V1 = true;
}

Or anything else, since I don't know exactly what you are trying to test.

Upvotes: 1

Related Questions