public void BuyHouse(Player player, Property testProperty = null, bool?testAnswer = null) { if (Board.Access().Houses < 1) { Console.WriteLine("Sorry, the bank doesn't have any houses left."); return; } if (player.IsInJail) { Console.WriteLine("Sorry, you are in jail."); return; } //create prompt var sPrompt = String.Format("{0}Please select a property to buy a house for:", PlayerPrompt(player)); //create variable for propertyToBuy Residential propertyToBuyFor = null; if (player.GetPropertiesOwnedFromBoard().Count == 0) { //write message Console.WriteLine("{0}You do not own any properties.", PlayerPrompt(player)); //return from method return; } //get the property to buy house for var property = testProperty ?? DisplayPropertyChooser(player.GetPropertiesOwnedFromBoard(), sPrompt); //if dont own any properties //check that it is a residential if (property.GetType() == (new Residential().GetType())) { //cast to residential property propertyToBuyFor = (Residential)property; } else //else display msg { Console.WriteLine("{0}A house can not be bought for {1} because it is not a Residential Property.", this.PlayerPrompt(player), property.GetName()); return; } if (propertyToBuyFor.IsMortgaged) { Console.WriteLine("{0}A house can not be bought for {1} because it is currently mortgaged.", PlayerPrompt(player), property.GetName()); return; } // Player must own all the propeties in the colour group if (!player.OwnsAllPropertiesOfColour(propertyToBuyFor.GetHouseColour())) { Console.WriteLine( "You must own all the properties within this colour group ({0}) to buy houses for this property.", propertyToBuyFor.GetHouseColour()); return; } // We've checked if they own the properties of the colour, now // check if each property is equally developed if (!player.CanDevelopPropertyFurther(propertyToBuyFor)) { Console.WriteLine("Each property in this colour group needs to have houses uniformly added"); } else if (propertyToBuyFor.HasHotel)// If it has a hotel they can't add more houses. { Console.WriteLine("This property has a hotel and can not be developed further."); } else { // Can't buy a fifth house if there are not hotels left if (propertyToBuyFor.GetHouseCount() == 4 && Board.Access().Hotels < 1) { Console.WriteLine("You can't buy another house as this would result in a hotel and there aren't any hotels left."); return; } //confirm var doBuyHouse = testAnswer ?? GetInputYn(player, string.Format("You chose to buy a house for {0}. Are you sure you want to purchase a house for ${1}?", propertyToBuyFor.GetName(), propertyToBuyFor.GetHouseCost())); //if confirmed if (doBuyHouse) { //buy the house propertyToBuyFor.AddHouse(); Console.WriteLine("{0}A new house for {1} has been bought successfully", PlayerPrompt(player), propertyToBuyFor.GetName()); } } }