help not to repeat the same case of a switch inside a for (c#)

Asked

Viewed 151 times

1

I am in the first period of an SI course and I need to play a truce and I came across a situation that I am not able to solve. I need that during the loop I can not repeat the same case in the truco plays. Follow the method:

static void jogo()
{

    for (int jogada = 0; jogada<3; jogada++)
    {            
        if(jogador1jogada == 2 || jogador2jogada == 2)
        {
            break;
        }

        Console.WriteLine("Qual carta vc ira jogar? ");
        escolha = int.Parse(Console.ReadLine());
        switch (escolha)
        {
            case 1:
                Console.WriteLine("Voce jogou a carta: " + jogador1[0]);
                IAjogo();
                Console.WriteLine("A IA jogou: " + jogador2[escolhaIA - 1]);
                verificavitoria();                                              
            break;
            case 2:
                Console.WriteLine("Voce jogou a carta: " + jogador1[1]);
                IAjogo();
                Console.WriteLine("A IA jogou: " + jogador2[escolhaIA - 1]);
                verificavitoria();                    
            break;
            case 3:
                Console.WriteLine("Voce jogou a carta: " + jogador1[2]);
                IAjogo();
                Console.WriteLine("A IA jogou: " + jogador2[escolhaIA - 1]);
                verificavitoria();                    
            break;
        }

    }

}
  • The question is confused. Why durante o loop não pode repetir o mesmo case na jogadas? What about this? What’s your problem? What solution do you need?

  • Because your hand has 3 cards, when your hand plays a card you cannot play the same card again

  • I even understand this, but your question does not reflect this and from what I understood the answers also did not understand this and spoke of something else completely.

2 answers

0

Create a string array, and add value if the option has been chosen, before the switch make an if to check if the option has already been chosen, if it has already been, enter Else giving error message and putting the value of play -1 not to increment the move and keep repeating until the value is valid.

using System;

class MainClass {
  public static void Main (string[] args) {

    var test = new string[3];
    Console.WriteLine("Atribuir o valor quando o valor for escolhido dentro do case");
    Console.WriteLine(test[0] = "1");
    Console.WriteLine(test[1] = "2");
    Console.WriteLine(test[2]);

    Console.WriteLine("Ver se é nulo, se for, não há valores");
    Console.WriteLine(test[0] == null);
    Console.WriteLine(test[1] == null);
    Console.WriteLine(test[2] == null);
  }
}

In the example above I put the string 1 and 2 but did not put 3, which means that the player has already chosen 1 and 2, 3 is missing

Atribuir o valor quando o valor for escolhido dentro do case
1
2

Ver se é nulo, se for, não há valores
False
False
True

Then to see if you have already chosen the option just check if it is null, if it is not yet chosen, at the end of the for just put everything null again.

EDIT: Switch must be inside the IF, Else after switch.

  • I didn’t understand how to use this :(

0


Realize that the only thing that changes in each case is the value of X in Console.WriteLine("Voce jogou a carta: " + jogador1[X]). So just use only once this snippet of putting code escolha - 1 in place of X.

With this the case was eliminated. Later, you can fill the position of the vector whose card was downloaded with "-1" and check this before discarding.

for (int jogada = 0; jogada<3; jogada++)
{            
    if(jogador1jogada == 2 || jogador2jogada == 2)
    {
        break;
    }
    Console.WriteLine("Qual carta vc ira jogar? ");
    escolha = int.Parse(Console.ReadLine());

    //  evita a reutilização da carta
    if (jogador1[escolha - 1] == "-1") 
    {
        jogada--;
        continue;
    }

    Console.WriteLine("Voce jogou a carta: " + jogador1[escolha -1]);
    jogador1[escolha - 1] = "-1" // marca a carta como já utilizada
    IAjogo();
    Console.WriteLine("A IA jogou: " + jogador2[escolhaIA - 1]);
    verificavitoria();                                              
}
  • I tried to replace here but it is not working because it is giving error claiming that it is not possible to convert type int into string nor to buy string and int

  • Ah, player1[ ] stores strings. Record some string, example "-1" or "used", signaling the use of the card in the two places where I previously suggested the use of -1 (example player1[choice - 1] == "-1" and player1[choice - 1] = "-1"). I changed the answer there.

  • agr spun but is only spinning once, at the time to hit the second letter it closes

  • Oh yes, there is another problem, when the discard of a card fails, we have to repeat the variable played (move++ before the continue). The break at the end I had to have removed, I corrected in the reply too. Due to the break I forgot at the end it was coming out of the Loop.

  • really agr is not giving to hit the same letter, thank you very much, however the program is closing if I try to hit the msm, I put inside the if a writeline for him to choose another card but the program does not continue in the same way, and I keep throwing the msm letter, but then I guess I have to change the way of AI that I haven’t touched yet?

  • Dude, I did it all wrong, where I put the++ move. It was wrong.

  • 1

    now gave certin vlwwws!!!

  • As for the game I figured you would have some problem. I would think about creating a base class Player and two subclasses Player and Human Player. The loop that performs the various hands of the game would be out of these classes, in another Game class for example.

  • Think in terms of entities. Who knows how you want to play is a player, but who controls the dynamics of the game is a series of rules that are independent of who the player is. If you think I helped, validate the answer there. Valeus.

  • I get it, I still don’t know how to use classes but you helped a lot I’ll see what I can do here. How do I validate the answer?? I can’t do that, first time around

  • I don’t know, I’m new here, I never asked a question. Somehow you can accept the answer as valid. She turns up with a green V on her side.

  • I think it was vlwws!!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.