0

I am working on a program in C# where you have a button, and it will choose two random cards from a pre-determined selection. For example, I'm starting off with 5 cards that will be chosen from: the Ace of Spades, Seven of Spades, Ace of Hearts, Five of Clubs and Two of Diamonds. Now it will compare these two cards determine which card is higher and will make it be the winner, with Spades > Hearts > Clubs > Diamonds and Ace being low. So a Ace of Spades will win over a Two of Diamonds. I have made a CardClass where I set the Suits and Ranks:

public enum Suits
{
    Spades, 
    Hearts, 
    Clubs, 
    Diamonds
}
public enum Ranks
{
    Ace,
    _2,
    _3,
    _4,
    _5,
    _6,
    _7,
    _8,
    _9,
    _10,
    Jacks,
    Queen,
    King
}    
//Private instance variables suit and rank
    private Suits suit;
    private Ranks rank;

    //Provide properties for suit and rank that insure they can only be set to valid values
    public Suits Suit
    {
        get
        {
            return this.suit;
        }
        set
        {
            this.suit = value;
        }

    }

    public Ranks Rank
    {
        get
        {
            return this.Rank;
        }

        set
        {
            this.rank = value;
        }
    }
    //Provide a default constructor (no parameters) that sets the card to Ace of Spades.
    public CardClass()
    {
        // Use the properties to set these values
        this.Rank = Ranks.Ace;
        this.Suit = Suits.Spades;
    }


    // Provide an explicit constructor with parameters for ALL instance variables.
    public CardClass(Ranks rank, Suits suit)
    {
        //Use the properties to set these values
        this.Rank = rank;
        this.Suit = suit;
    }

Now for my button click, I set it so it will choose 2 different numbers, one for player 1 and one for player 2:

{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int play1choice, play2choice;

    private void btnCard_Click(object sender, EventArgs e)
    {
        Random player1 = new Random();
        int Player1 = (player1.Next(5) + 1);
        Random player2 = new Random();
        int Player2 = (player2.Next(5) + 1);


    }
}

}

Is there a way to set certain cards for the numbers? So if the random number generator picks 1 it will assign it to Ace of Spades, and 2 make it Seven of Spades, etc. ?

Jkmama52
  • 1
  • 2
  • Would I put that in the FormLoad or somewhere else? But I need 2 random numbers, one for player 1's card and onefor player 2's card choice. how would I then make it so it stores 2 different random numbers – Jkmama52 Feb 13 '17 at 03:52
  • Your question isn't very clear. Why are there only five cards to start with? Wouldn't it make more sense to follow one of the many existing examples of representing and shuffling a full deck of 52 cards, and then just pick the first two cards from that deck? Or at worst, shuffle the full deck, pick the first five cards, and then pick the first two of those five? Or are you saying you always start with the same, pre-determined set of five cards? In any case, your current approach is flawed (as is the posted answer) because you do nothing to prevent the same card from being picked twice. – Peter Duniho Feb 13 '17 at 04:04
  • For starters, I am only having the two cards being picked getting picked from a partial deck, consisting of those 5 cards. Maybe eventually i will make it choose from the first deck, but for now I just want it to be picked from those 5 – Jkmama52 Feb 13 '17 at 04:09
  • Then you just need to start with a "deck" that has just those five cards, and shuffle it using the standard uniform shuffling algorithm. See marked duplicate for details. Once the deck is shuffled you don't need any more random numbers. Just select the first and second elements in your deck collection (list, array, whatever). – Peter Duniho Feb 13 '17 at 04:40

1 Answers1

0

Create a method that will generate the five initial cards (see GetFiveInitialCards):

public enum Suits
{
    Spades,
    Hearts,
    Clubs,
    Diamonds
}
public enum Ranks
{
    Ace,
    _2,
    _3,
    _4,
    _5,
    _6,
    _7,
    _8,
    _9,
    _10,
    Jacks,
    Queen,
    King
}

public class CardClass
{

    public static CardClass[] GetFiveInitialCards()
    {
        return new CardClass[] {
            new CardClass(Suits.Spades, Ranks.Ace),
            new CardClass(Suits.Spades, Ranks._7),
            new CardClass(Suits.Hearts, Ranks.Ace),
            new CardClass(Suits.Clubs, Ranks._5),
            new CardClass(Suits.Diamonds, Ranks._2)
        };
    }

    public Suits Suit { get; }
    public Ranks Rank { get; }

    public CardClass(Suits suit, Ranks rank)
    {
        this.Suit = suit;
        this.Rank = rank;
    }
}


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    int play1choice, play2choice;

    private readonly Random rnd = new Random();

    private void btnCard_Click(object sender, EventArgs e)
    {
        CardClass[] initialCards = CardClass.GetFiveInitialCards();

        int Player1 = (rnd.Next(5) + 1);
        CardClass player1Card = initialCards[Player1];

        int Player2 = (rnd.Next(5) + 1);
        CardClass player2Card = initialCards[Player2];

    }
}
Serge
  • 3,986
  • 2
  • 17
  • 37