public virtual void CreateMultipleBuildedCards(Card selectedCard, Table table) { Rank buildingRankCard = SelectBuildingRank(selectedCard); Table cardsSelectedFromTheTable = SelectCardsFromTheTable(table); List <Rank> ranksFromTheTable = new List <Rank> { selectedCard.Rank }; bool THERE_ARE_CARDS = cardsSelectedFromTheTable.Cards != null; bool THERE_ARE_BUILDED_CARDS = cardsSelectedFromTheTable.BuildedCards != null; if (THERE_ARE_CARDS) { cardsSelectedFromTheTable.Cards.ForEach(c => ranksFromTheTable.Add(c.Rank)); } if (THERE_ARE_BUILDED_CARDS) { cardsSelectedFromTheTable.BuildedCards.ForEach(b => ranksFromTheTable.Add(b.BuildedCardsRank)); } if ((ranksFromTheTable.Sum(r => Convert.ToInt32(r)) % Convert.ToInt32(buildingRankCard) == 0 || buildingRankCard == Rank.Ace) && ValidateMultipleBuildedCardsWithAce(ranksFromTheTable)) { BuildedCard buildedCard = new BuildedCard { BuildedCardsRank = buildingRankCard, IsMultiple = true, Owner = this.Name, Name = CardUtils.CreateRankName(buildingRankCard), BuildedCards = new List <Card>() }; buildedCard.BuildedCards.Add(selectedCard); if (THERE_ARE_CARDS) { cardsSelectedFromTheTable.Cards.ForEach(c => buildedCard.BuildedCards.Add(c)); cardsSelectedFromTheTable.Cards.ForEach(item => table.Cards .RemoveAll(b => b.Rank == item.Rank)); } if (THERE_ARE_BUILDED_CARDS) { foreach (List <Card> card in cardsSelectedFromTheTable.BuildedCards.Select(c => c.BuildedCards)) { buildedCard.BuildedCards.AddRange(card); } table.BuildedCards.RemoveAll(b => cardsSelectedFromTheTable.BuildedCards.Contains(b)); } if (table.BuildedCards == null) { table.BuildedCards = new List <BuildedCard>(); table.BuildedCards.Add(buildedCard); } else { table.BuildedCards.Add(buildedCard); } this.Cards.RemoveAll(c => c.Name == selectedCard.Name); ConsoleOutput.ShowTableCards(table); } else { ConsoleOutput.YouJustLostYourCardBecauseItIsInvalid(); this.Cards.RemoveAll(c => c.Name == selectedCard.Name); ThrowTheCardToTheTable(selectedCard, table); } }
public virtual void CreateSingleBuildedCards(Card selectedCard, Table table) { Rank buildingRankCard = SelectBuildingRank(selectedCard); var cardsSelectedFromTheTable = SelectCardsFromTheTable(table); bool tableBuildedCardsWasModified = false; if (cardsSelectedFromTheTable.BuildedCards == null && ((buildingRankCard != Rank.Ace && cardsSelectedFromTheTable.Cards.Sum(r => Convert.ToInt32(r.Rank)) + Convert.ToInt32(selectedCard.Rank) == Convert.ToInt32(buildingRankCard)) || (buildingRankCard == Rank.Ace && cardsSelectedFromTheTable.Cards.Sum(r => Convert.ToInt32(r.Rank)) + Convert.ToInt32(selectedCard.Rank) == Constants.ACE_MAX_VALUE))) { Cards.RemoveAll(c => c.Name == selectedCard.Name); table.Cards.RemoveAll(c => cardsSelectedFromTheTable.Cards.Contains(c)); cardsSelectedFromTheTable.Cards.Add(selectedCard); BuildedCard buildedCard = new BuildedCard { BuildedCards = cardsSelectedFromTheTable.Cards, BuildedCardsRank = buildingRankCard, Owner = this.Name, Name = CardUtils.CreateRankName(buildingRankCard) }; if (table.BuildedCards == null) { buildedCard.IsMultiple = false; } else if (table.BuildedCards.Any(c => c.BuildedCardsRank == buildedCard.BuildedCardsRank)) { table.BuildedCards.Where(c => c.BuildedCardsRank == buildedCard.BuildedCardsRank) .FirstOrDefault().IsMultiple = true; table.BuildedCards.Where(c => c.BuildedCardsRank == buildedCard.BuildedCardsRank) .FirstOrDefault().BuildedCards .AddRange(buildedCard.BuildedCards); tableBuildedCardsWasModified = true; } if (table.BuildedCards == null) { table.BuildedCards = new List <BuildedCard> { buildedCard }; } else if (table.BuildedCards != null && !tableBuildedCardsWasModified) { table.BuildedCards.Add(buildedCard); } ConsoleOutput.ShowTableCards(table); } else if (cardsSelectedFromTheTable.BuildedCards != null && cardsSelectedFromTheTable.Cards == null && ((buildingRankCard != Rank.Ace && cardsSelectedFromTheTable.BuildedCards.Sum(r => Convert.ToInt32(r.BuildedCardsRank)) + Convert.ToInt32(selectedCard.Rank) == Convert.ToInt32(buildingRankCard)) || buildingRankCard == Rank.Ace && cardsSelectedFromTheTable.BuildedCards.Sum(r => Convert.ToInt32(r.BuildedCardsRank)) + Convert.ToInt32(selectedCard.Rank) == Convert.ToInt32(Constants.ACE_MAX_VALUE)) && !tableBuildedCardsWasModified) { cardsSelectedFromTheTable.BuildedCards.ForEach(item => table.BuildedCards .RemoveAll(b => b.BuildedCardsRank == item.BuildedCardsRank)); cardsSelectedFromTheTable.BuildedCards.FirstOrDefault().IsMultiple = false; cardsSelectedFromTheTable.BuildedCards.FirstOrDefault().BuildedCardsRank = buildingRankCard; cardsSelectedFromTheTable.BuildedCards.FirstOrDefault().Owner = this.Name; cardsSelectedFromTheTable.BuildedCards.FirstOrDefault().BuildedCards .Add(selectedCard); table.BuildedCards.Add(cardsSelectedFromTheTable.BuildedCards.FirstOrDefault()); this.Cards.RemoveAll(c => c.Name == selectedCard.Name); ConsoleOutput.ShowTableCards(table); } else { ConsoleOutput.YouJustLostYourCardBecauseItIsInvalid(); ThrowTheCardToTheTable(selectedCard, table); } }