Last active
May 10, 2018 19:13
-
-
Save JohnLBevan/7fd5eb51c096692959525a84a5de24a9 to your computer and use it in GitHub Desktop.
c-sharp-array-to-create-a-deck-of-cards (Answer to Stack Overflow Question 50278559)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Question: https://stackoverflow.com/q/50278559/361842 | |
| //Fiddle: https://dotnetfiddle.net/26rxWs | |
| using System; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| //consider using classes with public static members instead of enums if you want to add any functionality over Suits or Titles: https://stackoverflow.com/a/12675874/361842 | |
| public enum Suit | |
| { | |
| Diamond | |
| ,Club | |
| ,Heart | |
| ,Spade | |
| } | |
| public enum CardValueTitle | |
| { | |
| Ace | |
| ,Two | |
| ,Three | |
| ,Four | |
| ,Five | |
| ,Six | |
| ,Seven | |
| ,Eight | |
| ,Nine | |
| ,Ten | |
| ,Jack | |
| ,Queen | |
| ,King | |
| } | |
| public class Card | |
| { | |
| public Suit Suit {get;set;} | |
| public CardValueTitle Title {get;set;} | |
| public Card (Suit suit, CardValueTitle title) | |
| { | |
| Suit = suit; | |
| Title = title; | |
| } | |
| public override string ToString() | |
| { | |
| return string.Format("{0} of {1}s", Title, Suit); | |
| } | |
| } | |
| public class Deck: IEnumerable<Card> | |
| { | |
| private static readonly Suit[] suits = Enum.GetValues(typeof(Suit)).Cast<Suit>().ToArray(); | |
| private static readonly CardValueTitle[] titles = Enum.GetValues(typeof(CardValueTitle)).Cast<CardValueTitle>().ToArray(); | |
| IList<Card> cards = new List<Card>(); | |
| public Deck() | |
| { | |
| foreach (var suit in suits) | |
| { | |
| foreach (var title in titles) | |
| { | |
| cards.Add(new Card(suit, title)); | |
| } | |
| } | |
| } | |
| public Card this[int index] | |
| { | |
| get{return cards[index];} | |
| } | |
| public IEnumerator<Card> GetEnumerator() | |
| { | |
| return cards.GetEnumerator(); | |
| } | |
| IEnumerator IEnumerable.GetEnumerator() | |
| { | |
| return this.GetEnumerator(); | |
| } | |
| public Card GetSpecificCard(Suit suit, CardValueTitle title) | |
| { | |
| //assumes that the deck's cards list does not get shuffled, so can dive in and pluck from the relevant inde | |
| //var index = (int)suit * titles.Count() + (int)title; //works because of how we populated our array | |
| //return cards[index]; | |
| //works regardless of card order | |
| return cards.First(x => x.Title.Equals(title) && x.Suit.Equals(suit)); | |
| } | |
| } | |
| public class Program | |
| { | |
| public static void Main() | |
| { | |
| var myDeck = new Deck(); | |
| Console.WriteLine(myDeck.GetSpecificCard(Suit.Spade, CardValueTitle.Queen)); | |
| foreach (var card in myDeck) | |
| { | |
| Console.WriteLine(card); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment