Class: Monotony::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/monotony/player.rb

Overview

Represents a player

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Player

Returns self.

Parameters:

  • args (Hash)
  • opts (Hash)

    a customizable set of options



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

#behaviourObject

Returns the value of attribute behaviour.



4
5
6
# File 'lib/monotony/player.rb', line 4

def behaviour
  @behaviour
end

#boardObject

Returns the value of attribute board.



4
5
6
# File 'lib/monotony/player.rb', line 4

def board
  @board
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'lib/monotony/player.rb', line 4

def currency
  @currency
end

#gameObject

Returns the value of attribute game.



4
5
6
# File 'lib/monotony/player.rb', line 4

def game
  @game
end

#historyObject

Returns the value of attribute history.



4
5
6
# File 'lib/monotony/player.rb', line 4

def history
  @history
end

#hitsObject

Returns the value of attribute hits.



4
5
6
# File 'lib/monotony/player.rb', line 4

def hits
  @hits
end

#in_gameObject

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_cardsObject

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

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/monotony/player.rb', line 4

def name
  @name
end

#propertiesObject

Returns the value of attribute properties.



4
5
6
# File 'lib/monotony/player.rb', line 4

def properties
  @properties
end

#turns_in_jailObject

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.

Parameters:

  • player (Player) (defaults to: nil)

    the player to whom this player’s remaining assets will be transferred. If nil, assets are given to the bank instead.



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_squareSquare

Returns The square this player is currently on.

Returns:

  • (Square)

    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_goInteger

Returns the number of squares between this player’s current position on the board, and the GO square.

Returns:

  • (Integer)

    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.

Parameters:

  • bool (Boolean)

    True for in jail, False for out of 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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Parameters:

  • amount (Integer)

    amount of currency to be raised.

Returns:

  • (Boolean)

    whether or not the player was able to raise the amount required.



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.

Parameters:

  • n (Integer) (defaults to: 1)

    Number of squares to move.

  • direction (Symbol) (defaults to: :forwards)

    :forwards or :backwards.

Returns:

  • (Square)

    the square the player has landed on.



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_hotelsInteger

Returns the number of hotels on properties owned by this player.

Returns:

  • (Integer)

    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_housesInteger

Returns the number of houses on properties owned by this player.

Returns:

  • (Integer)

    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

#opponentsArray<Player>

Returns an array of all other players in the game.

Returns:

  • (Array<Player>)

    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.

Parameters:

  • beneficiary (Symbol) (defaults to: :bank)

    target Player instance or :bank.

  • amount (Integer) (defaults to: 0)

    amount of currency to transfer.

  • description (String) (defaults to: nil)

    Reference for the transaction (for game log).

Returns:

  • (Boolean)

    whether or not the player was able to pay the amount requested. False indicates bancruptcy.



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
		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

#rollArray<Integer>

Roll the dice!

Returns:

  • (Array<Integer>)

    dice roll as an array of num_dice integers between 1 and die_size.



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_ownedInteger

Returns the number of property sets owned by this player.

Returns:

  • (Integer)

    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.

Returns:

  • (Boolean)

    whether the player was both in jail and had an unused jail card available.



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