Hi,
I’m making a card game and I was wondering what you guys thought was the best way to code the deck. I’m tossing up between instantiating each card and assign a value to them at the begining of the game, then just finding the top most card in the deck to deal. Or have a deck object that instantiated the cards as you needed them. Then I have to figure how to do the other things like a discard pile and cards on the board and in the hand, which could be actual cards or just place holders that take on the cards rank and suit and turn invisible if it is null. What do you guys think?
It’s kind of hard to answer these types of questions accurately because we really don’t know anything specific about your game. Your game might have some specific requirements that a generic implementation might have trouble with.
Card games generally don’t challenge the CPU or GPU, so don’t try to get cute with the code. You generally don’t need to optimize any of your algorithms in a card game, so just brute force it. It’s too easy to over-think these things. Just stick with strong OOP concepts, code what you need, and you’ll cause yourself fewer problems.
Most card games are going to have a Card class, a Deck class, and a Hand class for the purposes of the game. For example, a straight-forward Card class will, of course, contain the card’s rank and suit. It might also reference the card’s graphical and audio assets for easier management. The Deck class will store an array of Card references, and would also be a good place to include the discard list, and any other assistance your game requires in terms of dealing, shuffling, and cleaning up cards on the table. Again, you might consider referencing any graphical and audio assets that are specific to your deck, such as shuffling and dealing sounds. Similarly, you can write a Hand class that contains a list of cards in each player’s hand.
Here’s just a really quick off-the-cuff skeleton to help get you started…
C#
public enum SuitEnum {
Hearts=1,
Clubs=2,
Diamonds=3
Spades=4,
}
public class Card {
private SuitEnum _suit;
private int _rank;
public SuitEnum Suit { get { return _suit; } }
public int Rank { get { return _rank; } }
private GameObject _card;
public Card(SuitEnum suit, int rank, Vector3 position, Quaternion rotation) {
// to do: validate rank, position, and rotation
string assetName = string.Format("Card_{0}_{1}", suit, rank); // Example: "Card_1_10" would be the Jack of Hearts.
GameObject asset = GameObject.Find(assetName);
if (asset == null) {
Debug.LogError("Asset '" + assetName + "' could not be found.");
} else {
_card = Instantiate(asset, position, rotation);
_suit = suit;
_rank = rank;
}
}
}
public class Deck {
private List<Card> _deck = new List<Card>();
private List<Card> _discardPile = new List<Card>();
public void Shuffle() {
/* To Do */
}
public Card TakeCard() {
if (_deck.Count == 0)
return null; // the deck is depleted
// take the first card off the deck and add it to the discard pile
Card card = _deck[0];
_deck.RemoveAt(0);
_discardPile.Add(card);
return card;
}
/* ...etc... */
}
Wow Brian, that was heaps more than I was expecting. Thank you very much. I didn’t even think about enumerators. I was thinking that I should use a 2D array to represent a deck and and a discard pile: one axis being suit the other being rank and having a 1 or 0 if the card is there or not. And having the hand as 7 empty 2D variables. This stops any animation but it will do. I really like your way so I think I’ll have a play. Thanks kindly!
I wrote a prototype card game for someone the other day. It was not a standard deck of cards, but it should be applicable to that. (Or any cards.)
In it, I made a prefab out of every card. Then I created a card manager class that had an inspector-accessible variable that all the cards were added to. The card manager handled shuffling and all that. I’m a little fuzzy on how I handled prefabs vs instantiated cards, but I’m pretty sure that I only instantiated them when I needed them visually, and just used the values on the prefabs other than that.
In the game, there were 4 kinds of cards (like suits) that determined what a card did and values for each kind (like numbers, faces) that determined how much of it it did. I used an enumeration for the kinds, and an integer for the numbers, but both could have used enumerations.
Unfortunately, because I wrote it just for him, I don’t feel comfortable sharing the code.
Hello, I’m totally new to Unity4 and totally lost. I’m trying to create a deck of cards but I don’t have a clue how to do this, I’ve seen a lot of posts with code that seems to be perfect but I don’t know how to attach images to the code and to ‘‘see’’ it in the scene. It might seem a stupid question but I seem to have missed a step somewhere. I created planes and panels and put an image inside it but I need a list of cards and it needs to look like a deck. If you would be so kind to tell me how to do so, it would be very helpfull.