-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.rb
87 lines (87 loc) · 2.28 KB
/
player.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Player
attr_accessor :properties, :funds, :position, :token, :in_jail, :jail_turns, :bankrupt, :monopolies
def initialize(token)
@token = token
@properties = []
@funds = 1500
@position = 0
@in_jail = false
@jail_turns = 0
@bankrupt = false
@monopolies = [] # [ "color_name" ]
end
# - - -
# Actions
def move(number_of_spaces)
@position += number_of_spaces
if @position > 39
@position = @position - 40
end
return @position
end
# - - -
# Purchases
def pay_rent(property)
rent = property.rent
self.pay_fee(rent)
property.owner.earn_money(rent)
property.total_value += rent
end
def buy_property(property)
property.owner = self
self.pay_fee(property.price)
@properties << property
# check if monopoly
if @properties.count { |p| p.color == property.color } == property.color_count
@monopolies << property.color
@monopolies.uniq!
end
end
def buy_house_on_property(property)
return false if property.houses == 5 || property.owner != self || !(@monopolies.find_index(property.color)) || @funds < property.build_price
property.houses += 1
self.pay_fee(property.build_price)
return true
end
def unmortgage_property(property)
property.mortgaged = false
self.pay_fee(property.price/2)
end
def mortgage_property(property)
property.mortgaged = true
self.earn_money(property.price/2)
end
def sell_house_on_property(property)
return if property.houses == 0 || property.owner != self
property.houses -= 1
self.earn_money(property.build_price/2)
end
def go_broke
puts "!!!! #{@token} GOES BROKE !!!!"
@funds = 0
@bankrupt = true
@monopolies = []
@in_jail = false
@properties.each { |p| p.owner = nil; p.mortgaged = false; p.houses = 0 }
end
# - - -
# Transactions
def pay_fee(fee)
@funds -= fee
end
def earn_money(revenue)
@funds += revenue
end
# - - -
# State
def monolopy_for_color(color)
return true if @monopolies.find_index(color)
return false
end
def number_of_railroads_owned
return @properties.count { |p| p.type == TileType::RAILROAD }
end
def number_of_utilities_owned
return @properties.count { |p| p.type == TileType::UTILITY }
end
end