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 Play(Table table) { SelectCard: Card selectedCard = SelectYourCard(); ConsoleOutput.ChooseOneAction(table); string userinput = ""; while (userinput == "") { userinput = Console.ReadLine().Trim(); if (userinput != Keyboard.ONE && userinput != Keyboard.TWO && userinput != Keyboard.THREE && userinput != Keyboard.FOUR) { userinput = ""; ConsoleOutput.ThisIsNotAnAllowedAction(); ConsoleOutput.ChooseOneAction(table); } else if (table.BuildedCards != null && table.BuildedCards.Any(b => b.Owner == this.Name && userinput == Keyboard.ONE)) { userinput = ""; ConsoleOutput.YouCannotThrowCardsWhenYouAreABuildingCardOwner(); selectedCard = SelectYourCard(); ConsoleOutput.ChooseOneAction(table); } } bool buildedCardOwner = false; switch (userinput) { case Keyboard.ONE: ThrowTheCardToTheTable(selectedCard, table); break; case Keyboard.TWO: buildedCardOwner = TakeCardFromTheTable(selectedCard, table); break; case Keyboard.THREE: CreateSingleBuildedCards(selectedCard, table); break; case Keyboard.FOUR: CreateMultipleBuildedCards(selectedCard, table); break; } if (buildedCardOwner == true) { goto SelectCard; } }
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); } }
private Table SelectCardsFromTheTable(Table table) { int cardsOnTheTable = ConsoleOutput.WhichCardWouldYouLikeToTakeFromTheTable(table); ConsoleOutput.PressFWhenFinished(); string cardRank = ""; Table cardsSelectedFromTheTable = new Table { Cards = new List <Card>(), BuildedCards = new List <BuildedCard>() }; while (cardRank != Keyboard.UPPERCASE_F && cardRank != Keyboard.LOWERCASE_F) { cardRank = Console.ReadLine().Trim(); if (string.IsNullOrEmpty(cardRank)) { ConsoleOutput.TypeValidCardNumber(); continue; } else if (cardRank != Keyboard.UPPERCASE_F && cardRank != Keyboard.LOWERCASE_F && cardRank.All(char.IsDigit) && Enumerable.Range((int)General.Zero, cardsOnTheTable).Contains(int.Parse(cardRank))) { bool isBuildedCard = ConsoleOutput.YouSelected(table, cardRank); if (!isBuildedCard) { cardsSelectedFromTheTable.Cards.Add(table.Cards.ElementAt(int.Parse(cardRank))); } else { int buildedCardsSelected = int.Parse(cardRank) - table.Cards.Count; cardsSelectedFromTheTable.BuildedCards.Add(table.BuildedCards.ElementAt(buildedCardsSelected)); } } else if (cardRank == Keyboard.UPPERCASE_F || cardRank == Keyboard.LOWERCASE_F) { break; } else { ConsoleOutput.TypeValidCardNumber(); cardRank = ""; continue; } } if (!cardsSelectedFromTheTable.Cards.Any()) { cardsSelectedFromTheTable.Cards = null; } if (!cardsSelectedFromTheTable.BuildedCards.Any()) { cardsSelectedFromTheTable.BuildedCards = null; } return(cardsSelectedFromTheTable); }