public void ConsiderPurchase(Asset asset) { if (Money > asset.Price) { var value = Genetics.GetGeneExpression(GeneType.PropertyValue, asset); if ((value * Money) > asset.Price) { PurchaseAsset(asset); } } }
public void Pay(int amount, IAssetHolder payee, Bank bank) { LogEvent("Pay", amount); if (amount < 0) { throw new ApplicationException("Can't pay less than 0"); } if ((Money - amount) < 0) { // can't afford it var sumReached = false; while (sumReached == false) { var assetToSellBuilding = Properties.Where(b => b.BuildingCount > 0) .Where(c => c.MonopolySet.Max(m => (((Property)m).BuildingCount)) == c.BuildingCount) .OrderBy(o => Genetics.GetGeneExpression(GeneType.ImprovementValue, o)) .FirstOrDefault(); if (assetToSellBuilding != null) { Money += assetToSellBuilding.SellBuilding(); if ((Money - amount) >= 0) { sumReached = true; } } else // no buildings, start mortgaging { var assetForMortaging = Assets.Where(x => x.Mortgaged == false) .OrderBy(o => Genetics.GetGeneExpression(GeneType.PropertyValue, o)) .FirstOrDefault(); if (assetForMortaging != null) { Money += assetForMortaging.MortgageAsset(); if ((Money - amount) >= 0) { sumReached = true; } } else { // Nothing more to mortgage, broke! break; } } } if (sumReached == false) { // Couldn't pay. Hand over all assets to debtor if (payee is FreeParking || payee is Bank) { foreach (var asset in Assets) { asset.Owner = bank; bank.Assets.Add(asset); asset.UnmortgageAsset(); } } else { foreach (var asset in Assets) { asset.Owner = payee; payee.Assets.Add(asset); } } Assets.Clear(); // Lose get out of jail free cards while (GetOutOfJailFreeCards.Count > 0) { GetOutOfJailFreeCards.Dequeue(); } // All money turned over payee.Receive(Money); Money = 0; Status = PlayerStatus.Bankrupt; LogEvent("Bankrupt trying to pay ", amount); return; } } Money -= amount; payee.Receive(amount); }
internal void Trade(List <Player> players) { var possibleExchanges = new List <Tuple <Asset, Asset, double> >(); foreach (var myAsset in Assets.Where(a => ((a is Property) && ((Property)a).BuildingCount == 0) || ((a is Property) == false))) { foreach (var otherPlayer in players.Excluding(this)) { foreach (var theirAsset in otherPlayer.Assets.Where(a => ((a is Property) && ((Property)a).BuildingCount == 0) || ((a is Property) == false))) { var myValueOfMine = Genetics.GetGeneExpression(GeneType.PropertyValue, myAsset) * myAsset.Price; var myValueOfTheirs = Genetics.GetGeneExpression(GeneType.PropertyValue, theirAsset) * theirAsset.Price; var theirValueOfMine = otherPlayer.Genetics.GetGeneExpression(GeneType.PropertyValue, myAsset) * myAsset.Price; var theirValueOfTheirs = otherPlayer.Genetics.GetGeneExpression(GeneType.PropertyValue, theirAsset) * theirAsset.Price; // Double the value for monopolies if (myAsset.OwnerHasMonopoly) { myValueOfMine *= 2; } if (theirAsset.OwnerHasMonopoly) { theirValueOfTheirs *= 2; } if (theirAsset.MonopolySet.Excluding(theirAsset).ToList().TrueForAll(o => o.Owner == this)) { myValueOfTheirs *= 2; } if (myAsset.MonopolySet.Excluding(myAsset).ToList().TrueForAll(o => o.Owner == otherPlayer)) { theirValueOfMine *= 2; } if (theirValueOfMine > myValueOfMine && theirValueOfTheirs < myValueOfTheirs) { // Don't swap if i'm not net gaining net value (although the swap will likely happen on their turn) if ((myValueOfTheirs - myValueOfMine) > 0) { possibleExchanges.Add(new Tuple <Asset, Asset, double>(myAsset, theirAsset, myValueOfTheirs - myValueOfMine)); } } } } } // Select the best if multiple exchanges exist var bestExchanges = new List <Tuple <Asset, Asset> >(); //foreach (var myAsset in possibleExchanges.Select(x => x.Item1).Distinct()) { var bestSwap = possibleExchanges.OrderByDescending(o => o.Item3).FirstOrDefault(); if (bestSwap != null) { //bestExchanges.Add(new Tuple<Asset, Asset>(bestSwap.Item1, bestSwap.Item2)); var otherPlayer = bestSwap.Item2.Owner; var myAsset = bestSwap.Item1; var theirAsset = bestSwap.Item2; LogEvent("Swapped " + myAsset.Name + " for " + theirAsset.Name); this.Assets.Remove(myAsset); otherPlayer.Assets.Add(myAsset); myAsset.Owner = otherPlayer; otherPlayer.Assets.Remove(theirAsset); this.Assets.Add(theirAsset); theirAsset.Owner = this; } } }
public void ConsiderUnmortgaging() { while (true) { var assetToUnmortgage = Assets.Where(a => a.Mortgaged && (a.MortgageValue * 1.1) < Money).OrderByDescending(o => Genetics.GetGeneExpression(GeneType.PropertyValue, o)).FirstOrDefault(); if (assetToUnmortgage != null) { UnmortgageAsset(assetToUnmortgage); } else { break; } } }
public void ConsiderBuilding() { bool bContinue = true; while (bContinue) { var assetToBuildOn = Properties.Where(p => p.OwnerHasMonopoly && p.BuildingCost <= Money && p.BuildingCount < 5 && p.BuildingPossible).OrderByDescending(o => Genetics.GetGeneExpression(GeneType.ImprovementValue, o)).FirstOrDefault(); if (assetToBuildOn != null) { PurchaseBuilding(assetToBuildOn); } else { bContinue = false; } } }