卡处理二十一点问题 [英] Card dealing blackjack problem

查看:61
本文介绍了卡处理二十一点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用Windows Forms Apps用C#编写一个二十一点程序,我已经能够创建卡片组,随机播放并分发卡片以使它们出现在列表框中.我遇到的问题是发牌人向玩家和发牌人都发出了相同的两张牌.在使用两副牌给发牌人和一副牌给发牌人之前,我已经使它起作用了,但是有时候我会得到同一张牌.我需要弄清楚如何使用一副牌并获得4张卡.我相信我的问题不是在显示卡片的foreach循环中,就是在Form 1加载中,请看一下任何帮助,将不胜感激:
这是我认为是错误的两部分的代码:

Hello, I am writing a blackjack program in C# using Windows Forms Apps, I have been able to create the deck, and shuffle, and deal out cards to make them appear in a listbox. The problem that I am having is the dealer deals the same two cards out to both the player and the dealer. I had it working before using two decks one for the dealer and one for the player, however sometimes I would get the same card. I need to figure out how to use one deck and get it to deal 4 cards. I believe that my problems are either in the foreach loops to display the cards, or in the Form 1 load, please take a look any help would be greatly appreciated:
Here''s my code for the two parts I believe to be wrong:

public partial class Blackjack : Form
   {


       string[] card = new string[53];
       string[] playerHand = new string[10];
       string[] dealerHand = new string[10];

       public Blackjack()
       {
           InitializeComponent();
       }


       private void Blackjack_Load(object sender, EventArgs e)
       {

           int top = 1;
           CreateDeck(card);
           RandomizeDeck(card);
           RandomizeDeck(card);



           string[] player = new string[10];
           string[] dealer = new string[10];
           for (int i = 0; i < 10; i++)
               dealer[i] = "";

           GetCard(dealer, card, ref top);     //// this is where I believe
           GetCard(dealer, card, ref top);     //// part of the problem is
           while (HValue(dealer) < 17)
               GetCard(dealer, card, ref top);
           GetCard(player, card, ref top);
           GetCard(player, card, ref top);

       }
      //// I believe the main problem is here with the two foreach loops

      //// Is there a better way to display the cards in the listboxes?

       private void btnStart_Click(object sender, EventArgs e)
       {
           {
               int countPlayer = 0;
               int countDealer = 0;

               foreach (string a in card)
               {

                   if (a != null)
                   {
                       string b = a;
                       for (int i = 0; i < playerHand.Length; i++)
                       {
                           if (playerHand[i] == null)
                           {
                               playerHand[i] = b;
                               break;
                           }
                           if (countPlayer == 2)
                           break;
                           this.lstYou.Items.Add(b);
                           countPlayer++;
                       }


                   }
               }
               foreach (string a in card)   /// this is where i believe
               {                            /// my problem is
                   if (a != null)
                   {
                       string b = a;
                       for (int i = 0; i < dealerHand.Length; i++)
                       {
                           if (dealerHand[i] == null)
                           {
                               dealerHand[i] = b;
                               break;
                           }
                           if (countDealer == 2)
                               break;
                           this.lstDeal.Items.Add(b);
                           countDealer++;


                       }
                   }
               }
           }

       }
 }
       }

推荐答案

问题出在两个foreach循环中,您从同一卡座中取出卡,但没有从卡座中取出卡,所以最终结果是这样.

交易玩家
-赠送顶级卡的玩家副本
-提供第二张卡的玩家副本

交易经销商
-给经销商顶卡
-提供第二张卡的经销商副本

您需要能够从卡组中取出卡,否则将始终遇到此问题.我建议使用ArrayList而不是Array,因为可以编辑ArrayList.
The problem is in the two foreach loops, you deal out the cards from the same deck but you do not remove the cards from the deck, so it ends up going like this.

Deal Player
-Give player copy of top card
-Give player copy of 2nd card

Deal Dealer
-Give dealer copy of top card
-Give dealer copy of 2nd card

You need to be able to remove the cards from the deck otherwise you will always have this problem. I would suggest using an ArrayList instead of an Array as the ArrayList can be edited.


将数组中的每张卡分配给其相应图像的路径.例如,您可以在c#
中使用Dictionary<t,k> 例如;
Assign each card in the array to the path of it''s appropriate image. You could for an example use a Dictionary<t,k> in c#
eg;
Dictionary<Card,string> cards = 
            new Dictionary<Card,string>();
cards.Add(new Card {Value = 2, Face = diamond}, "di2.jpg");


其余的逻辑取决于您.这只是一个入门的简单想法.顺便说一句,Card是一个自定义类.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx [ ^ ]


The rest of the logic is down to you. this is just a simple idea to get you started. And Card is a custom class by the way.
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]


这篇关于卡处理二十一点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆