Shuffling playing cards

5

I need to make a kind of "Mini-Pife", but I can not create the method to shuffle the cards, can anyone help me?

public class Baralho
{
    public Carta[] Cartas { get; private set; }

    public Baralho()
    {
        this.Cartas = new Carta[52];

        int c = 0;

        for (int nIdx = 0; nIdx < 4; ++nIdx)
        {
            Naipe naipe = (Naipe)(nIdx + 1);

            for (int vIdx = 0; vIdx < 13; ++vIdx)
            {
                Valor valor = (Valor)(vIdx + 1);

                this.Cartas[c++] = new Carta()
                {
                    Naipe = naipe,
                    Valor = valor
                };
            }
        }
    }
    
asked by anonymous 07.12.2017 / 18:00

1 answer

5
public class Baralho
{
    public Carta[] Cartas { get; private set; }

    public Baralho()
    {
        this.Cartas = new Carta[52];

        int c = 0;

        for (int nIdx = 0; nIdx < 4; ++nIdx)
        {
            Naipe naipe = (Naipe)(nIdx + 1);

            for (int vIdx = 0; vIdx < 13; ++vIdx)
            {
                Valor valor = (Valor)(vIdx + 1);

                this.Cartas[c++] = new Carta()
                {
                    Naipe = naipe,
                    Valor = valor
                };
            }
        }
        this.Cartas = Embaralha(this.Cartas);
    }

    public Carta[] Embaralha(Carta[] baralho){
        Random rnd = new Random();
        return MyArray.OrderBy(x => rnd.Next()).ToArray();  
    }

}

Retired from here: link

Tip: I wanted to give you a tip that will help you streamline your studies / work with programming. Your situation is: you have Array of Cartas and you need to shuffle these Cartas ... You do not need to lock in Cartas but in the resources you are using ( Array ).

If every Carta is within a Array , then you need to randomize a Array what you have inside Array does not matter (in your case they are Cartas ) or you would have gained much more If you had searched for "How to randomize a Array " and would have already found the solution, you would not have to wait for an answer here. Well, it's not a negative criticism, but constructive I hope it helps.

    
07.12.2017 / 18:13