-1

Basically i want to compare J with the next card drawn, the problem is everytime i put a list to get it i instantly get all 13 instead of one. My goal is to get a card from a deck (52), give them values 1-13 (13) highest and compare them each time card is drawn.

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)
        {

        }
    }
}
BJ Myers
  • 6,617
  • 6
  • 34
  • 50
Ayy lamo
  • 31
  • 5
  • 2
    What human language is this? – Zack Mar 24 '15 at 21:31
  • Swedish, inb4 trolls. – Ayy lamo Mar 24 '15 at 21:33
  • You are asking the question in English, so could you provide your code and comments in English? It would make it possible to help you. – Zack Mar 24 '15 at 21:37
  • You probably want to ask this question on the code review http://codereview.stackexchange.com/ – Max Brodin Mar 24 '15 at 21:37
  • What are the 4 `myList` variables? Lists of cards for each suit? And where are you actually adding the cards of each suit to your main deck (is it called `kortlek`) ? – ryanyuyu Mar 24 '15 at 21:38
  • myList was my try, it's basically a list that gets all the J values which is the values of specific cards, the problem is it gets all 13. i is basically all cards instead of 13 split into 4 (four colors) – Ayy lamo Mar 24 '15 at 21:39
  • 2
    @MaxBrodin No, this does not belong on Code Review. Please read [Be careful when recommending Code Review to askers](http://meta.stackoverflow.com/questions/253975/be-careful-when-recommending-code-review-to-askers) – Simon Forsberg Mar 24 '15 at 21:39
  • Where are you comparing J with the next card drawn? What gets all 13? – CoderDennis Mar 24 '15 at 21:43
  • I don't, i tried to but it compares them all, one card will get drawn depending on the randomizer(which draws the entire stack (i or 52cards), J is basically up to 13 and split four times for each color, what i want to do is to compare the old card to the new drawn card (their J values). – Ayy lamo Mar 24 '15 at 21:46
  • 1
    Where would you like to "compare the old card to the new drawn card"? What have you actually tried? What did that attempt do? How was that different from what you wanted it to do? I don't see any code here that compares the current with the previously drawn card. (Aside: please see [Fisher](http://stackoverflow.com/a/1287572/3538012)-[Yates](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) shuffling algorithm for a _much_ better approach to selecting random cards from a deck) – Peter Duniho Mar 24 '15 at 21:56
  • There's 3 classes 1 array, one new and one old, a randomizer and a array bool. What i want is that the array contains all 52 cards and a image for each of them, the 52 cards is not the cards value at all, it's just their position, J is gonna be their value that's gonna be compared, that's what i can't do. The bool is there to make the cards drawn to true which will make them not get drawn again. – Ayy lamo Mar 24 '15 at 22:00
  • What about implementing IEquatable on your Card class so you don't need so many arrays to keep track of the state? – Rufus L Mar 24 '15 at 23:36

1 Answers1

0

Not sure if this answers your question, but hopefully it helps. I think part of the problem may be that there are so many lists of things to keep track of, that you may be losing state? At any rate, it is a little difficult to follow the code.

One suggestion would be to track the Suit in the Card object itself, instead of having 4 separate lists for them.

My other suggestion is to have only one or two card lists: A deck of cards, which is initially populated with the 52 cards, and a list of drawn cards (completely optional, but may be handy if you need to search through the cards that have already been drawn).

Note: I've copied your code, but translated it into English for my own ease.

First, I created an enum to represent the Suit:

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

Then modified the Card class to include a Suit in the constructor, and to automatically pick the correct image based on value and suit, rather than have the client do it:

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

Next, I created a method that will return a fresh deck of cards. This can be called when your form loads, or anytime you want to get reset the deck:

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

And then the rest of your partial Form class becomes:

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;
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • 1
    As for the value, you could do something like `Suit suit = (Suit)(i / 13 + 1);` for the suit. It would avoid this `if...else` chain. – Arthur Rey Mar 25 '15 at 01:49