Class: Gambler::Player

Inherits:
Object
  • Object
show all
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 to nil.

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

Instance Method Summary collapse

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, options = {})
  raise Exceptions::NoPlayerName unless name
  @name  = name
  @hand  = options[:hand] || Array.new

  @chips = options[:chips] || CHIP_STACK
  raise Exceptions::InvalidChipCount unless @chips.is_a? Fixnum
end

Instance Attribute Details

#chipsObject

Returns the value of attribute chips.



19
20
21
# File 'lib/gambler/player.rb', line 19

def chips
  @chips
end

#handObject

Returns the value of attribute hand.



19
20
21
# File 'lib/gambler/player.rb', line 19

def hand
  @hand
end

#nameObject

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

#inspectObject

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.

Returns:

  • (Boolean)


41
42
43
# File 'lib/gambler/player.rb', line 41

def is_broke?
  @chips <= 0
end

#to_sObject

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(options = {})
  format    = options[:format]    || :array
  pretty    = options[:pretty]    || false
  seperator = options[: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