Enums and Ints
-
I'm confused on how to convert between enum types and integer types effeciently. Here's the code I'm working with: public enum Rank { Hearts = 0, Diamonds = 1, Spades = 3, Clubs = 4 } public enum Suit { Deuce = 0, Trey = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 } Inside the card, the rank and suit are stored as: Rank rank; Suit suit; The point being: I have a function shuffle which shuffles the deck using for loops. public void Shuffle() { FillDeck(); Card tempCard = new Card(); Random rInt = new Random(); for (int i = 0; i < 100; i++) { int first = rInt.Next(51); int second = rInt.Next(51); // Copy deck[first] to tempCard // Copy deck[second] to deck[first] // Copy tempCard to deck[second] tempCard.Set(deck[first].Rank, deck[first].Suit); deck[first].Set(deck[second].Rank, deck[second].Suit); deck[second].Set(tempCard.Rank, tempCard.Suit); } } I'm attemping to pass integer values from the for loops into the Set function of card which takes Rank and Suit parameters. It's obviously causing errors. Can I simply typecast an integer to the equivalent suit or rank like so without causing problems, and is it proper programming practice or is there a better way to do it? tempCard.Set( (Rank)deck[first].Rank, (Suit)deck[first].Suit );
-
I'm confused on how to convert between enum types and integer types effeciently. Here's the code I'm working with: public enum Rank { Hearts = 0, Diamonds = 1, Spades = 3, Clubs = 4 } public enum Suit { Deuce = 0, Trey = 1, Four = 2, Five = 3, Six = 4, Seven = 5, Eight = 6, Nine = 7, Ten = 8, Jack = 9, Queen = 10, King = 11, Ace = 12 } Inside the card, the rank and suit are stored as: Rank rank; Suit suit; The point being: I have a function shuffle which shuffles the deck using for loops. public void Shuffle() { FillDeck(); Card tempCard = new Card(); Random rInt = new Random(); for (int i = 0; i < 100; i++) { int first = rInt.Next(51); int second = rInt.Next(51); // Copy deck[first] to tempCard // Copy deck[second] to deck[first] // Copy tempCard to deck[second] tempCard.Set(deck[first].Rank, deck[first].Suit); deck[first].Set(deck[second].Rank, deck[second].Suit); deck[second].Set(tempCard.Rank, tempCard.Suit); } } I'm attemping to pass integer values from the for loops into the Set function of card which takes Rank and Suit parameters. It's obviously causing errors. Can I simply typecast an integer to the equivalent suit or rank like so without causing problems, and is it proper programming practice or is there a better way to do it? tempCard.Set( (Rank)deck[first].Rank, (Suit)deck[first].Suit );
Whew, where to begin. First about the enums. I am assuming your Card class set method is structured as such: Card.Set ( Rank r, Suit s) and you have an array of Cards in deck[]. So to set your first card you would say deck[0].Set ( Rank.Hearts, Suit.Ace ). Now the first problem is you have Rank and Suit mixed up, the Suit should be hearts, clubs, etc. Confusing to read. Your second issue is your Card property Rank and Suit have the same name as your enum, that cannot be. I see what you are doing, you want to return that enum to set the next card with it, but you can't have the same name. I would change the name to CardRank and CardSuit and define the property like this: //member variables private Rank cardRank; private Suit cardSuit; //Properties public Rank CardRank { set { cardRank = value; } get { return cardRank; } } // do the same for CardSuit now your swap functions will be: tempCard.Set ( deck[first].CardRank, deck[first].CardSuit ); If you used integers in you Set function prototype then this won't work and you have to modify your properties to convert to integer or from integer depending on what your members are. Your third issue is your last card will never get shuffled, Random returns a number less than the number passed in and your first and second could theoretcially be the same card (not a real problem, just wasted a shuffle) If you want the integer value of an enum do this: int x = (int) Rank.Clubs; //now x = 4 or Rank r = Rank.Clubs; int x = (int) r; //x = 4 finally if you want the string value: string s = Rank.Clubs.ToString(); // s = "Clubs"
-
Whew, where to begin. First about the enums. I am assuming your Card class set method is structured as such: Card.Set ( Rank r, Suit s) and you have an array of Cards in deck[]. So to set your first card you would say deck[0].Set ( Rank.Hearts, Suit.Ace ). Now the first problem is you have Rank and Suit mixed up, the Suit should be hearts, clubs, etc. Confusing to read. Your second issue is your Card property Rank and Suit have the same name as your enum, that cannot be. I see what you are doing, you want to return that enum to set the next card with it, but you can't have the same name. I would change the name to CardRank and CardSuit and define the property like this: //member variables private Rank cardRank; private Suit cardSuit; //Properties public Rank CardRank { set { cardRank = value; } get { return cardRank; } } // do the same for CardSuit now your swap functions will be: tempCard.Set ( deck[first].CardRank, deck[first].CardSuit ); If you used integers in you Set function prototype then this won't work and you have to modify your properties to convert to integer or from integer depending on what your members are. Your third issue is your last card will never get shuffled, Random returns a number less than the number passed in and your first and second could theoretcially be the same card (not a real problem, just wasted a shuffle) If you want the integer value of an enum do this: int x = (int) Rank.Clubs; //now x = 4 or Rank r = Rank.Clubs; int x = (int) r; //x = 4 finally if you want the string value: string s = Rank.Clubs.ToString(); // s = "Clubs"
Awesome. Thanks for those pointers. That will certainly help me get sorted out. There is one issue, the big one, that you didn't mention however (I don't think). I have two integers from the four loops, s and r. s is between 0 and 3 and r is between 0 and 12. They correspond directly to the integer values I assigned the suits and ranks in the enum. From those integers, I need to store the suit and rank, in the for loops. So - I need to do something like: Rank rank = (Rank)r; // r is in integer. if r == 12, then this should set rank equal to Rank.Ace, however, you can't typecast an integer to the equavalent enum value. I don't need the string. You see what I'm saying?
-
Awesome. Thanks for those pointers. That will certainly help me get sorted out. There is one issue, the big one, that you didn't mention however (I don't think). I have two integers from the four loops, s and r. s is between 0 and 3 and r is between 0 and 12. They correspond directly to the integer values I assigned the suits and ranks in the enum. From those integers, I need to store the suit and rank, in the for loops. So - I need to do something like: Rank rank = (Rank)r; // r is in integer. if r == 12, then this should set rank equal to Rank.Ace, however, you can't typecast an integer to the equavalent enum value. I don't need the string. You see what I'm saying?
I just wrote my own static functions to do this, and added them to the card class: public static Rank ConvertToRank(int i); public static Suit ConvertToSuit(int i); ... just filled with switch statements to return the appropriote enum object. I was hoping there was a better way to do this though.
-
I just wrote my own static functions to do this, and added them to the card class: public static Rank ConvertToRank(int i); public static Suit ConvertToSuit(int i); ... just filled with switch statements to return the appropriote enum object. I was hoping there was a better way to do this though.
In vb you can do
Dim r as Integer '<- Random rank
'Make r as random number
CardRank = CType(r, Rank)I think in think in c# that would be
int r;
// Make r a random number
CardRank = (Rank)r;But you say that doesn't work :confused: Pretty weird... Pompiedompiedom... ;)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
-- modified at 18:30 Thursday 27th October, 2005
-
In vb you can do
Dim r as Integer '<- Random rank
'Make r as random number
CardRank = CType(r, Rank)I think in think in c# that would be
int r;
// Make r a random number
CardRank = (Rank)r;But you say that doesn't work :confused: Pretty weird... Pompiedompiedom... ;)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
-- modified at 18:30 Thursday 27th October, 2005