无法获得循环数组

时间:2015-03-24 21:28:57

标签: c#

基本上我想将J与下一张卡片进行比较,问题是我每次都会列出一个列表来获取它,我立刻得到全部13而不是一张。 我的目标是从牌组(52)获得一张牌,给他们最高值1-13(13),并在每次抽出牌时对它们进行比较。

internal class Kort
{
    //datamedlemmar
    public int siffra;
    public Image bild;

    //konstruktor
    public Kort(int nr, Image card)
    {
        this.siffra = nr;
        this.bild = card;
    }
}

public partial class Form1 : Form
{
    private Kort old; //gamla kortet
    private Kort ny; //nuvarande
    private Kort[] kortlek = new Kort[52]; //Kort array
    private Random slump = new Random(); //Randomizer
    private bool[] usedPictures; //Bool array gjord för att kolla fall korten har dragits
    private int plats = 0;
    private List<int> myList = new List<int>();
    private List<int> myList2 = new List<int>();
    private List<int> myList3 = new List<int>();
    private List<int> myList4 = new List<int>();

    public Form1()
    {
        InitializeComponent();

        for (int i = 0, j = 1; i < 13; i++, j++)
        {
            myList.Add(j);
            kortlek[i] = new Kort(j, Image.FromFile("Bilder/h" + j + ".png"));
        }

        for (int i = 13, j = 1; i < 26; i++, j++)
        {
            myList2.Add(j);
            kortlek[i] = new Kort(j, Image.FromFile("Bilder/c" + j + ".png"));
        }

        for (int i = 26, j = 1; i < 39; i++, j++)
        {
            myList3.Add(j);
            kortlek[i] = new Kort(j, Image.FromFile("Bilder/d" + j + ".png"));
        }

        for (int i = 39, j = 1; i < 52; i++, j++)
        {
            myList4.Add(j);
            kortlek[i] = new Kort(j, Image.FromFile("Bilder/s" + j + ".png"));
        }

        usedPictures = new bool[kortlek.Length];
        BackgroundImage = Image.FromFile("Bilder/deck_background.png");
    }

    private void btnDraKort_Click(object sender, EventArgs e)
    {
        plats = slump.Next(1, usedPictures.Length);

        //kontrollera om kortet dragits mha usedPictures
        while (usedPictures[plats])
        {
            plats = slump.Next(1, usedPictures.Length);
        }

        ny = kortlek[plats];
        usedPictures[plats] = true;

        if (old == null)
        {
            old = ny;
        }

        pbxNy.Image = (Image) ny.bild;
        pbxOld.Image = (Image) old.bild;
        old = ny;

        if (rbnHogre.Checked == true)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

不确定这是否能回答您的问题,但希望它有所帮助。我认为问题的一部分可能是有很多东西要跟踪,你可能会失去状态?无论如何,遵循代码有点困难。

一个建议是跟踪卡片对象本身的套装,而不是为它们提供4个单独的列表。

我的另一个建议是只有一个或两个卡片列表:一副卡片,最初填充52张卡片,以及一张拉出的卡片列表(完全可选,但如果您需要搜索,可能会很方便已经绘制的卡片。)

注意:我已经复制了您的代码,但我自己将其翻译成英文。

首先,我创建了一个代表诉讼的枚举:

public enum Suit
{
    Hearts, Clubs, Diamonds, Spades
}

然后修改了Card类,在构造函数中包含一个Suit,并根据value和suit自动选择正确的图像,而不是让客户端执行:

internal class Card
{
    public int Value { get; set; }
    public Image Picture { get; private set; }
    public Suit Suit { get; set; }

    public Card(int value, Suit suit)
    {
        this.Value = value;
        this.Suit = suit;

        // Load the correct picture automatically based on value and suit
        string firstLetterOfSuit = suit.ToString().First().ToString();
        string picturePath = string.Format("Pictures/{0}{1}.png", 
            firstLetterOfSuit, value);
        this.Picture = Image.FromFile(picturePath);
    }
}

接下来,我创建了一种方法,它将返回一副新牌。这可以在您加载表单时调用,也可以在您想要重置表单时调用:

private static List<Card> GetFreshDeck()
{
    var freshDeck = new List<Card>();

    for (int i = 0; i < 52; i++)
    {
        // Value is always 1 through 13
        int value = i % 13 + 1;

        // Determine suit based on i
        Suit suit = (i < 13) ? Suit.Hearts 
            : (i < 26) ? Suit.Clubs 
            : (i < 39) ? Suit.Diamonds 
            : Suit.Spades;

        // Add a card to the deck
        freshDeck.Add(new Card(value, suit));
    }

    return freshDeck;
}

然后你的部分Form类的其余部分变为:

public partial class Form1 : Form
{
    private Card oldCard;
    private Card currentCard;

    private List<Card> cardDeck = new List<Card>();
    private List<Card> drawnCards = new List<Card>();
    private Random random = new Random();

    public Form1()
    {
        InitializeComponent();
        BackgroundImage = Image.FromFile("Pictures/deck_background.png");
        cardDeck = GetFreshDeck();
    }

    private static List<Card> GetFreshDeck()
    {
        // Code already supplied above
    }

    private void btnDrawCard_Click(object sender, EventArgs e)
    {
        // Pick a random index from the card deck
        int cardIndex = random.Next(0, cardDeck.Count);

        // Get the card, add it to drawn cards, and remove it from the deck
        Card currentCard = cardDeck[cardIndex];
        drawnCards.Add(drawnCard);
        cardDeck.RemoveAt(cardIndex);

        if (oldCard == null)
        {
            oldCard = currentCard;
        }

        pbxCurrnet.Image = currentCard.Picture;
        pbxOld.Image = oldCard.Picture;
        oldCard = currentCard;

        // If we drew the last card, disable this button
        if (!cardDeck.Any()) btnDrawCard_Click.Enabled = false;
    }
}