Help in randomized variable comparison

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 X variable that sets my particular 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 random order and if it hits, it 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 debugged and I have noticed that the variables are passing the values correctly but I am in doubt as to how I will compare those variables, and how will the if to verify that they are correct.

The logic I'm using is as follows:

This is the comparison I am using to see if the user has hit the sequence but is wrong

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

This is the timer I use to flash the colors (not the whole timer, just 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 blinking 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";
        }

    }
    
asked by anonymous 29.11.2014 / 20:16

1 answer

1

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

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

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

Here's an example: let's 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 , whose combination 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 to 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 this helps you.

    
30.11.2014 / 16:00