Class: Monotony::BasicProperty

Inherits:
PurchasableProperty show all
Defined in:
lib/monotony/basicproperty.rb

Overview

A property class representing the majority of properties on the board.

Instance Attribute Summary collapse

Attributes inherited from PurchasableProperty

#cost, #is_mortgaged, #mortgage_value, #owner, #value

Attributes inherited from Square

#action, #colour, #name, #owner

Instance Method Summary collapse

Methods inherited from PurchasableProperty

#give_to, #is_mortgaged?, #number_in_set, #number_of_set_owned, #place_offer, #properties_in_set, #sell_to, #set_owned?, #unmortgage

Constructor Details

#initialize(opts) ⇒ BasicProperty

Returns a new instance of BasicProperty.

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :rent (Array<Integer>)

    An array of six elements containing rent values for an property with no houses, one house, two houses, three houses, four houses, and a hotel.

  • :house_cost (Integer)

    The cost of purchasing a house on this property.

  • :hotel_cost (Integer)

    The cost of purchasing a hotel on this property. Traditionally equal to house_cost.

  • :set (Symbol)

    A symbol identifying this property as a member of a set of properties.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/monotony/basicproperty.rb', line 24

def initialize(opts)
  super
  @rent = opts[:rent]
  @house_cost = opts[:house_cost]
  @hotel_cost = opts[:hotel_cost]
  @set = opts[:set]
  @num_houses = 0
  @num_hotels = 0
  @action = Proc.new do |game, owner, player, property|
    if owner
      if set_owned?
        rent_to_pay = (property.num_hotels  == 1 ? property.rent[5] : ( property.num_houses == 0 ? (@rent[0] * 2) :  property.rent[property.num_houses] ) )
      else
        rent_to_pay = property.rent[0]
      end
      if owner != player
        if not owner.is_out? and not is_mortgaged?
          puts '[%s] Due to pay £%d rent to %s for landing on %s with %s' % [ player.name, rent_to_pay, owner.name, property.name, ( property.num_hotels == 1 ? 'a hotel' : '%d houses' % property.num_houses) ]
          player.pay(owner, rent_to_pay)
        end
      end
    else
      player.behaviour[:purchase_possible].call(game, player, self) if player.currency >= cost
    end
  end
end

Instance Attribute Details

#hotel_costInteger

Returns the cost of purchasing a hotel on this property.

Returns:

  • (Integer)

    Returns the cost of purchasing a hotel on this property.



10
11
12
# File 'lib/monotony/basicproperty.rb', line 10

def hotel_cost
  @hotel_cost
end

#house_costInteger

Returns the cost of purchasing a house on this property.

Returns:

  • (Integer)

    Returns the cost of purchasing a house on this property.



8
9
10
# File 'lib/monotony/basicproperty.rb', line 8

def house_cost
  @house_cost
end

#num_hotelsInteger

Returns the number of hotels on this property.

Returns:

  • (Integer)

    Returns the number of hotels on this property.



16
17
18
# File 'lib/monotony/basicproperty.rb', line 16

def num_hotels
  @num_hotels
end

#num_housesInteger

Returns the number of houses on this property.

Returns:

  • (Integer)

    Returns the number of houses on this property.



14
15
16
# File 'lib/monotony/basicproperty.rb', line 14

def num_houses
  @num_houses
end

#rentArray<Integer>

Returns an array of six elements containing rent values for an property with no houses, one house, two houses, three houses, four houses, and a hotel.

Returns:

  • (Array<Integer>)

    Returns an array of six elements containing rent values for an property with no houses, one house, two houses, three houses, four houses, and a hotel.



18
19
20
# File 'lib/monotony/basicproperty.rb', line 18

def rent
  @rent
end

#setSymbol

Returns the name of the set containing this property.

Returns:

  • (Symbol)

    Returns the name of the set containing this property.



12
13
14
# File 'lib/monotony/basicproperty.rb', line 12

def set
  @set
end

Instance Method Details

#add_hotelself

Buy a hotel on the property.

Returns:

  • (self)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/monotony/basicproperty.rb', line 107

def add_hotel
  if @num_houses == 4
    if @owner.game.num_houses > 0
      if @owner.currency < @hotel_cost
        puts '[%s] Unable to buy a hotel! (short of cash by £%d)' % [ @owner.name, (@hotel_cost - @owner.currency) ]
      else
        @owner.currency = @owner.currency - @hotel_cost
        @num_houses, @num_hotels = 0, 1
        @owner.game.num_houses = @owner.game.num_houses + 4
        @owner.game.num_hotels = @owner.game.num_hotels - 1
        puts '[%s] Purchased a hotel on %s for £%d (new balance: £%d)' % [ @owner.name, @name, @hotel_cost, @owner.currency ]
      end      
    else
      puts '[%s] Not enough hotels left to purchase one for %s' % [ @owner.name, @name ]
    end
  end
  self
end

#add_houses(number) ⇒ self

Buy houses on the property.

Parameters:

  • number (Integer)

    number of houses to add to the property.

Returns:

  • (self)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/monotony/basicproperty.rb', line 65

def add_houses(number)
  housing_value = @house_cost * number
  if @owner.game.num_houses >= number
    if (@num_houses + number) > 4
      puts '[%s] Cannot place more than 4 houses on %s' % [ @owner.name, @name ]
    else
      if @owner.currency < housing_value
        puts '[%s] Unable to buy %d houses! (short of cash by £%d)' % [ @owner.name, number, (housing_value - @owner.currency) ]
        false
      else
        @owner.currency = @owner.currency - housing_value
        @owner.game.num_houses = @owner.game.num_houses - number
        @num_houses = @num_houses + number
        puts '[%s] Purchased %d houses on %s for £%d (new balance: £%d)' % [ @owner.name, number, @name, housing_value, @owner.currency ]
        true
      end
    end
  else
    puts '[%s] Not enough houses left to purchase %d more for %s' % [ @owner.name, number, @name ]
  end
  self
end

#display_house_asciivoid

This method returns an undefined value.

Draw an ASCII representation of this property’s housing.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/monotony/basicproperty.rb', line 155

def display_house_ascii
  house_array = []
  house_string = ''

  if @num_hotels == 1
    house_string << '   '.colorize(:background => :light_black) + '     '.colorize(:background => :red) + '   '.colorize(:background => :light_black)
  else
    until house_array.length == @num_houses
      house_array << '  '.colorize(:background => :green) 
    end
    until house_array.length == 4
      house_array << '  '.colorize(:background => :light_black)
    end

    house_string = house_array.join(' '.colorize(:background => :light_black))
  end
  house_string + ' '.colorize(:color => :default)
end

#mortgageself

Mortgage the property to raise cash for its owner.

Returns:

  • (self)


53
54
55
56
57
58
59
60
# File 'lib/monotony/basicproperty.rb', line 53

def mortgage
  super
  properties_in_set(@owner.game).each do |other|
    other.sell_hotel if @num_hotels > 0
    other.sell_houses if @num_houses > 0
  end
  self
end

#sell_hotelself

Sell hotels from the property.

Returns:

  • (self)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/monotony/basicproperty.rb', line 128

def sell_hotel
  if @num_hotels < 1
    puts "[%s] Can't sell hotel on %s, as there isn't one!" % [ @owner.name, @name ]
  else
     housing_value = (@hotel_cost / 2) 
    @num_hotels = 0
    @owner.game.num_hotels = @owner.game.num_hotels + 1
    @owner.currency = @owner.currency + housing_value
    puts '[%s] Sold hotel on %s for £%d' % [ @owner.name, @name, housing_value ]
    case @owner.game.num_houses
    when 1..3
      sell_houses(4 - @owner.game.num_houses)
      puts '[%s] Devolved %s to %d houses as 4 were not available' % [ @owner.name, @name, @num_houses ]
    when 0
      sell_houses(4)
      puts '[%s] Devolved to undeveloped site as no houses were available' % [ @owner.name, @name, @num_houses ]
    else
      @owner.game.num_houses = @owner.game.num_houses - 4
      @num_houses = 4
      puts '[%s] Devolved %s to %d houses' % [ @owner.name, @name, @num_houses ]
    end
  end
  self
end

#sell_houses(number) ⇒ self

Sell houses from the property.

Parameters:

  • number (Integer)

    number of houses to sell from the property.

Returns:

  • (self)


91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/monotony/basicproperty.rb', line 91

def sell_houses(number)
  housing_value = (@house_cost / 2) * number
  if number > @num_houses
    puts "[%s] Can't sell %d houses on %s, as there are only %d" % [ @owner.name, number, @name, @num_houses ]
    false
  else
    @num_houses = @num_houses - number
    @owner.game.num_houses = @owner.game.num_houses + number
    @owner.currency = @owner.currency + housing_value
    puts '[%s] Sold %d houses on %s for £%d (%d remaining)' % [ @owner.name, number, @name, housing_value, @num_houses ]
  end
  self
end