2017-02-13 85 views
0

我正在C#中的一個程序中,您有一個按鈕,它將從預先確定的選擇中選擇兩張隨機卡。例如,我將從5張卡片開始,這些卡片將選自:黑桃王牌,黑桃王牌,愛心王牌,五個俱樂部和兩個鑽石。現在它會比較這兩張牌決定哪張牌更高,並且會使它成爲贏家,黑桃>心型>俱樂部>鑽石和王牌都很低。所以黑桃A將贏得兩顆鑽石。我已經做了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,它會將其分配給黑桃王牌,並使其成爲黑桃王牌等等。

+0

您應該將按鈕單擊事件以外創建只有一個'Random'類的實例,並做到這一點。然後只需在事件內部多次調用它。 –

+0

我會把它放在FormLoad或其他地方嗎?但我需要2個隨機數字,一個用於玩家1的卡牌,另一個用於玩家2的卡牌選擇。我將如何讓它,因此它存儲2個不同的隨機數 – Jkmama52

+0

你的問題不是很清楚。爲什麼只有五張牌開始?遵循現有的代表和洗牌全套52張牌的例子之一是否更有意義,然後從該牌組中挑選前兩張牌?或者在最糟糕的情況下,洗牌全套牌,挑選前五張牌,然後挑選這五張牌中的前兩張?還是你說你總是以同樣的,預先確定的五張牌開始?無論如何,你目前的做法是有缺陷的(因爲是張貼的答案),因爲你什麼都不做,以防止同一張卡被挑選兩次。 –

回答

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

    } 
}