'error CS0029: Cannot implicitly convert type 'bool' to '(bool valorA, bool valorB)' c#

I'm having a problem, I want to make a Switch Case that says who won, who lost or if they drew ( Venceu, Perdeu e Empatou).

I did it first with integer values, with int valueA and valueB, where if someone wins it's worth 1, whoever loses it's 0 and a tie, both are worth 1.

However, it ended up giving the error:

error CS0029: Cannot implicitly convert type 'bool' to '(int valueA, int valueB)'

So I redid using boolean values, 1 = true and 0 = false.

But the error still persists and I don't know how to fix it.

This is my code:

if(Input.GetKey(key1))     // Vence
        {
            animBlue.SetBool("AzulTiro", true);
            valorA = true;
        }
        else if(animRed.GetBool("VermelhoTiro") == true)   // Perde
        {
            animBlue.SetBool("AzulMorte", true);
            valorA = false;
        }
   


        if(Input.GetKey(key2))     // Vence
        {
            animRed.SetBool("VermelhoTiro", true);
            valorB = true;
        }
        else if(animBlue.GetBool("AzulTiro") == true)     // Perde
        {
            animRed.SetBool("VermelhoMorte", true);
            valorB = false;
        }



    switch (valorA, valorB)
    {
        case (valorA == true && valorB == false):
              Debug.Log("Azul Venceu");
              break;
        
        case (valorA == false && valorB == true):
              Debug.Log("Vermelho Venceu");
              break;

        case (valorA == true && valorB == true):
              Debug.Log("Empatou");
              break;
    }

I'm pretty sure the error is on the Switch but I don't know what it is



Solution 1:[1]

wrong syntax, you need

  switch (valorA, valorB)
    {
        case (true ,false):
              Debug.Log("Azul Venceu");
              break;
        
        case (false , true):
              Debug.Log("Vermelho Venceu");
              break;

        case (true, true):
              Debug.Log("Empatou");
              break;
    }

Solution 2:[2]

How about pattern matching if your C# is modern enough?

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  _ => "Empatou"
};

Debug.Log(msg);

I reasoned that a false/false would either not occur, or be a draw too. If it isn't then you can tweak the statement:

var msg = (valorA, valorB) switch {
  (true, false) => "Azul Venceu",
  (false, true) => "Vermelho Venceu",
  (false, true) => "Empatou",
  _ => null
};

if(msg != null) Debug...

Remember that this a value-providing statement rather than an action-taking one; you'll get complaints from the compiler if it isn't exhaustive (doesn't cover all possible input values) and the easiest way to make it so is to provide the "else" case _. Also because it's providing values rather than performing code, you might need to inspect the value if you're making a "do/don't" decision.

You can't use this to run actions that are void (assuming Debug.Log returns void) in the same way you can't put voids in a ternary; the ternary produces values:

//no
someBoolean? Debug.Log("yes") : Debug.Log ("no");

//yes
Debug.Log(someBoolean? "yes" : "no");

One trick you can pull, however, is to use a switch (or a ternary) to return a lambda that you then execute

Action a = (valorA, valorB) switch {
  (true, false) => () => Debug.Log("Azul Venceu"),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
};

a(); //execute the returned action, which may be the do-nothing action 

These switches do return a value; they each return an Action and later it can be invoked to carry out the logging

It can also be formed and executed in one line, though it starts to get a bit "wtf?":

((valorA, valorB) switch {
  (true, false) => (Action)(() => Debug.Log("Azul Venceu")),
  (false, true) => () => Debug.Log("Vermelho Venceu"),
  (true, true) => () => Debug.Log("Empatou")
  _ => () => {}
})();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Caius Jard
Solution 2