按下按钮随机播放扑克牌

时间:2017-02-13 03:45:20

标签: c# random comparison

我正在使用C#编写一个程序,你有一个按钮,它将从预先确定的选择中选择两张随机卡。例如,我开始选择5张牌:黑桃王牌,黑桃七张,红心王牌,五张俱乐部和两枚钻石牌。现在它将比较这两张牌确定哪张牌更高并且将使其成为赢家,使用黑桃>心>俱乐部>钻石和王牌低。因此,黑桃王牌将赢得两枚钻石。我已经制作了一个CardClass,我设置了套装和等级:

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;
    }

现在点击我的按钮,我将其设置为选择2个不同的数字,一个用于玩家1,一个用于玩家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);


    }
}

}

有没有办法为这些数字设置某些卡?因此,如果随机数发生器选择1,它会将它分配给黑桃王牌,2则将它分成七个黑桃等等?

1 个答案:

答案 0 :(得分:0)

创建一个生成五张初始牌的方法(参见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];

    }
}
相关问题