Class: Gambler::Player
- Inherits:
-
Object
- Object
- Gambler::Player
- Defined in:
- lib/gambler/player.rb
Overview
Main Player class in which all others should inherit (if neccessary). The Player’s name
is required.
options
can contain:
-
chips
: Amount of chips the new Player will have. Defaults to 100. -
hand
: An array of Cards the new Player will be holding. Defaults tonil
.
Example:
Player.new('Dale')
Player.new('Kenny', :chips => 1_000_000)
Player.new( 'Cheaty McGee', # has blackjack from the start!
:hand => [Card.new('Ad'), Card.new('Kd')] )
Constant Summary collapse
- CHIP_STACK =
Initial chip count if none is specified.
100
Instance Attribute Summary collapse
-
#chips ⇒ Object
Returns the value of attribute chips.
-
#hand ⇒ Object
Returns the value of attribute hand.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#empty_hand! ⇒ Object
Removes all the Cards from the Player’s hand.
-
#initialize(name, options = {}) ⇒ Player
constructor
A new instance of Player.
-
#inspect ⇒ Object
Pretty object inspection.
-
#is_broke? ⇒ Boolean
Return true/false depending on if the Player has chips.
-
#to_s ⇒ Object
Pretty printing of a Player.
-
#view_hand(options = {}) ⇒ Object
Allows a Player to view their
hand
in various formats.
Constructor Details
#initialize(name, options = {}) ⇒ Player
Returns a new instance of Player.
21 22 23 24 25 26 27 28 |
# File 'lib/gambler/player.rb', line 21 def initialize(name, = {}) raise Exceptions::NoPlayerName unless name @name = name @hand = [:hand] || Array.new @chips = [:chips] || CHIP_STACK raise Exceptions::InvalidChipCount unless @chips.is_a? Fixnum end |
Instance Attribute Details
#chips ⇒ Object
Returns the value of attribute chips.
19 20 21 |
# File 'lib/gambler/player.rb', line 19 def chips @chips end |
#hand ⇒ Object
Returns the value of attribute hand.
19 20 21 |
# File 'lib/gambler/player.rb', line 19 def hand @hand end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/gambler/player.rb', line 19 def name @name end |
Instance Method Details
#empty_hand! ⇒ Object
Removes all the Cards from the Player’s hand.
31 32 33 |
# File 'lib/gambler/player.rb', line 31 def empty_hand! @hand = Array.new end |
#inspect ⇒ Object
Pretty object inspection.
36 37 38 |
# File 'lib/gambler/player.rb', line 36 def inspect %Q{#<Gambler::Player '#{@name}'>} end |
#is_broke? ⇒ Boolean
Return true/false depending on if the Player has chips.
41 42 43 |
# File 'lib/gambler/player.rb', line 41 def is_broke? @chips <= 0 end |
#to_s ⇒ Object
Pretty printing of a Player.
46 47 48 |
# File 'lib/gambler/player.rb', line 46 def to_s "#{@name} ($#{@chips})" end |
#view_hand(options = {}) ⇒ Object
Allows a Player to view their hand
in various formats. Examples:
@player = Player.new('Dale', :hand => [Card.new('Ad'), Card.new('Kd')])
# :format => :array (the default)
@player.view_hand
=> ['Ad', 'Kd']
@player.view_hand(:pretty => true)
=> ['Ace of Diamonds', 'King of Diamonds']
# :format => :string
@player.view_hand(:format => :string)
=> 'Ad Kd'
@player.view_hand(:format => :string, :pretty => true)
=> 'Ace of Diamonds, King of Diamonds'
@player.view_hand(:format => :string, :pretty => true, :seperator => ' | ')
=> 'Ace of Diamonds | King of Diamonds'
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gambler/player.rb', line 67 def view_hand( = {}) format = [:format] || :array pretty = [:pretty] || false seperator = [:seperator] || (pretty ? ', ' : ' ') method = (pretty ? :to_pretty_s : :to_s) case format when :array cards = Array.new @hand.each { |card| cards << card.send(method) } when :string cards = @hand.collect { |card| card.send(method) }.join(seperator) end return cards end |