Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made this code for a blackjack game, it works fine, but even when I created an array of cards to display the type of cards each player has at the end, it is not doing so, it is just displaying who won, who lost and the total number of card, never displays the type of cards of each player. Any suggestion, please?
Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUI_BLACKJACK
{
    public partial class Form1 : Form
    {

        class Card
        {

            public string Name { get; set; }
            public int Value { get; set; }
            public string Suit { get; set; }

            Card[] DeckOfCards = new Card[]
            {

                    new Card{Name="two",Value=2,Suit="Heart"},
                    new Card{Name="three",Value=3,Suit="Heart"},
                    new Card{Name="four",Value=4,Suit="Heart"},
                    new Card{Name = "five", Value = 5, Suit = "Heart" },
                    new Card{Name = "six", Value = 6, Suit = "Heart" },
                    new Card{Name = "seven", Value = 7, Suit = "Heart" },
                    new Card{Name = "eight", Value = 8, Suit = "Heart" },
                    new Card{Name = "Nine", Value = 9, Suit = "heart" },
                    new Card{Name = "ten", Value = 10, Suit = "Heart" },

                    new Card { Name = "two", Value = 2, Suit = "Spades" },
                    new Card { Name = "three", Value = 3, Suit = "Spades" },
                    new Card { Name = "four", Value = 4, Suit = "Spades" },
                    new Card { Name = "five", Value = 5, Suit = "Spades" },
                    new Card { Name = "six", Value = 6, Suit = "Spades" },
                    new Card { Name = "seven", Value = 7, Suit = "Spades" },
                    new Card { Name = "eight", Value = 8, Suit = "Spades" },
                    new Card { Name = "Nine", Value = 9, Suit = "Spades" },
                    new Card { Name = "ten", Value = 10, Suit = "Spades" },


                    new Card { Name = "two", Value = 2, Suit = "Diamonds" },
                    new Card { Name = "three", Value = 3, Suit = "Diamonds" },
                    new Card { Name = "four", Value = 4, Suit = "Diamonds" },
                    new Card { Name = "five", Value = 5, Suit = "Diamonds" },
                    new Card { Name = "six", Value = 6, Suit = "Diamonds" },
                    new Card { Name = "seven", Value = 7, Suit = "Diamonds" },
                    new Card { Name = "eight", Value = 8, Suit = "Diamonds" },
                    new Card { Name = "Nine", Value = 9, Suit = "Diamonds" },
                    new Card { Name = "ten", Value = 10, Suit = "Diamonds" },


                    new Card { Name = "two", Value = 2, Suit = "Clubs" },
                    new Card { Name = "three", Value = 3, Suit = "Clubs" },
                    new Card { Name = "four", Value = 4, Suit = "Clubs" },
                    new Card { Name = "five", Value = 5, Suit = "Clubs" },
                    new Card { Name = "six", Value = 6, Suit = "Clubs" },
                    new Card { Name = "seven", Value = 7, Suit = "Clubs" },
                    new Card { Name = "eight", Value = 8, Suit = "Clubs" },
                    new Card { Name = "Nine", Value = 9, Suit = "Clubs" },
                    new Card { Name = "ten", Value = 10, Suit = "Clubs" },
             };
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            int dealer = random.Next(1, 10);

            int player = random.Next(1, 10);

            label4.Text = ($"Let's play a modified version of the card game blackjack," +
                $" The dealer is showing a :{ dealer}, the other card is hidden");

            label1.Text = ("you were dealt 2 cards that total " + player);
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            label4.Text = ("Do you want another card? Type [Y]es or [N]o");

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }
      
        
        public string PlayGame(int dealer, int player)
        {


            string response = "";
           
            if (player <= 21 && dealer > 21)
            {
                label1.Text = ($"You WON with {player}");
                label2.Text = ($"The dealer ended with {dealer}");
            }

            else if (player < 21 && dealer < 21 && dealer > player)
            {
                label1.Text = ($"You lost with {player} ");
                label2.Text = ($"The dealer ended with {dealer}");
            }

            else if (player < 21 && dealer < 21 && player > dealer)
            {
                label1.Text = ($"You WON with {player}");
                label2.Text = ($"The dealer ended with {dealer}");
            }

            else if (player > 21 && dealer < player)
            {
                label1.Text = ($"You lost with {player}");
                label2.Text = ($"The dealer ended with  {dealer}");
            }
            else if (player == 21 && dealer < player)
            {
                label1.Text = ($"You WON with {player}");
                label2.Text = ($"The dealer ended with {dealer}");
            }
            else if (player > 21)
            {
                label1.Text = ($"You lost with {player}");
                label2.Text = ($"The dealer ended with  {dealer}");
            }
            
            {
                return response;
            }
        }
            
            public void button3_Click(object sender, EventArgs e)
            {
                Random random = new Random();
                int dealer = random.Next(1, 10);

                int player = random.Next(1, 10);

                int randomNumber = random.Next(1, 10);
                player += randomNumber;

                PlayGame(dealer, player);

            }

            private void button4_Click(object sender, EventArgs e)
            {

                Random random = new Random();
                int dealer = random.Next(1, 10);

                int player = random.Next(1, 10);

                int randomNumber = random.Next(1, 10);
                player += randomNumber;

                PlayGame(dealer, player);
            }

            private void label2_Click_1(object sender, EventArgs e)
            {

            }

        
}   }                


What I have tried:

I have tried by adding {card} after {player} and after {dealer} at the label. text

label1.Text = ($"You WON with {player}{Card}");
label2.Text = ($"The dealer ended with {dealer} {Card}");
I have tried by adding {Suit} at the same positions above mentioned to see if at least the suit of cards are mentioned.
Posted
Updated 16-Sep-21 19:53pm

1 solution

There is no code in there to display cards: you would need to add it. I'd start by adding an override of ToString to your Card class - but frankly you need to use it first.

Your code to generate a card totally ignores your Card class, and looks like it was written in C and randomly translated to C# - it contains nothing to hold the two players "cards so far", just the total value, and can work with a deck containing more than 4 Aces for example. I'd stringy suggest you sit down and think about exactly what you have to do here, then design classes (note the plural, it's deliberate) to handle it. What you have is probably not a good starting place for this homework.
 
Share this answer
 
Comments
CPallini 17-Sep-21 2:10am    
5.
Diasalva5 17-Sep-21 10:04am     CRLF
Dear OriginalGiff Thank you for your answer and ideas. One more question, please: In addition to study hard and practice, how could I become a good programmer? Any tips ? Thanks
OriginalGriff 17-Sep-21 10:40am     CRLF
Not really - there isn't a short cut! THink of it like riding a bicycle: you can watch as much of the Tour de France on TV as you want, talk to as many bike riders as you can - but when you first get on a bike you are going to fall off. The reason is simple: you don't have the reflexes, the mindset to make the tiny adjustments that keep you and it upright without even having to think about them. Development is the same: you can copy'n'paste code, look at other peoples work, watch as many Youtube videos as you can stand - none of it will make you a better coder because you don't have the mindset which lets you know instinctively what to do. Look at the code you first showed us: did you actually write all that? Because I doubt it myself! :laugh:
Diasalva5 17-Sep-21 10:54am    
Why are you doubting yourself that I wrote all my code by my own effort? Do you know me? You are guessing very wrong, I wrote all of that code my self.
OriginalGriff 17-Sep-21 11:04am     CRLF
OK, Why? Why go to all the effort of creating a Card class, and then ignore it in favour of what looks like Console based C code?

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