1

I'm developing a basic poker game (texas hold 'em) in C# right now. I have divided faces and suits in to Enums like so:

public enum Face { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }

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

I then generate a string for debug purposes to test the generation.

string[] Faces = Enum.GetNames(typeof (Face));
string[] Suits = Enum.GetNames(typeof (Suit));

int r3 = rnd.Next(Faces.Length);
int r4 = rnd.Next(Suits.Length);

string currentCard = string.Format("{0} of {1}", Faces[r3], Suits[r4]);

This will generate a random string such as "Six of Diamonds"

For the visualization of the "generated" cards, I've loaded several images of each card in a deck into the resources and loaded those images into an image array like so (shortened for clutter purposes):

Image[] cardArray = { 
                      Resources._10_of_clubs, 
                      Resources._10_of_diamonds,
                      Resources._10_of_hearts,
                      Resources._10_of_spades,
                      Resources._2_of_clubs,
                      etc...
};

Is it possible to select a specific image from the array based on the generated circumstances created by the face and suit combinations above? So if "Six of Diamonds" were to be generated, the program will go in to the array and pick out the six of diamonds resource. Sorry if this is a bit much to ask. Even a push in the right direction would be appreciated.

4 Answers 4

1

Enums are based on integers. Setup the images to match the cards in sequence and then do

var myCard =  cardArray[(int)Three];

Since there are the suits with the cards, have the suit's enum value as an offset to their location such as

public enum Suit { Clubs = 0, Spades=13, Hearts=26, Diamonds=39 }

Then bring them together

var myCard =  cardArray[(int)Three + (int)Hearts];
Sign up to request clarification or add additional context in comments.

2 Comments

This appears to be the way I want to do it, however I'm confused with what you mean by "match the cards in sequence". Sequence meaning 2,3,4,5,ect.. or like clubs,diamonds,hearts,spades?
@JakeRieger The first image will be the Ace of Clubs, Two of CLubs, Three of Clubs, ... So Ace (0) of Clubs (0) is the first image and resides at 0 in the image array. Two (index 1) of clubs (0) resides at index 1, etc. So when they the two numbers are added up the card image matches the index. Note you could skip the two enums and create one enum with all combinations "AceofClubs" = 0, "TwoOfClubs"..."AceOfSpades", "TwoOfSpades" and not have to add them together.
0

Store your images in a Dictionary<String, Image> where String (being the key) would be "Six of Diamonds" and the Image would be the resource image of the 6 of Diamonds. Put all your images into this Dictionary with this format. Then your usage of getting an image from the Dictionary would be as followed.

Image imageToDisplay = cardDictionary[cardName];

cardName would be a string holding "Six of Diamonds" or whatever name you would be wanting to display.

2 Comments

I like this, but it's kind of a step back from what I've already done. It certainly is more efficient though. I might come back to this. Thanks!
This is the way I decided to go. Works likes a charm. Thanks!
0

I would probably create a Card class and give it properties for Suit, Number, and Image. Once that's done, you can setup your card resources by creating a Collection of Card objects and then use LINQ to select the card that matches the criteria that you're looking for.

Comments

0

You array have no information about original name/source of the item, so you will not be able to find element in array. If array constructed carefully you can compute index based on face and suite:

cardArray = {r_Ace_Clubs, ... r_King_Clubs, r_Ace_Spades...};
arrayIndex = suite * (int)Face.MaxElement + (int)face; // assuming MaxElement = King

Dictionary<T,U> is generally better choice when you need to do look-up. You can use anonyous type of new {Face=face, Suite=suite} as key and image as value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.