[英]C# Blackjack Game
I want to make my blackjack game give me a new card when i press my button Draw A Card (hit) 我想让我的二十一点游戏给我一张新卡,当我按下按钮Draw A Card(命中)时
private void btnDraw_Click(object sender, EventArgs e)
{
Random rdn = new Random();
int YourCardOne = rdn.Next(1, 10 + 1);
this.lblYourCardOne.Text = Convert.ToString(YourCardOne);
This is the code i have to draw one card, i want it to draw another card when i press the button again but i don't want it to change the first one. 这是我必须画一张卡的代码,当我再次按下按钮时我想让它画另一张卡,但是我不想让它更改第一张。
I tried doing this but it changed both cards whenever i pressed draw, 我尝试这样做,但是每当我按下平局时,它都会更改两张卡,
private void btnDraw_Click(object sender, EventArgs e)
{
Random rdn = new Random();
int YourCardOne = rdn.Next(1, 10 + 1);
this.lblYourCardOne.Text = Convert.ToString(YourCardOne);
Random rdn1 = new Random();
int YourCardTwo = rdn.Next(1, 10 + 1);
this.lblYourCardTwo.Text = Convert.ToString(YourCardTwo);
Maybe instead you could use a list of ints for your hand 也许相反,您可以使用整数列表
You could make your hand like this: 你可以像这样使你的手:
List<int> hand = new List<int>();
Then when the button is clicked add to the list: 然后,当单击按钮时,将添加到列表中:
private void btnDraw_Click(object sender, EventArgs e)
{
Random rdn = new Random();
hand.add(rdn.Next(1, 10 + 1));
Your best bet is to try and mimic the real world, instead of trying to apply programing patterns to your game. 最好的选择是尝试模拟现实世界,而不是尝试将编程模式应用于您的游戏。 Don't use a list of cards and randomly pick one.
不要使用卡片列表,而是随机选择一张。 You don't do that in a game of Blackjack, you always take the top card.
您不会在二十一点游戏中做到这一点,您总是拿最高的牌。 Which means you should have a single
Shuffle()
method that randomly mixes up the cards, and one that takes the top card off. 这意味着您应该有一个
Shuffle()
方法,该方法随机地混合卡片,而另一个方法则是去除顶部卡片。
Here's some example code I had from a gard game I did when I was just learning to program that may give you an example of what I mean for the shuffle. 这是我在学习编程时从Gard游戏中获得的一些示例代码,可能会为您提供我对洗牌意味着什么的示例。 After that, the act of getting the top card is straight forward.
在那之后,拿到顶牌的行为是直接的。
int deckSize;
public void Shuffle()
{
Random rand = new Random();
//Swap every card with a random card in the deck
for(int count = 0; count < deckSize; count++)
{
int index = rand.Next(deckSize);
Card swapee = cards[index];
cards[index] = cards[count];
cards[count] = swapee;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.