简体   繁体   中英

C# index number randomly generated and put into array to not occur again, still get the same result out of list with the number

class Cards
{
    private Random random = new Random();
    private List<Card> playerDeck = new List<Card>();
    private List<Card> computerDeck = new List<Card>();
    private List<int> usedPlayerNrs = new List<int>();
    private List<int> usedComputerNrs = new List<int>();

    public enum Card
    {
        AceClubs = 11,
        AceDiamonds = 11,
        AceHearts = 11,
        AceSpades = 11,
        DeuceClubs = 2,
        DeuceDiamonds = 2,
        DeuceHearts = 2,
        DeuceSpades = 2,
        EightClubs = 8,
        EightDiamonds = 8,
        EightHearts = 8,
        EightSpades = 8,
        FiveClubs = 5,
        FiveDiamonds = 5,
        FiveHearts = 5,
        FiveSpades = 5,
        FourClubs = 4,
        FourDiamonds = 4,
        FourHearts = 4,
        FourSpades = 4,
        JackClubs = 11,
        JackDiamonds = 11,
        JackHearts = 11,
        JackSpades = 11,
        KingClubs = 13,
        KingDiamonds = 13,
        KingHearts = 13,
        KingSpades = 13,
        NineClubs = 9,
        NineDiamonds = 9,
        NineHearts = 9,
        NineSpades = 9,
        QueenClubs = 12,
        QueenDiamonds = 12,
        QueenHearts = 12,
        QueenSpades = 12,
        SevenClubs = 7,
        SevenDiamonds = 7,
        SevenHearts = 7,
        SevenSpades = 7,
        SixClubs = 6,
        SixDiamonds = 6,
        SixHearts = 6,
        SixSpades = 6,
        TenClubs = 10,
        TenDiamonds = 10,
        TenHearts = 10,
        TenSpades = 10,
        ThreeClubs = 3,
        ThreeDiamonds = 3,
        ThreeHearts = 3,
        ThreeSpades = 3
    }

    public void createDecks()
    {
        playerDeck = Enum.GetValues(typeof(Card)).Cast<Card>().ToList();
        computerDeck = Enum.GetValues(typeof(Card)).Cast<Card>().ToList();
        usedPlayerNrs = new List<int>();
        usedComputerNrs = new List<int>();
    }

    public Card getPlayerCard()
    {
        int index = random.Next(playerDeck.Count());
        usedPlayerNrs.Add(index);
        while (usedPlayerNrs.Contains(index))
        {
            index = random.Next(playerDeck.Count());
        }
        Card randomCard = playerDeck[index];
        return randomCard;
    }

    public Card getComputerCard()
    {
        int index = random.Next(computerDeck.Count());
        usedComputerNrs.Add(index);
        while (usedComputerNrs.Contains(index))
        {
            index = random.Next(computerDeck.Count());
        }
        Card randomCard = computerDeck[index];
        return randomCard;
    }

}

I need to get a card from a deck of cards but I am only allowed to get it once. However when I put my randomly generated numbers into an array to exclude that number from occurring again I still get the same cards. Have tried alot already that's why the codes a bit messy at this point.

The reason you are getting the 'same result' is because you assigned identical values to multiple items in your enumeration. Instead, give each item a unique value.

public enum Card
{
   AceClubs = 11,
   AceDiamonds = 12,
   AceHearts = 13,
   AceSpades = ...,
}

In addition, you could check uniqueness of your sample by comparing the name of your enum item, not the value assigned to it. Like so:

var propertyName = typeof(Card).GetEnumName(Card.AceClubs);

The variables usedPlayerNrs and usedComputerNrs will always have unique values in it but playerDeck and computerDeck have duplicate values and you're fetching the card based on unique index as:

 Card randomCard = playerDeck[index]

since, playerDeck has duplicate values you're getting same card multiple times.

You've got an error in here:

public Card getPlayerCard(List<int> usedPlayerNrs, List<Card> playerDeck)
{
    int index = random.Next(playerDeck.Count());
    while (usedPlayerNrs.Contains(index))
    {
        index = random.Next(playerDeck.Count());
    }
    usedPlayerNrs.Add(index);//Only add your index to the userplayednumbers here!
    Card randomCard = playerDeck[index];
    return randomCard;
}

Also, look up a shuffling algorithm , it will help with getting a random collection of cards in a more efficient way.

And as has been said, having multiple enums with the same value is a code smell. Are you sure you don't want to number them 1-52? Or go for an object oriented design and add a number + Spades/Diamonds/... enum to that class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM