LarsGast / DeckOfPlayingCards

Simple code to play with a standard deck of 52 cards
MIT License
0 stars 0 forks source link

Extension methods for the display string for Rank en Suit enum #2

Closed LarsGast closed 1 year ago

LarsGast commented 1 year ago

Is your feature request related to a problem? Please describe. If you want to display the rank and the suit of a card right now, the only way of doing that would be to display the string values of those enums. It would be nice to be able to all a method that converts those enums to a prettier display string.

Describe the solution you'd like Create a method for each enum (Rank and Suit) that converts the value of that enum into single character strings.

For the Rank enum, the method could return the number of the value (so 2, 3, etc.) instead of the string value (so Two, Three, etc.) if the value if lower then Jack. For Jack, Queen, King, and Ace, the method could return the single letter that represents those values (so J, Q, K, and A).

For the Suit enum, the method could return the unicode string for the different type of suits. These are:

Describe alternatives you've considered Displaying the first letter of the Suit enum could also work instead of the unicode characters. These letters have no overlap with the first letters of Jack, Queen, King, and Ace.

Additional context I've already experimented with this in a different project. The code that I used for that is this:

/// <summary>
/// Extension method.
/// Gets the value of the rank as a string for displaying.
/// </summary>
/// <param name="rank"></param>
/// <returns></returns>
public static string getDisplayString(this Rank rank) {
    switch (rank) {
        case Rank.Jack:
            return "J";
        case Rank.Queen:
            return "Q";
        case Rank.King:
            return "K";
        case Rank.Ace:
            return "A";
        default:
            return ((int)rank).ToString();
    }
}

/// <summary>
/// Extension method.
/// Gets the unicode value of the suit as a string for displaying.
/// </summary>
/// <param name="suit"></param>
/// <returns></returns>
public static string? getDisplayString(this Suit suit) {
    switch (suit) {
        case Suit.Hearts:
            return "\u2665";
        case Suit.Spades:
            return "\u2660";
        case Suit.Diamonds:
            return "\u2666";
        case Suit.Clubs:
            return "\u2663";
        default:
            return null;
    }
}
LarsGast commented 1 year ago

The getDisplayString() method for the rank gets a single character string for all values except 10. To keep this consistent 10 could be displayed as T, I've seen this being done before. This would mean that the display string for a card would always be 2 characters long. However, this might not be intuitive for everyone, so maybe an optional boolean parameter displayTenAsT could work here.

LarsGast commented 1 year ago

I decided to go with a displayTenAsT boolean optional parameter that is false by default. It being false by default seems more intuitive, as ten is not part of the royal cards.