Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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++;


                       }
                   }
               }
           }

       }
 }
       }
Posted
Comments
wizardzz 2-Apr-12 17:34pm    
This wouldn't happen to be for a job interview would it?
ericsommerfelt 2-Apr-12 20:14pm    
No, actually a class assignment. I can figure out the hit, stay buttons myself, I'm just stuck on how to deal the cards. I not a strong enough programmer to be employed in it, I need direction so I can get better at it, I'm not so good at visualizing how the program should run and what needs to be done.

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.
 
Share this answer
 
Comments
ericsommerfelt 3-Apr-12 20:14pm    
Thank you very much, I am not asking for the code, just direction. I thought that the two for each loops were the problem, I'm not completely done, but I will try to implement the arraylist, but I am also going to keep the thread alive, just in case I come across another issue. But again, thank you very much!!!
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;
C#
Dictionary<Card,string> cards = 
            new Dictionary<Card,string>();
cards.Add(new Card {Value = 2, Face = diamond}, "di2.jpg");

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[^]
 
Share this answer
 
v2
Comments
ericsommerfelt 3-Apr-12 2:09am    
I'm not that advanced, and there are no picture icons. Is there a more simple way? I am thinking part of my problem is that there should be a deal method, and currently in the rest of my code there is none. I can send all the code but it's long, there are create, randomize and show deck. Would a deal method work so that the dealer and player do not display the same hands in the listbox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900