AddCard() 공개 메소드

Adds a card to the current deck
Adds to the end of the deck by default
public AddCard ( BaseCard card ) : void
card BaseCard The card to add
리턴 void
예제 #1
0
        /// <summary>
        /// Creates a deck from a deck file
        /// </summary>
        /// <param name="pathToFile">Path to the deck file</param>
        /// <returns>The deck based on the deck file</returns>
        /// <remarks>
        /// A deck file follows the following format:
        /// [count], [card name]
        /// with each card on a separate line
        /// </remarks>
        public static Deck FromDeckFile(string pathToFile)
        {
            var deck = new Deck();

            using (var stream = new StreamReader(pathToFile))
            {
                while (stream.Peek() >= 0)
                {
                    var line = stream.ReadLine();

                    if (line == null)
                    {
                        continue;
                    }

                    // lines follow the format of [count], [card name]
                    var splitLine = line.Split(',');

                    if (splitLine.Count() != 2)
                    {
                        throw new InvalidDataException(string.Format("Expected format of line is \"[count], [card name]\". Got {0} instead", line));
                    }

                    var count           = int.Parse(splitLine[0]);
                    var cardName        = splitLine[1];
                    var cleanedCardName = Regex.Replace(cardName, @"[\W]", "", RegexOptions.None);

                    for (int i = 0; i < count; i++)
                    {
                        var assembly = Assembly.Load("HearthAnalyzer.Core");
                        var cardType = assembly.GetTypes().FirstOrDefault(t => String.Equals(t.Name, cleanedCardName, StringComparison.InvariantCultureIgnoreCase));
                        if (cardType == null)
                        {
                            throw new InvalidDataException(string.Format("Failed to find card type with name: {0}", cleanedCardName));
                        }

                        var     createCardMethod     = typeof(HearthEntityFactory).GetMethod("CreateCard");
                        var     createCardTypeMethod = createCardMethod.MakeGenericMethod(new[] { cardType });
                        dynamic card = createCardTypeMethod.Invoke(null, null);

                        deck.AddCard(card);
                    }
                }
            }

            if (deck.Cards.Count() > Constants.MAX_CARDS_IN_DECK)
            {
                throw new InvalidDataException("There are too many cards in this deck!");
            }

            if (deck.Cards.Count() < Constants.MAX_CARDS_IN_DECK)
            {
                throw new InvalidDataException("There are too few cards in this deck!");
            }

            return(deck);
        }
예제 #2
0
        /// <summary>
        /// Creates a deck from a deck file
        /// </summary>
        /// <param name="pathToFile">Path to the deck file</param>
        /// <returns>The deck based on the deck file</returns>
        /// <remarks>
        /// A deck file follows the following format:
        /// [count], [card name]
        /// with each card on a separate line
        /// </remarks>
        public static Deck FromDeckFile(string pathToFile)
        {
            var deck = new Deck();
            using (var stream = new StreamReader(pathToFile))
            {
                while (stream.Peek() >= 0)
                {
                    var line = stream.ReadLine();

                    if (line == null) continue;

                    // lines follow the format of [count], [card name]
                    var splitLine = line.Split(',');

                    if (splitLine.Count() != 2)
                    {
                        throw new InvalidDataException(string.Format("Expected format of line is \"[count], [card name]\". Got {0} instead", line));    
                    }

                    var count = int.Parse(splitLine[0]);
                    var cardName = splitLine[1];
                    var cleanedCardName = Regex.Replace(cardName, @"[\W]", "", RegexOptions.None);

                    for (int i = 0; i < count; i++)
                    {
                        var assembly = Assembly.Load("HearthAnalyzer.Core");
                        var cardType = assembly.GetTypes().FirstOrDefault(t => String.Equals(t.Name, cleanedCardName, StringComparison.InvariantCultureIgnoreCase));
                        if (cardType == null)
                        {
                            throw new InvalidDataException(string.Format("Failed to find card type with name: {0}", cleanedCardName));
                        }

                        var createCardMethod = typeof (HearthEntityFactory).GetMethod("CreateCard");
                        var createCardTypeMethod = createCardMethod.MakeGenericMethod(new[] {cardType});
                        dynamic card = createCardTypeMethod.Invoke(null, null);

                        deck.AddCard(card);
                    }
                }
            }

            if (deck.Cards.Count() > Constants.MAX_CARDS_IN_DECK)
            {
                throw new InvalidDataException("There are too many cards in this deck!");
            }

            if (deck.Cards.Count() < Constants.MAX_CARDS_IN_DECK)
            {
                throw new InvalidDataException("There are too few cards in this deck!");
            }

            return deck;
        }