Ejemplo n.º 1
0
        public void SelectCards()
        {
            if (this.State != GameState.CardSelection)
            {
                throw new GameCreationException("This method can only be called during CardSelection!");
            }

            if (_Table != null)
            {
                _Table.TearDown();
            }

            this.CardsAvailable = Cards.CardCollection.GetAllCards(c => IsCardAllowed(c));
            Cards.CardCollection _CardsChosen = new Cards.CardCollection();

            if (this.Settings.Preset != null)
            {
                _CardsChosen = this.Settings.Preset.Cards;
            }
            else if (this.Settings.Constraints != null)
            {
                _CardsChosen.AddRange(this.Settings.Constraints.SelectCards(this.CardsAvailable, 10));
            }

            this.CardsAvailable.RemoveAll(card => _CardsChosen.Contains(card));
            _Table = new Table(this, this.Players.Count);
            _CardsChosen.ForEach(c => _Table.AddKingdomSupply(_Players, c.CardType));

            // should now have a list of cards that can be drawn from for things like Bane supplies

            _Table.SetupSupplies(this);
        }
Ejemplo n.º 2
0
        private static List <Type> GetAllSerializingTypes(Cards.CardCollection cards)
        {
            List <Type> typeDict = new List <Type>()
            {
                typeof(Cards.Cost), typeof(Cards.Group), typeof(Cards.Category)
            };

            cards.ForEach(c => typeDict.AddRange(c.GetSerializingTypes()));
            return(typeDict);
        }
Ejemplo n.º 3
0
        public void Clear()
        {
            if (this.Players != null)
            {
                this.Players.TearDown();
            }
            if (this.Table != null)
            {
                this.Table.TearDown();
            }
            foreach (Cards.Card card in this.CardsAvailable)
            {
                card.TearDown();
            }

            Cards.CardCollection cardsAvailable = new Cards.CardCollection(this.CardsAvailable);
            List <Player>        players        = new List <Player>();

            if (this.Players != null)
            {
                players.AddRange(this.Players);
            }
            if (this.Table != null)
            {
                this.Table.Clear();
            }
            if (this.Players != null)
            {
                this.Players.Clear();
            }
            this.TurnsTaken.Clear();
            this.MessageRequestQueue.Clear();
            this.MessageResponseQueue.Clear();
            this.CardsAvailable.Clear();

#if DEBUG
            foreach (Cards.Card card in cardsAvailable)
            {
                card.TestFireAllEvents();
            }

            foreach (Player p in players)
            {
                p.TestFireAllEvents();
            }

            TestFireAllEvents();
#endif
        }
Ejemplo n.º 4
0
        public void Load(String filename)
        {
            if (this.State != GameState.Unknown)
            {
                return;
            }

            //try
            //{
            XmlDocument xdGame = new XmlDocument();

            xdGame.LoadXml(Utilities.StringUtility.Decrypt(Utilities.StringUtility.Unzip(System.IO.File.ReadAllBytes(filename))));
            //xdGame.Load("gamedump.xml");

            XmlNode xn = xdGame.SelectSingleNode("game/rng");

            if (xn != null)
            {
                MemoryStream    msRandom = new MemoryStream(Convert.FromBase64String(xn.InnerText));
                BinaryFormatter bf       = new BinaryFormatter();
                this.RNG = (Random)bf.Deserialize(msRandom);
                msRandom.Close();
            }

            xn = xdGame.SelectSingleNode("game/start_time");
            if (xn != null)
            {
                this.StartTime = DateTime.Parse(xn.InnerText);
            }

            this.State = GameState.Setup;

            xn = xdGame.SelectSingleNode("game/settings/GameSettings");
            if (xn != null)
            {
                Cards.CardCollection allCards       = Cards.CardCollection.GetAllCards(c => true);
                XmlSerializer        myDeserializer = new XmlSerializer(typeof(GameSettings), GetAllSerializingTypes(allCards).ToArray());
                using (StringReader sr = new StringReader(xn.OuterXml))
                {
                    this.Settings = (GameSettings)myDeserializer.Deserialize(sr);
                }
            }

            _Table = new Table(this);
            XmlNodeList xnl = xdGame.SelectNodes("game/players/player");

            this._Players = new PlayerCollection();
            foreach (XmlNode xnPlayer in xnl)
            {
                Player player = Player.Load(this, xnPlayer);
                this._Players.Add(player);
                this.Table.AddPlayer(player);
            }

            xn = xdGame.SelectSingleNode("game/table");
            if (xn == null)
            {
                return;
            }
            this.Table.Load(xn);

            this._Players.Setup(this);

            xn = xdGame.SelectSingleNode("game/state");
            if (xn != null)
            {
                this.State = (GameState)Enum.Parse(typeof(GameState), xn.InnerText, true);
            }

            this.TurnsTaken.Load(this, xdGame.SelectSingleNode("game/turns"));

            xn = xdGame.SelectSingleNode("game/activeplayer");
            if (xn != null && !String.IsNullOrEmpty(xn.InnerText))
            {
                this.ActivePlayer = this.Players.FirstOrDefault(p => p.UniqueId == new Guid(xn.InnerText));
                this.ActivePlayer.SetCurrentTurn(this.TurnsTaken.Last());
            }
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
        }
Ejemplo n.º 5
0
        public void Save(String filename)
        {
            XmlDocument xdGame = new XmlDocument();
            XmlElement  xeGame = xdGame.CreateElement("game");

            xdGame.AppendChild(xeGame);

            MemoryStream    msRandom = new MemoryStream();
            BinaryFormatter bf       = new BinaryFormatter();

            bf.Serialize(msRandom, this.RNG);
            msRandom.Close();
            XmlElement xe = xdGame.CreateElement("rng");

            xe.InnerText = Convert.ToBase64String(msRandom.ToArray());
            xeGame.AppendChild(xe);

            xe           = xdGame.CreateElement("start_time");
            xe.InnerText = this.StartTime.ToString();
            xeGame.AppendChild(xe);

            xe           = xdGame.CreateElement("state");
            xe.InnerText = this.State.ToString();
            xeGame.AppendChild(xe);

            xe = xdGame.CreateElement("activeplayer");
            if (this.ActivePlayer != null)
            {
                xe.InnerText = this.ActivePlayer.UniqueId.ToString();
            }
            xeGame.AppendChild(xe);

            XmlElement xeSettings = xdGame.CreateElement("settings");

            xeGame.AppendChild(xeSettings);

            Cards.CardCollection allCards = Cards.CardCollection.GetAllCards(c => true);

            XmlSerializer xsSettings = new XmlSerializer(typeof(GameSettings), GetAllSerializingTypes(allCards).ToArray());
            StringWriter  swSettings = new StringWriter();

            xsSettings.Serialize(swSettings, this.Settings);
            swSettings.Close();

            XmlDocument xdSettings = new XmlDocument();

            xdSettings.LoadXml(swSettings.ToString());
            xeSettings.AppendChild(xdGame.ImportNode(xdSettings.DocumentElement, true));

            xeGame.AppendChild(this.Table.GenerateXml(xdGame, "table"));

            xeGame.AppendChild(this.Players.GenerateXml(xdGame));

            xeGame.AppendChild(this.TurnsTaken.GenerateXml(xdGame, "turns"));

            xdGame.Save("gamedump.xml");

            using (StringWriter sw = new StringWriter())
                using (XmlWriter xw = XmlWriter.Create(sw))
                {
                    xdGame.WriteTo(xw);
                    xw.Flush();
                    System.IO.File.WriteAllBytes(filename, Utilities.StringUtility.Zip(Utilities.StringUtility.Encrypt(sw.GetStringBuilder().ToString())));
                }
        }
Ejemplo n.º 6
0
        public IList <DominionBase.Cards.Card> SelectCards(IList <Cards.Card> availableCards, int numberCardsToSelect)
        {
            List <Cards.Card> _CardsChosen = new List <Cards.Card>();

            // Remove all "CardDontUse" constraint cards first
            IList <Cards.Card> usableCards = new List <Cards.Card>(availableCards);

            foreach (Cards.Constraint constraint in this.Where(c => c.ConstraintType == Cards.ConstraintType.CardDontUse))
            {
                foreach (Cards.Card card in constraint.GetMatchingCards(availableCards))
                {
                    usableCards.Remove(card);
                }
            }

            IEnumerable <Constraint> usableConstraints = this.Where(c => c.ConstraintType != Cards.ConstraintType.CardDontUse);

            Dictionary <Cards.Constraint, IList <Cards.Card> > constraintCards = new Dictionary <Cards.Constraint, IList <Cards.Card> >();

            foreach (Cards.Constraint constraint in usableConstraints)
            {
                constraintCards[constraint] = new Cards.CardCollection(constraint.GetMatchingCards(availableCards));
            }

            int attempts = 0;

            // Satisfy Minimum constraints first
            do
            {
                attempts++;
                _CardsChosen.Clear();

                // Add in required cards first
                foreach (Cards.Constraint constraint in usableConstraints.Where(c => c.ConstraintType == Cards.ConstraintType.CardMustUse))
                {
                    _CardsChosen.AddRange(constraintCards[constraint]);
                }

                if (_CardsChosen.Count > numberCardsToSelect)
                {
                    throw new ConstraintException(String.Format("Too many required cards specified in constraints!  Please double-check your setup and loosen the requirements.  {0} needed & found {1} required constraints.", numberCardsToSelect, _CardsChosen.Count));
                }

                foreach (Cards.Constraint constraint in usableConstraints.OrderByDescending(c => c.Minimum))
                {
                    if (constraint.MinimumMet(_CardsChosen))
                    {
                        continue;
                    }

                    CardCollection discardCards = new CardCollection();
                    Utilities.Shuffler.Shuffle(constraintCards[constraint]);
                    foreach (Cards.Card card in constraintCards[constraint])
                    {
                        if (discardCards.Contains(card))
                        {
                            continue;
                        }

                        if (this.IsChoosable(_CardsChosen, card))
                        {
                            _CardsChosen.Add(card);
                            if (constraint.MinimumMet(_CardsChosen))
                            {
                                break;
                            }
                            // Certain constraints (like NumberOfSets) immediately disallow other cards when a card is chosen.
                            // We need to get the list of disallowed cards after each card is added to the list.
                            // This is only needed to ensure the Minimum constraint without taking 10k iterations
                            discardCards.AddRange(constraint.GetCardsToDiscard(_CardsChosen, constraintCards[constraint]));
                        }
                    }
                }

                // Give it 50 attempts at trying to satisfy the Minimum requirements
                if (attempts > 50)
                {
                    throw new ConstraintException("Cannot satisfy specified constraints!  Please double-check and make sure it's possible to construct a Kingdom card setup with the constraints specified.");
                }
            } while (!this.MinimumMet(_CardsChosen) || _CardsChosen.Count > numberCardsToSelect);

            // After satisfying the Minimums, Maximums should be pretty easy to handle
            List <Cards.Card> _CardsChosenNeeded = new List <Cards.Card>(_CardsChosen);

            attempts = 0;
            while (_CardsChosen.Count < numberCardsToSelect)
            {
                attempts++;
                // Give it 50 attempts at trying to satisfy the Minimum requirements
                if (attempts > 50)
                {
                    throw new ConstraintException("Cannot satisfy specified constraints!  Please double-check and make sure it's possible to construct a Kingdom card setup with the constraints specified.");
                }

                _CardsChosen.Clear();
                _CardsChosen.AddRange(_CardsChosenNeeded);
                Utilities.Shuffler.Shuffle(usableCards);

                foreach (Cards.Card chosenCard in usableCards)
                {
                    if (_CardsChosen.Contains(chosenCard))
                    {
                        continue;
                    }

                    if (this.IsChoosable(_CardsChosen, chosenCard))
                    {
                        _CardsChosen.Add(chosenCard);
                    }

                    if (_CardsChosen.Count == numberCardsToSelect)
                    {
                        break;
                    }
                }
            }

            return(_CardsChosen);
        }
Ejemplo n.º 7
0
 public CardCollection Except(CardCollection second)
 {
     return(new CardCollection(this.Except <Card>(second)));
 }