public bool CanDevelopPropertyFurther(Residential propertyToBuyHouseFor)
        {
            var propertiesOfColour           = Board.Access().GetAllPropertiesOfColour(propertyToBuyHouseFor.GetHouseColour());
            var propertiesExcludingDevelopee = propertiesOfColour.Where(property => property != propertyToBuyHouseFor);

            /***
             * In order to build a house on a property, the player must build houses
             * uniformly across the properties of a colour. That means the player can't
             * build a second house on a property of a colour until all properties of
             * that colour have one house.
             * So each property (excluding the one in question) should have the same amount
             * of houses as the one in question. Minus one for the case where a property in the
             * colour group might have 1 house and the one in question has 0, they aren't equal
             * so we need to minus 1 from the property that has a house.
             * */
            return(propertiesExcludingDevelopee
                   .All(property =>
                        property.GetHouseCount() == (propertyToBuyHouseFor.GetHouseCount()) ||
                        property.GetHouseCount() - 1 == (propertyToBuyHouseFor.GetHouseCount())));
        }
Ejemplo n.º 2
0
        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());
                }
            }
        }
        public bool CanDevelopPropertyFurther(Residential propertyToBuyHouseFor)
        {
            var propertiesOfColour = Board.Access().GetAllPropertiesOfColour(propertyToBuyHouseFor.GetHouseColour());
            var propertiesExcludingDevelopee = propertiesOfColour.Where(property => property != propertyToBuyHouseFor);

            /***
             * In order to build a house on a property, the player must build houses 
             * uniformly across the properties of a colour. That means the player can't 
             * build a second house on a property of a colour until all properties of
             * that colour have one house.
             * So each property (excluding the one in question) should have the same amount 
             * of houses as the one in question. Minus one for the case where a property in the
             * colour group might have 1 house and the one in question has 0, they aren't equal
             * so we need to minus 1 from the property that has a house.
             * */
            return propertiesExcludingDevelopee
                .All(property =>
                    property.GetHouseCount() == (propertyToBuyHouseFor.GetHouseCount()) ||
                    property.GetHouseCount() - 1 == (propertyToBuyHouseFor.GetHouseCount()));
        }