0

こんにちはみんな私はカードゲームを作ろうとしていて、32枚の絵の箱にカードの32枚の画像を配布する際にいくつかの問題に直面していますが、名前で画像のリストを作成する方法についてのコードを書くことができません。ボタンをカードとして使用してコードを試し、プレーヤー間でランダムに配布して成功しましたが、画像の場合にそれを実行しようとすると、正しく理解できません。両方のコードをここに投稿します。

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 "";
        }
    }
}

そしてメインフォームで

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();
    }
}

この場合、文字列ではなく画像を使用して実行しようとしていますが、コードを正しく取得できません。誰かがこの点で私を助けてくれますか

 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++;
      }
 } 
4

2 に答える 2

1

ImageListを使用して、リストカーに従って順番にImageListに画像を追加できます

`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" };

カードを使用してImageListのニーズインデックスを取得するよりも。

作業方法については、http://msdn.microsoft.com/ru-ru/library/system.windows.forms.imagelist.aspxをご覧ください。

于 2012-07-31T13:44:11.493 に答える
0

あなたのアプローチは、オブジェクト指向の観点から恩恵を受けるようです。PlayingCard オブジェクトを作成してみませんか。「CardImage」プロパティを持つものは?そうすれば、2 つの個別のリスト (カードの値の 1 つとカードの画像の 1 つ) を維持する代わりに、PlayingCards のリストを維持するだけで、.CardImage プロパティにアクセスできますか?

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;
}

次に、コンストラクターでスーツとランクを解析して画像を決定します。

コレクションを List<PlayingCard> として作成します

于 2012-07-31T15:19:07.720 に答える