class Monotony::Player

Represents a player

Attributes

behaviour[RW]
board[RW]
currency[RW]
game[RW]
history[RW]
hits[RW]
in_game[RW]
jail_free_cards[RW]
name[RW]
properties[RW]
turns_in_jail[RW]

Public Class Methods

new(args) click to toggle source

@return [Player] self @param [Hash] args @option opts [Hash] :behaviour Behaviour has describing this player’s reaction to certain in-game situations. See Behaviour class. @option opts [String] :name The name of the player

# 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

Public Instance Methods

bankrupt!(player = nil) click to toggle source

Declares a player as bankrupt, transferring their assets to their creditor. @param player [Player] the player to whom this player’s remaining assets will be transferred. If nil, assets are given to the bank instead.

# 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() click to toggle source

@return [Square] The square this player is currently on.

# File lib/monotony/player.rb, line 93
def current_square
        @board[0]
end
distance_to_go() click to toggle source

@return [Integer] the number of squares between this player’s current position on the board, and the GO square.

# 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) click to toggle source

Sets whether or not this player is currently in jail. @param [Boolean] bool True for in jail, False for out of jail.

# File lib/monotony/player.rb, line 51
def in_jail=(bool)
        @in_jail = bool
        @turns_in_jail = 0 if bool == false
end
in_jail?() click to toggle source

@return [Boolean] whether or not this player is currently in jail.

# File lib/monotony/player.rb, line 25
def in_jail?
        @in_jail
end
is_out?() click to toggle source

@return [Boolean] whether or not this player has been eliminated from the game.

# File lib/monotony/player.rb, line 134
def is_out?
        ! @in_game
end
money_trouble(amount) click to toggle source

Called when a player is unable to pay a debt. Calls the ‘money_trouble’ behaviour. @param [Integer] amount amount of currency to be raised. @return [Boolean] whether or not the player was able to raise the amount required.

# 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) click to toggle source

Moves a player on the game board. @param [Integer] n Number of squares to move. @param [Symbol] direction :forwards or :backwards. @return [Square] the square the player has landed on.

# 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() click to toggle source

@return [Integer] the number of hotels on properties owned by this player.

# 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() click to toggle source

@return [Integer] the number of houses on properties owned by this player.

# File lib/monotony/player.rb, line 35
def num_houses
        @properties.select { |p| p.is_a? BasicProperty }.collect(&:num_houses).inject(:+) || 0
end
opponents() click to toggle source

@return [Array<Player>] an array of all other players in the game.

# File lib/monotony/player.rb, line 30
def opponents
        @game.players.reject{ |p| p == self }
end
out!() click to toggle source

Declares a player as out of the game.

# 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) click to toggle source

Transfer currency to another player, or the bank. @return [Boolean] whether or not the player was able to pay the amount requested. False indicates bancruptcy. @param [Symbol] beneficiary target Player instance or :bank. @param [Integer] amount amount of currency to transfer. @param [String] description Reference for the transaction (for game log).

# 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
                paying_to = 'bank'
        when :free_parking
                @game.free_parking_balance = @game.free_parking_balance + amount_to_pay
                paying_to = 'free parking'
        when Player
                beneficiary.currency = beneficiary.currency + amount_to_pay
                paying_to = 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, paying_to, ( description ? ' for %s' % description : '' ), amount_to_pay ]
                bankrupt!(beneficiary)
                false
        else
                puts '[%s] Paid £%d to %s%s (balance: £%d)' % [ @name, amount, paying_to, ( description ? ' for %s' % description : '' ), @currency ]
                true
        end
end
roll() click to toggle source

Roll the dice! @return [Array<Integer>] dice roll as an array of num_dice integers between 1 and die_size.

# File lib/monotony/player.rb, line 187
def roll
        Array.new(@game.num_dice).collect { Random.rand(1..@game.die_size) }
end
sets_owned() click to toggle source

@return [Integer] the number of property sets owned by this player.

# 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!() click to toggle source

Use a ‘get out of jail free’ card to exit jail. @return [Boolean] whether the player was both in jail and had an unused jail card available.

# 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