0

I'm making a card game and want to use the Unicode playing cards which range from U+1F0A0 to U+1F0DE.

Is there a way to create a partial Unicode sequence such as "\U0001F0" and then concatenate the last two digits on the end, or some way to change the last two digits of a full sequence to the ones needed?

I have tried StringBuilder, ToCharArray, .Insert, .Remove, .Add, .Replace etc, to replace the last two digits.

I have tried,

string cardCode = "\U0001F0" + suit + value;

string cardCode = "@\U0001F0" + suit + value;

string cardCode = $"\U0001F0{suit}{value}";

but of course the last two only display the string as plaintext, and the first one is an invalid escape sequence.

  • You can't compute escape codes. use [Char.ConvertFromUtf32(Int32)](https://learn.microsoft.com/en-us/dotnet/api/system.char.convertfromutf32?view=net-6.0) and do the math with integers. – Mark Tolonen Jul 18 '22 at 02:30
  • Does this answer your question? [Convert a Unicode string to an escaped ASCII string](https://stackoverflow.com/questions/1615559/convert-a-unicode-string-to-an-escaped-ascii-string) – gunr2171 Jul 18 '22 at 02:42
  • @MarkTolonen The integer (127136) is too high, otherwise it works great. Nevermind I didn't see your edit I will try that. Char.ConvertFromUtf32 works thank you! – GreyXephos Jul 18 '22 at 03:19
  • `string card = Char.ConvertFromUtf32(0x1F0A0 + suit * 16 + value);` – Codo Jul 18 '22 at 09:19

2 Answers2

0

There is a Unicode codepoint for each playing cards. The same value for different suits are multiples of 16 apart. And the values are nicely ordered from 1 for the ace to 14 for the king. So the effective codepoint can easily be calculated.

As proposed in the comments, Char.ConvertFromUtf32() will convert the integer codepoint into a string:

using System;
                    
public class Card
{
    public const int Spade = 0;
    public const int Hearts = 1;
    public const int Diamonds = 2;
    public const int Clubs = 3;
    
    public const int Ace = 1;
    public const int Two = 2;
    public const int Three = 3;
    public const int Four = 4;
    public const int Five = 5;
    public const int Six = 6;
    public const int Seven = 7;
    public const int Eight = 8;
    public const int Nine = 9;
    public const int Ten = 10;
    public const int Jack = 11;
    public const int Knight = 12;
    public const int Queen = 13;
    public const int King = 14;
    
    public static string CardString(int suit, int val) {
        return Char.ConvertFromUtf32(0x1F0A0 + suit * 16 + val);
    }
    
    public static void Main()
    {
        Console.WriteLine(CardString(Spade, Eight));
        Console.WriteLine(CardString(Diamonds, Ace));
        Console.WriteLine(CardString(Hearts, Queen));
        Console.WriteLine(CardString(Clubs, King));
    }
}

The result is:





Codo
  • 75,595
  • 17
  • 168
  • 206
0

Answer courtesy of Mark Tolonen's comment.

I convert the hex value of the first playing card, 1F0A0, to decimal and store that integer. Then as I generate each card, I increment that integer by 1.

Then I use that integer to convert to a UTF-16 encoded string.

string cardUnicode = char.ConvertFromUtf32(cardInteger);


private void generateDeck()
{
    int cardInteger = 127136;
    List<string> suits = new List<string> { "Spades", "Hearts", "Diamonds", "Clubs" };

    for (int j = 0; j < suits.Count; j++)
    {
        for (int i = 1; i <= 13; i++)
        {
            string cardRank = i.ToString();
            string rankSpelledOut = "";
            int cardValue = i;

            switch (cardRank)
            {
                case "1":
                    rankSpelledOut = "Ace";
                    cardRank = "A";
                    cardValue = 14;
                    break;
                case "2":
                    rankSpelledOut = "Two";
                    break;
                case "3":
                    rankSpelledOut = "Three";
                    break;
                case "4":
                    rankSpelledOut = "Four";
                    break;
                case "5":
                    rankSpelledOut = "Five";
                    break;
                case "6":
                    rankSpelledOut = "Six";
                    break;
                case "7":
                    rankSpelledOut = "Seven";
                    break;
                case "8":
                    rankSpelledOut = "Eight";
                    break;
                case "9":
                    rankSpelledOut = "Nine";
                    break;
                case "10":
                    rankSpelledOut = "Ten";
                    break;
                case "11":
                    rankSpelledOut = "Jack";
                    cardRank = "J";
                    break;
                case "12":
                    rankSpelledOut = "Queen";
                    cardRank = "Q";
                    break;
                case "13":
                    rankSpelledOut = "King";
                    cardRank = "K";
                    break;
            }

            //skip knight cards
            if (i == 12)
            {
                cardInteger++;
            }

            cardInteger++;
            string cardUnicode = char.ConvertFromUtf32(cardInteger);
            string cardString = rankSpelledOut + " of " + suits[j];
            deckList.Add(new Cards(cardRank, suits[j], cardString, cardValue, cardUnicode));
            //skip to next suit
            if (i == 13)
            {
                cardInteger += 2;
            }
        }
    }
}