Array for Blackjack cards, strings or integers?

无人久伴 提交于 2019-12-30 07:33:05

问题


Having a look at related BlackJack questions, there is a confusion on what is better to use for the cards, either making an array of strings/items or integers.

With my BlackJack game Aces are 11 only and 10, J, Q and K are all value 10, doesn't matter of the suit. What would make it easier for me to code the cards with, strings or integers?

Visual Studio C# 2010, Windows form application


回答1:


A Card is an object, so make it its own class.

An enum of Suit (Heart, Diamond, Club, Spade) and an enum for Rank (Ace, Jack, 10, etc., ordered accordingly for ease of comparison) should be used. You can derive color from suit, so I would not suggest making color a parameter to the constructor since it is redundant. You really only need those two fields in the constructor, unless you are planning on having duplicates of the cards, like if you are using multiple decks in your game. When using these cards, using a List or similar type is highly advisable, because a lot of the sorting operations are built-in. And when you need to shuffle the deck, use the Knuth shuffle! You can find it with Google pretty easily, it's easy to implement, and works fast and efficiently.

This approach is from my CS professor, and I am inclined to appreciate it as well. The more your code models real life, the easier it is to understand and extend. You could get by with int or string constants, but they are extra work you don't need to do nor are they elegant.

public class Card
{
    public Suit Suit { get; private set; }
    public Rank Rank { get; private set; }

    public Card(Suit suit, Rank rank)
    {
         this.suit = suit;
         this.rank = rank;
    }
} 



回答2:


Strings are almost never the best internal representation for anything but actual text. Strings are for people; computers use numbers. For cards especially it's a no-brainer, since cards often need to be compared by rank and added by value. It takes a lot less code to tell a program that 12 > 11 or to add 10 to a total than it does to tell it that "K" > "Q" or to add "J" to a total. Using strings internally is common with rookie programmers too lazy to learn about data representation.

Of course in an object-oriented language like C++ you can use objects, but the member variables of those objects that hold the card rank and suits should be integers so you can index lookup tables, compare ranges, and so on.

I wrote an essay on card representations here.

For blackjack, if you don't need a general-purpose card representation, then just using the integers 1 to 10 is ideal. Use 1 for aces, not 11, it will make your total calculations faster: you only ever need to promote one ace from 1 to 11, but you'd need to demote several from 11 to 1.

For example, if you have an array of these integers representing a hand, adding the hand total is something like this (of course, fleshed out):

int total = 0, acefound = 0, soft = 0;

for (int i = 0; i < cardsinhand; ++i) {
    total += hand[i];
    if (1 == hand[i]) acefound = 1;
}
if (acefound && total < 12) {
    soft = 1;
    total += 10;
}

Simple, and blindingly fast. If you are representing actual cards, so you'll have face cards with ranks 11, 12, 13, then just add something like if (r > 10) r = 10; in there (another reason to make aces 1). I can simulate billions of hands in minutes like this.




回答3:


Blackjack Console Application Sample Project (C#) GitHub:

https://github.com/koistya/Blackjack

public class Card
{
    public Card(Rank rank, Suite suite)
    {
        this.Rank = rank;
        this.Suite = suite;
        this.IsFaceUp = true;
    }

    public Rank Rank { get; private set; }

    public Suite Suite { get; private set; }

    public bool IsFaceUp { get; private set; }

    public void Flip()
    {
        this.IsFaceUp = !this.IsFaceUp;
    }
}

public enum Rank : byte
{
    Ace   = 1,
    Two   = 2,
    Three = 3,
    Four  = 4,
    Five  = 5,
    Six   = 6,
    Seven = 7,
    Eight = 8,
    Nine  = 9,
    Ten   = 10,
    Jack  = 11,
    Queen = 12,
    King  = 13,
}

public enum Suite : byte
{
    Club    = 1,
    Diamond = 2,
    Heart   = 3,
    Spades  = 4
}



回答4:


enum for the 13 possible values would be better idea



来源:https://stackoverflow.com/questions/17052091/array-for-blackjack-cards-strings-or-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!