/// <summary> /// Merges 1 or more Card Set(s) into this temporary Game Set. /// </summary> /// <param name="cardSetGuids">GUIDs for all the Card Sets to be added.</param> /// <exception cref="ArgumentNullException">Thrown if no Guid is provided.</exception> /// <exception cref="FileNotFoundException">Thrown if the card set cannot be found.</exception> /// <exception cref="XmlException">Thrown if the card set XML is corrupted.</exception> /// <exception cref="ApplicationException">Thrown if the card set is corrupted.</exception> public void Merge(List <string> cardSetGuids) { if (cardSetGuids == null) { throw new ArgumentNullException("cardSetGuids"); } CardSetGuid = new Guid().ToString(); Name = "GameSet"; Version = "0.0"; foreach (string guid in cardSetGuids) { string[] files = Directory.GetFiles(Program.CardSetPath, "*" + guid + ".cardset"); if (files == null) { throw new FileNotFoundException("Cannot find card set", Program.CardSetPath + guid + ".cardset"); } XmlDocument cardSetDoc = new XmlDocument(); cardSetDoc.Load(files[0]); XmlElement cardSetInfo = (XmlElement)cardSetDoc.GetElementsByTagName("CardSet")[0]; XmlElement xmlBlackCards = (XmlElement)cardSetDoc.GetElementsByTagName("BlackCards")[0]; XmlElement xmlWhiteCards = (XmlElement)cardSetDoc.GetElementsByTagName("WhiteCards")[0]; SHA256CryptoServiceProvider hasher = new SHA256CryptoServiceProvider(); byte[] allCards = Encoding.Default.GetBytes(xmlBlackCards.InnerXml + xmlWhiteCards.InnerXml); byte[] hash = hasher.ComputeHash(allCards); Hash = Convert.ToBase64String(hash); if (cardSetInfo.GetAttribute("Hash") == Hash) { } else { throw new FormatException("Card Set " + Name + " Corrupt."); } XmlNodeList CardBlock = cardSetDoc.GetElementsByTagName("CardPack"); CardBlock = cardSetDoc.GetElementsByTagName("BlackCards"); XmlNodeList Cards = CardBlock[0].ChildNodes; foreach (XmlElement Card in Cards) { string cardID = guid + "/" + Card.Attributes["ID"].Value; BlackCards.Add(cardID, new Card(cardID, Card.InnerText, Convert.ToInt32(Card.Attributes["Needs"].Value))); BlackCardCount++; BlackCardIndex.Add(BlackCardCount, cardID); } CardBlock = cardSetDoc.GetElementsByTagName("WhiteCards"); Cards = CardBlock[0].ChildNodes; foreach (XmlElement Card in Cards) { string cardID = guid + "/" + Card.Attributes["ID"].Value; WhiteCards.Add(cardID, new Card(cardID, Card.InnerText)); WhiteCardCount++; WhiteCardIndex.Add(WhiteCardCount, cardID); } } Dealer.ShuffleCards(BlackCardIndex); Dealer.ShuffleCards(WhiteCardIndex); }
public string SelectWhiteCard() { Random rand = new Random(); var selected = rand.Next(WhiteCards.Count); string card = WhiteCards[selected]; WhiteCards.RemoveAt(selected); return(card); }
public void PlayCard(int playerId, int[] cardIds) { if (_currentPhase != GamePhase.PlayersTurn) { throw new InvalidOperationException("Not allowed during Czar turn"); } bool success = Players.TryGetValue(playerId, out var player); if (!success) { throw new ArgumentException("Player with provided ID is not present in the game"); } if (_cardsOnTheTable.ContainsKey(playerId)) { throw new ArgumentException("Player with provided ID already played this turn"); } if (CurrentCzar.Id == playerId) { throw new ArgumentException("Czar can not play white cards"); } List <int> cardIndices = new List <int>(); foreach (int cardId in cardIds) { bool present = false; for (int i = 0; i < player.Hand.Count; i++) { if (player.Hand[i].Id == cardId) { cardIndices.Add(i); present = true; break; } } if (!present) { throw new ArgumentException($"Card with ID {cardId} is not present in players hand"); } } Card[] playedCards = new Card[cardIndices.Count]; for (int i = 0; i < playedCards.Length; i++) { playedCards[i] = player.Hand[cardIndices[i]]; player.Hand[cardIndices[i]] = WhiteCards.DrawCard(); } _cardsOnTheTable.Add(playerId, playedCards); }
/// <summary> /// Checks if card is already in the coresponding deck /// </summary> /// <param name="card">Black/White card</param> /// <returns>True if card is in the deck, otherwise false</returns> public bool IsCardAlreadyInDeck(dynamic card) { switch (card) { case BlackCard blackCard: return(BlackCards.Any(x => x.OriginalText == blackCard.OriginalText)); case WhiteCard whiteCard: return(WhiteCards.Any(x => x.OriginalText == whiteCard.OriginalText)); default: return(false); } }
public void Start() { _currentPhase = GamePhase.PlayersTurn; CurrentBlack = BlackCards.DrawCard(); _cardsOnTheTable = new Dictionary <int, Card[]>(); _playerIds = new List <int>(); foreach (var player in Players) { _playerIds.Add(player.Key); } _currentCzarIndex = 0; CurrentCzar = Players[_playerIds[_currentCzarIndex]]; foreach (var player in Players.Values) { for (int i = 0; i < 10; i++) { player.Hand.Add(WhiteCards.DrawCard()); } } }
/// <summary> /// Adds card to the coresponding base if it's not empty or already in deck /// </summary> /// <param name="card">White/Black card to be added</param> public void AddCardToList(dynamic card) { switch (card) { case BlackCard blackCard: if (!(IsCardAlreadyInDeck(blackCard) || string.IsNullOrEmpty(blackCard.OriginalText))) { BlackCards.Add(blackCard); } break; case WhiteCard whiteCard: if (!(IsCardAlreadyInDeck(whiteCard) || string.IsNullOrEmpty(whiteCard.OriginalText))) { WhiteCards.Add(whiteCard); } break; default: break; } }
public void AddWhiteCard(string text) { WhiteCards.Add(text); }
public void RemoveDeck(Deck deck) { BlackCards.RemoveAll(x => deck.BlackCards.Find(y => x.Text == y.Text).Equals(x)); WhiteCards.RemoveAll(x => deck.WhiteCards.Find(y => x == y).Equals(x)); }
public void AddDeck(Deck deck) { BlackCards.AddRange(deck.BlackCards); WhiteCards.AddRange(deck.WhiteCards); }
public void RemoveWhiteCard(string text) { WhiteCards.Remove(WhiteCards.Find(x => x.Equals(text))); }