Class: Monotony::Player
- Inherits:
-
Object
- Object
- Monotony::Player
- Defined in:
- lib/monotony/player.rb
Overview
Represents a player
Instance Attribute Summary collapse
-
#behaviour ⇒ Object
Returns the value of attribute behaviour.
-
#board ⇒ Object
Returns the value of attribute board.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#game ⇒ Object
Returns the value of attribute game.
-
#history ⇒ Object
Returns the value of attribute history.
-
#hits ⇒ Object
Returns the value of attribute hits.
-
#in_game ⇒ Object
Returns the value of attribute in_game.
-
#jail_free_cards ⇒ Object
Returns the value of attribute jail_free_cards.
-
#name ⇒ Object
Returns the value of attribute name.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#turns_in_jail ⇒ Object
Returns the value of attribute turns_in_jail.
Instance Method Summary collapse
-
#bankrupt!(player = nil) ⇒ Object
Declares a player as bankrupt, transferring their assets to their creditor.
-
#current_square ⇒ Square
The square this player is currently on.
-
#distance_to_go ⇒ Integer
The number of squares between this player’s current position on the board, and the GO square.
-
#in_jail=(bool) ⇒ Object
Sets whether or not this player is currently in jail.
-
#in_jail? ⇒ Boolean
Whether or not this player is currently in jail.
-
#initialize(args) ⇒ Player
constructor
Self.
-
#is_out? ⇒ Boolean
Whether or not this player has been eliminated from the game.
-
#money_trouble(amount) ⇒ Boolean
Called when a player is unable to pay a debt.
-
#move(n = 1, direction = :forwards) ⇒ Square
Moves a player on the game board.
-
#num_hotels ⇒ Integer
The number of hotels on properties owned by this player.
-
#num_houses ⇒ Integer
The number of houses on properties owned by this player.
-
#opponents ⇒ Array<Player>
An array of all other players in the game.
-
#out! ⇒ Object
Declares a player as out of the game.
-
#pay(beneficiary = :bank, amount = 0, description = nil) ⇒ Boolean
Transfer currency to another player, or the bank.
-
#roll ⇒ Array<Integer>
Roll the dice!.
-
#sets_owned ⇒ Integer
The number of property sets owned by this player.
-
#use_jail_card! ⇒ Boolean
Use a ‘get out of jail free’ card to exit jail.
Constructor Details
#initialize(args) ⇒ Player
Returns self.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/monotony/player.rb', line 9 def initialize(args) @history = [] @in_game = true @in_jail = false @turns_in_jail = 0 @jail_free_cards = 0 @currency = 0 @game = nil @name = args[:name] @board = [] @properties = [] @behaviour = args[:behaviour] || Monotony::DefaultBehaviour::DEFAULT self end |
Instance Attribute Details
#behaviour ⇒ Object
Returns the value of attribute behaviour.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def behaviour @behaviour end |
#board ⇒ Object
Returns the value of attribute board.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def board @board end |
#currency ⇒ Object
Returns the value of attribute currency.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def currency @currency end |
#game ⇒ Object
Returns the value of attribute game.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def game @game end |
#history ⇒ Object
Returns the value of attribute history.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def history @history end |
#hits ⇒ Object
Returns the value of attribute hits.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def hits @hits end |
#in_game ⇒ Object
Returns the value of attribute in_game.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def in_game @in_game end |
#jail_free_cards ⇒ Object
Returns the value of attribute jail_free_cards.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def jail_free_cards @jail_free_cards end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def name @name end |
#properties ⇒ Object
Returns the value of attribute properties.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def properties @properties end |
#turns_in_jail ⇒ Object
Returns the value of attribute turns_in_jail.
4 5 6 |
# File 'lib/monotony/player.rb', line 4 def turns_in_jail @turns_in_jail end |
Instance Method Details
#bankrupt!(player = nil) ⇒ Object
Declares a player as bankrupt, transferring their assets to their creditor.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/monotony/player.rb', line 99 def bankrupt!(player = nil) if player == nil puts '[%s] Bankrupt! Giving all assets to bank' % @name @properties.each do |property| property.owner = nil property.is_mortgaged = false end @properties = [] else puts '[%s] Bankrupt! Giving all assets to %s' % [ @name, player.name ] @properties.each { |p| p.owner = player } puts '[%s] Transferred properties to %s: %s' % [ @name, player.name, @properties.collect { |p| p.name }.join(', ') ] player.properties.concat @properties unless player == nil @properties = [] end out! end |
#current_square ⇒ Square
Returns The square this player is currently on.
93 94 95 |
# File 'lib/monotony/player.rb', line 93 def current_square @board[0] end |
#distance_to_go ⇒ Integer
Returns the number of squares between this player’s current position on the board, and the GO square.
57 58 59 60 |
# File 'lib/monotony/player.rb', line 57 def distance_to_go index = @board.collect(&:name).find_index('GO') index == 0 ? @board.length : index end |
#in_jail=(bool) ⇒ Object
Sets whether or not this player is currently in jail.
51 52 53 54 |
# File 'lib/monotony/player.rb', line 51 def in_jail=(bool) @in_jail = bool @turns_in_jail = 0 if bool == false end |
#in_jail? ⇒ Boolean
Returns whether or not this player is currently in jail.
25 26 27 |
# File 'lib/monotony/player.rb', line 25 def in_jail? @in_jail end |
#is_out? ⇒ Boolean
Returns whether or not this player has been eliminated from the game.
134 135 136 |
# File 'lib/monotony/player.rb', line 134 def is_out? ! @in_game end |
#money_trouble(amount) ⇒ Boolean
Called when a player is unable to pay a debt. Calls the ‘money_trouble’ behaviour.
121 122 123 124 125 |
# File 'lib/monotony/player.rb', line 121 def money_trouble(amount) puts '[%s] Has money trouble and is trying to raise £%d... (balance: £%d)' % [ @name, (amount - @currency), @currency ] @behaviour[:money_trouble].call(game, self, amount) @currency > amount end |
#move(n = 1, direction = :forwards) ⇒ Square
Moves a player on the game board.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/monotony/player.rb', line 66 def move(n = 1, direction = :forwards) n = @board.collect(&:name).find_index(n) if n.is_a? String case direction when :forwards if n >= distance_to_go unless in_jail? puts '[%s] Passed GO' % @name @game.pay_player(self, @game.go_amount, 'passing go') end end (n % @board.length).times { @board.push @board.shift } when :backwards n = @board.length - n (n % @board.length).times { @board.unshift @board.pop } end @history << @board[0].name @board[0] end |
#num_hotels ⇒ Integer
Returns the number of hotels on properties owned by this player.
40 41 42 |
# File 'lib/monotony/player.rb', line 40 def num_hotels @properties.select { |p| p.is_a? BasicProperty }.collect(&:num_hotels).inject(:+) || 0 end |
#num_houses ⇒ Integer
Returns the number of houses on properties owned by this player.
35 36 37 |
# File 'lib/monotony/player.rb', line 35 def num_houses @properties.select { |p| p.is_a? BasicProperty }.collect(&:num_houses).inject(:+) || 0 end |
#opponents ⇒ Array<Player>
Returns an array of all other players in the game.
30 31 32 |
# File 'lib/monotony/player.rb', line 30 def opponents @game.players.reject{ |p| p == self } end |
#out! ⇒ Object
Declares a player as out of the game.
128 129 130 131 |
# File 'lib/monotony/player.rb', line 128 def out! puts '[%s] is out of the game!' % @name @in_game = false end |
#pay(beneficiary = :bank, amount = 0, description = nil) ⇒ Boolean
Transfer currency to another player, or the bank.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/monotony/player.rb', line 157 def pay(beneficiary = :bank, amount = 0, description = nil) money_trouble(amount) if @currency < amount amount_to_pay = ( @currency >= amount ? amount : @currency ) case beneficiary when :bank @game.bank_balance = @game.bank_balance + amount_to_pay = 'bank' when :free_parking @game.free_parking_balance = @game.free_parking_balance + amount_to_pay = 'free parking' when Player beneficiary.currency = beneficiary.currency + amount_to_pay = beneficiary.name end @currency = @currency - amount_to_pay if amount_to_pay < amount then puts '[%s] Unable to pay £%d to %s%s! Paid £%d instead' % [ @name, amount, , ( description ? ' for %s' % description : '' ), amount_to_pay ] bankrupt!(beneficiary) false else puts '[%s] Paid £%d to %s%s (balance: £%d)' % [ @name, amount, , ( description ? ' for %s' % description : '' ), @currency ] true end end |
#roll ⇒ Array<Integer>
Roll the dice!
187 188 189 |
# File 'lib/monotony/player.rb', line 187 def roll Array.new(@game.num_dice).collect { Random.rand(1..@game.die_size) } end |
#sets_owned ⇒ Integer
Returns the number of property sets owned by this player.
45 46 47 |
# File 'lib/monotony/player.rb', line 45 def sets_owned @properties.select { |p| p.is_a? BasicProperty }.select(&:set_owned?).group_by { |p| p.set }.keys end |
#use_jail_card! ⇒ Boolean
Use a ‘get out of jail free’ card to exit jail.
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/monotony/player.rb', line 140 def use_jail_card! if @jail_free_cards > 0 and @in_jail puts "[%s] Used a 'get out of jail free' card!" % @name @in_jail = false @turns_in_jail = 0 @jail_free_cards = @jail_free_cards - 1 true else false end end |