syd_buet
syd_buet

Reputation: 19

C# Randomizing multiple images in multiple picture boxes

Hi guys i am trying to make a card game and facing some problems in distributing the 32 images of cards in 32 picture box but i cannot write my code on how to make the list of images by their names. I have tried my code using button as cards and randomly distribute them among the players and it was a success but when i am trying to do it in case of images i cant get it right I am posting my both code here.

class CardsDeck
{
    public static Random r = new Random();

    private static List<string> cards = new List<string>{ "♣ King", "♣ Queen", "♣ Jack", " ♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10",
                                                          "♦ King", "♦ Queen", "♦ Jack", " ♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10",
                                                          "♥ King", "♥ Queen", "♥ Jack", " ♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10",
                                                          "♠ King", "♠ Queen", "♠ Jack", " ♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" };
    public string ReceiveCards()
    {
        if (cards.Count > 0)
        {
            int index = r.Next(cards.Count);
            var card = cards[index];
            cards.RemoveAt(index);
            return card;
        }
        else
        {
            return "";
        }
    }
}

And in the Main Form

public partial class mainForm : Form
{
    public mainForm()
    {
        InitializeComponent();

        CardsDeck cd = new CardsDeck();

        btnA1.Text = cd.ReceiveCards();
        btnA2.Text = cd.ReceiveCards();
        btnA3.Text = cd.ReceiveCards();
        btnA4.Text = cd.ReceiveCards();

        btnB1.Text = cd.ReceiveCards();
        btnB2.Text = cd.ReceiveCards();
        btnB3.Text = cd.ReceiveCards();
        btnB4.Text = cd.ReceiveCards();


        btnC1.Text = cd.ReceiveCards();
        btnC2.Text = cd.ReceiveCards();
        btnC3.Text = cd.ReceiveCards();
        btnC4.Text = cd.ReceiveCards();


        btnD1.Text = cd.ReceiveCards();
        btnD2.Text = cd.ReceiveCards();
        btnD3.Text = cd.ReceiveCards();
        btnD4.Text = cd.ReceiveCards();



    }

    private void btnTrump_Click(object sender, EventArgs e)
    {
        CardsDeck cd = new CardsDeck();


        btnA5.Text = cd.ReceiveCards();
        btnA6.Text = cd.ReceiveCards();
        btnA7.Text = cd.ReceiveCards();
        btnA8.Text = cd.ReceiveCards();

        btnB5.Text = cd.ReceiveCards();
        btnB6.Text = cd.ReceiveCards();
        btnB7.Text = cd.ReceiveCards();
        btnB8.Text = cd.ReceiveCards();

        btnC5.Text = cd.ReceiveCards();
        btnC6.Text = cd.ReceiveCards();
        btnC7.Text = cd.ReceiveCards();
        btnC8.Text = cd.ReceiveCards();

        btnD5.Text = cd.ReceiveCards();
        btnD6.Text = cd.ReceiveCards();
        btnD7.Text = cd.ReceiveCards();
        btnD8.Text = cd.ReceiveCards();
    }
}

Now in this case i am trying to do it with images instead of doing with string but i cant get my code right. Can anyone please help me in this regard

 class Card
{
    public static Random r = new Random();

    PictureBox[] picBox = new PictureBox[32]

    public static Bitmap[] pictures = new Bitmap[32];
    // What should i write here??

    bool[] usedPictures = new bool[pictures.Length];


}
public string ReceiveCards()
{
    int ICount = 0;
    while (iCount < pictures.Length)
    {
         int attempt = random.Next(0, pictures.Length);

         //Ensures you will only use an available picture
        if (usedPictures[attempt] == false)
        {            
             picBox[attempt].Image= pictures[iCount];
             doorUsed[attempt] = true;
             iCount++;
      }
 } 

Upvotes: 0

Views: 1050

Answers (2)

Richthofen
Richthofen

Reputation: 2086

Seems your approach would benefit from an Object-oriented perspective. Why not create a PlayingCard object; one which has an 'CardImage' property? That way instead of maintaining two separate lists (one of the card value, one of the card image), you just maintain a list of PlayingCards and you can access the .CardImage property?

public class PlayingCard
{
    public enum Suit { Spades, Hearts, Diamonds, Clubs }
    public enum Rank {King =13, Queen = 12, Jack = 11, Seven = 7, Eight = 8, Nine = 9 }

    public Suit CardSuit;
    public Rank CardRank;

    public Bitmap CardImage;
}

Then in your constructor parse the Suit and Rank to determine the image.

Create your collection as List<PlayingCard>

Upvotes: 0

Yuurga
Yuurga

Reputation: 9

You can use ImageList, add Images in ImageList in order according to List cars

`private static List<string> cards = new List<string>{ "♣ King", "♣ Queen", "♣ Jack", " ♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10",
                                                      "♦ King", "♦ Queen", "♦ Jack", " ♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10",
                                                      "♥ King", "♥ Queen", "♥ Jack", " ♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10",
                                                      "♠ King", "♠ Queen", "♠ Jack", " ♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" };

Than you can use card to get need index in ImageList.

You can see how to work with at http://msdn.microsoft.com/ru-ru/library/system.windows.forms.imagelist.aspx

Upvotes: 1

Related Questions