private List <Card> GetAvaivableCardsToBuild() { var freeCards = GetFreeCards(); var avaivableCards = new List <Card>(); foreach (var card in CardsOnHand.Except(freeCards)) { var necessaryResources = new Dictionary <ResourceType, int>(); // calculating all necessary resources for each card foreach (var cardResource in card.Price) { int difference = cardResource.Value - AvaivableResources[cardResource.Key]; if (difference > 0) { necessaryResources.Add(cardResource.Key, difference); } } // this if shouldn't executed if (necessaryResources.All(r => r.Value == 0)) { avaivableCards.Add(card); continue; } int summaryPrice = 0; // calculating is it enough resources for trade from neighbours and is it enough coins to buy it foreach (var necessaryResource in necessaryResources) { var left = LeftNeighbourResources.First(r => r.ResourceType == necessaryResource.Key); var right = RightNeighbourResources.First(r => r.ResourceType == necessaryResource.Key); var count = left.Count + right.Count; if (necessaryResource.Value > count) { break; } var minimal = left.Price <= right.Price ? left : right; var maximum = left.Price <= right.Price ? right : left; var toBuyCheap = Math.Min(necessaryResource.Value, minimal.Count); summaryPrice += toBuyCheap * minimal.Price; var toBuyExpensive = necessaryResource.Value - toBuyCheap; summaryPrice += toBuyExpensive * maximum.Price; } if (summaryPrice <= Coins) { avaivableCards.Add(card); } } return(avaivableCards); }
// returns cards which player can build by his resources or by links between cards private List <Card> GetFreeCards() { var result = new List <Card>(); var linkedCards = CardsOnHand.Where(c => BuildCards.Select(bc => bc.UniqCardId).Contains(c.ParentUniqCard)) .ToList(); foreach (var card in CardsOnHand.Except(linkedCards)) { if (card.CardType == (int)CardType.ResourceCard) { if (Coins >= card.CoinsCost) { result.Add(card); } continue; } // var isEnough = card.Price.All(resource => AvaivableResources[resource.Key] > resource.Value); // if(isEnough) result.Add(card); } result.AddRange(linkedCards); return(result); }