string array converts to enum

Viewed 78

Hi guys I just wanted to ask if it is possible

I am currently making a simple poker game and here's my array of string which has the value of

public static string[] cardSprite = new string[]
   {
        "1c", "1s", "1d", "1h", "2c", "2s", "2d", "2h", "3c", "3s", "3d", "3h",
        "4c", "4s", "4d", "4h", "5c", "5s", "5d", "5h", "6c", "6s", "6d", "6h",
        "7c", "7s", "7d", "7h", "8c", "8s", "8d", "8h", "9c", "9s", "9d", "9h",
        "10c", "10s", "10d", "10h", "11c", "11s", "11d", "11h", "12c", "12s", "12d", "12h",
        "13c", "13s", "13d", "13h",
   };

Now those value inside the array I used it to load the cards from my resources like this

Resources.Load<Sprite>

and store the original values on this variable

string[] cardplaceholderValue

Now its working perfectly but I am having trouble of getting the game rules of poker like onePair() twoPair() etc.

So my solution for that is convert/put the array to enum but I am stuck so what on my mind is like this

public enum CardValue
    {
        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
    }

Here's the problem on my conditional statement

if(player1ValueCard[0] == player1ValueCard[1])
{
    ValueHolder.Player1OnePair = true;    
}

How will I know the value of my card that is converted into enum so that even if it is 1 of heart or 1 of diamond or 1 of clover or 1 of spade it will be considered as ACE or 1

WHAT I"VE DONE SO FAR

GAMERULE.CS

public static void ConvertCards()
    {
        for (int i = 0; i < 9; i++)
        {
            if (ValueHolder.shuffleDeck[i] == "1h" || ValueHolder.shuffleDeck[i] == "1d" || ValueHolder.shuffleDeck[i] == "1c"
                || ValueHolder.shuffleDeck[i] == "1s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.ACE.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "2h" || ValueHolder.shuffleDeck[i] == "2d" || ValueHolder.shuffleDeck[i] == "2c"
               || ValueHolder.shuffleDeck[i] == "2s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.TWO.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "3h" || ValueHolder.shuffleDeck[i] == "3d" || ValueHolder.shuffleDeck[i] == "3c"
               || ValueHolder.shuffleDeck[i] == "3s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.THREE.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "4h" || ValueHolder.shuffleDeck[i] == "4d" || ValueHolder.shuffleDeck[i] == "4c"
               || ValueHolder.shuffleDeck[i] == "4s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.FOUR.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "5h" || ValueHolder.shuffleDeck[i] == "5d" || ValueHolder.shuffleDeck[i] == "5c"
               || ValueHolder.shuffleDeck[i] == "5s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.FIVE.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "6h" || ValueHolder.shuffleDeck[i] == "6d" || ValueHolder.shuffleDeck[i] == "6c"
               || ValueHolder.shuffleDeck[i] == "6s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.SIX.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "7h" || ValueHolder.shuffleDeck[i] == "7d" || ValueHolder.shuffleDeck[i] == "7c"
               || ValueHolder.shuffleDeck[i] == "7s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.SEVEN.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "8h" || ValueHolder.shuffleDeck[i] == "8d" || ValueHolder.shuffleDeck[i] == "8c"
               || ValueHolder.shuffleDeck[i] == "8s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.EIGHT.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "9h" || ValueHolder.shuffleDeck[i] == "9d" || ValueHolder.shuffleDeck[i] == "9c"
               || ValueHolder.shuffleDeck[i] == "9s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.NINE.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "10h" || ValueHolder.shuffleDeck[i] == "10d" || ValueHolder.shuffleDeck[i] == "10c"
               || ValueHolder.shuffleDeck[i] == "10s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.TEN.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "11h" || ValueHolder.shuffleDeck[i] == "11d" || ValueHolder.shuffleDeck[i] == "11c"
               || ValueHolder.shuffleDeck[i] == "11s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.JACK.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "12h" || ValueHolder.shuffleDeck[i] == "12d" || ValueHolder.shuffleDeck[i] == "12c"
               || ValueHolder.shuffleDeck[i] == "12s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.QUEEN.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
            if (ValueHolder.shuffleDeck[i] == "13h" || ValueHolder.shuffleDeck[i] == "13d" || ValueHolder.shuffleDeck[i] == "13c"
               || ValueHolder.shuffleDeck[i] == "13s")
            {
                ValueHolder.shuffleDeck[i] = ValueHolder.CardValue.KING.ToString();
                Debug.Log(ValueHolder.shuffleDeck[i]);
            }
        }
       
    }

I created this method and it can now convert the string array to the enum

I know it's not the efficient way but it's working .

2 Answers

You could get the first part of your string eg. the number with str.Substring(0, 1);. You don't need the Enum because you just set the value equal to the first part of the string (numbers from 1 to 13) anyway.

Example:

// Getting the first character of our string.
string temp = ValueHolder.shuffleDeck[i].Substring(0, 1);

// Set the card equal to the Number (1, 2, 3, ...)
ValueHolder.shuffleDeck[i] = temp;
Debug.Log(ValueHolder.shuffleDeck[i]);

In this particular case, I wouldn't be using string representations for your game logic. You'll eventually want to be able to compare card face values, as well as suits, and combinations of those. For that reason, I'd suggest 'breaking' your enum into parts using the Flags attribute. Here's an example of how I would start the code:

[Flags]
public enum Cards
{
    None = 0,
    Spades = 1 << 0,
    Hearts = 1 << 1,
    Diamonds = 1 << 2,
    Clubs = 1 << 3,

    Ace = 1 << 10,
    Two = 1 << 11,
    Three = 1 << 12,
    Four = 1 << 13,
    Five = 1 << 14,
    Six = 1 << 15,
    Seven = 1 << 16,
    Eight = 1 << 17,
    Nine = 1 << 18,
    Jack = 1 << 19,
    Queen = 1 << 20,
    King = 1 << 21,

    Suits = Spades | Hearts | Diamonds | Clubs,
    Values = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Jack | Queen | King
}

public Cards GetSuit ( Cards c ) => c & Cards.Suits;
public Cards GetValue ( Cards c ) => c & Cards.Values;
/// <summary>
/// Will return the card value and suit name. E.g. AceSpades
/// </summary>
/// <param name="c">The Card enum representing the card value.</param>
/// <returns>The expanded string representation of the enum. E.g. "AceSpades"</returns>
public string GetCardAsString ( Cards c ) => $"{ GetValue ( c )}{ GetSuit ( c )}";

It's certainly not complete. And there's no valid value checking. For example, the previous code WOULD allow a Card to be assigned like this:

var newCard = Card.Ace | Card.Two | Card.Heats | Card.Diamonds;

So, you will need to decide at what point you're going to introduce bounds and value sanitisation.

You could also introduce another method containing a big switch statement (two in fact) to reduce Card.Ace to "1", and Card.Spades to "s", but I'm not quite sure that would benefit you here. The option is obviously there though.

Related