Help in comparison of randomized variable

Asked

Viewed 110 times

0

I’m making a game of colors, in it I have a Random that randomizes buttons in a certain sequence.

The value of these buttons are concatenated in the variavelX that sets my given sequence.

ex:

  botão1 = "1";

  botão2 = "2";

  botão3 = "3";

  botão4 = "4";

variavelX = 32131 // variavelX recebe a ordem da sequência randomizada, que no exemplo foi 32131

I need to make the user of my system press the buttons that were drawn, that is, in the same randomized order and if right, assigns 10 points in the label lblPontuacao

Example:

variavelX = 32131
// Jogador precisa clicar na seguinte sequência, 3 (botão3), 2 (botão2), 1 (botão1), 3 (botão3), 1 (botão1), e se ele acertar, é atruibuido 10 pontos

I have already debug and I have noticed that the variables are passing the values correctly but I am doubtful how I will compare these variables, and how will be the if to check if they are correct.

The logic I’m using is the following:

This is the comparison I’m using to see if the user hit the sequence, but it’s wrong

if (botao.Name == "btnAmarelo")
        {
                ordem1 = ordem;
                if (ordem1 == ordem)
                {
                    lblPontuacao.Text = "10";
                }
        }

This is the timer I use to blink colors (there is not the whole timer, only a part)

private void timerCores_Tick(object sender, EventArgs e)
    {

        Random random = new Random();
        int rand = random.Next(1, 13);

        if (rand == 1)
        {
            timerCores.Start();
            Cores(btnAzul);
            timerCores.Stop();
            lblPreparado.Text = "Clique na sequência";
        }
        else if (rand == 2)
        {
            Cores(btnVermelho);
            timerCores.Start();

        }
        else if (rand == 3)
        {
            Cores(btnVerde);
            timerCores.Start();

        }
    }

And this is the method that assigns a value to the flashing colors.

    public void Posicao(Button botao)
    {

        if (botao.Name == "btnAzul")
        {
            ordem = ordem + "a";
        }
        if (botao.Name == "btnVermelho")
        {
            ordem = ordem + "b";
        }
        if (botao.Name == "btnVerde")
        {
            ordem = ordem + "c";
        }
        if (botao.Name == "btnAmarelo")
        {
            ordem = ordem + "d";
        }

    }
  • 2

    The information is very loose, it’s hard to understand what you did and where you’re going. You should [Dit] the question including more details and mainly what you did. Give us as much relevant information as you can so we can help you.

  • 2

    My suggestion is to use something like a list (or array) to represent your sequence, not a number. You you can even encode a sequence into a number (eg.: 1243 = 1*1000 + 2*100 + 4*10 + 3) and use module operations/rest to access individual items, but in my opinion this would complicate for nothing... (unless you are already doing this, of course - what kind of variavelX?)

1 answer

1


Felipe, I believe you are creating an electronic version of the famous game "Genius"! If this is the case, maybe the algorithm used in the solution is taking you a long way.

When we talk about sequence, they all lead us to think about these kinds of arrangements in memory: rows, stacks and lists. Since we want to upload information that will be used in the form "first random number placed on it should be the first number used", then the organization we are looking for is the pile.

In C#, the object that implements it is the Queue. To add a number to the stack, we call the function .Enqueue(), and to use/take information, we use the function .Dequeue().

Here’s an example: suppose you want to create a sequence of N times (let’s assume 4 different buttons, for 4 different colors, as in Genius):

 // nNumeros = quantos números vai ter a sequencia
 Queue CriarSequencia(int nNumeros, int nCombinacoes)
 {
     Random rand = new Random();
     Queue sequencia = new Queue();
     for(int i = 0; i < nCombinacoes; i++) 
     {
         sequencia.Enqueue(rand.Next(1, nCombinacoes));
     }
     return sequencia;
  }

This list "queues" a sequence of nNumeros, the combination of which is between 1 and nCombinacoes (in the case of 4-color Genius, nCombinacoes = 4).

When the user clicks a button, you need to see if the number of the button he clicked is next in the queue. So you check like this:

   // usuário clicou no numero X
   int proximoFila = (int)sequencia.Dequeue();
   if(x == proximoFila)
   {
       // usuário acertou...
       if(sequencia.Count == 0)
       {
            // usuário acertou todos!!!! dar mensagem de parabéns!
       }
    }
    else
    {
        // usuário errou... ele perdeu
    }

I hope it helps you.

Browser other questions tagged

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