Class: BlackJack::Player
- Inherits:
-
Object
- Object
- BlackJack::Player
- Defined in:
- lib/black_jack/player.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_value ⇒ Object
Returns the value of attribute current_value.
-
#hand ⇒ Object
readonly
Returns the value of attribute hand.
-
#hand_value(current_value = 0) ⇒ Object
Program interates through the hand calling the card_value of each card.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#bust? ⇒ Boolean
When the hand has a value of over 21 it busts and declares true when it equals 21 it declares blackjack and returns true.
-
#card_value(card) ⇒ Object
I hate this method and feel like there is a much easier way of doing it.
-
#fold_hand ⇒ Object
gives us a method for folding a busted hand.
-
#hit(deck) ⇒ Object
this might be a bit redundant but I feel like it makes the code more intuitive.
-
#initialize(name) ⇒ Player
constructor
A new instance of Player.
- #take_card(deck) ⇒ Object
- #take_specific_card(deck = @deck, index_number = 0) ⇒ Object
-
#to_s ⇒ Object
I feel like I should have a to_s method, not sure I’m doing it right.
Constructor Details
#initialize(name) ⇒ Player
Returns a new instance of Player.
10 11 12 13 14 |
# File 'lib/black_jack/player.rb', line 10 def initialize (name) @name = name @hand = [] @current_value = 0 end |
Instance Attribute Details
#current_value ⇒ Object
Returns the value of attribute current_value.
8 9 10 |
# File 'lib/black_jack/player.rb', line 8 def current_value @current_value end |
#hand ⇒ Object (readonly)
Returns the value of attribute hand.
7 8 9 |
# File 'lib/black_jack/player.rb', line 7 def hand @hand end |
#hand_value(current_value = 0) ⇒ Object
Program interates through the hand calling the card_value of each card
36 37 38 |
# File 'lib/black_jack/player.rb', line 36 def hand_value @hand_value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/black_jack/player.rb', line 7 def name @name end |
Instance Method Details
#bust? ⇒ Boolean
When the hand has a value of over 21 it busts and declares true when it equals 21 it declares blackjack and returns true
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/black_jack/player.rb', line 45 def bust? if hand_value > 21 fold_hand puts "#{@name} is Bust!" true elsif hand_value == 21 puts "BlackJack!" true else puts "#{@name}'s hand is worth #{self.hand_value}" false end end |
#card_value(card) ⇒ Object
I hate this method and feel like there is a much easier way of doing it.
65 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 91 92 93 94 95 |
# File 'lib/black_jack/player.rb', line 65 def card_value card if card =~ /Queen/ 10 elsif card =~ /King/ 10 elsif card =~ /Jack/ 10 elsif card =~ /Ace/ 11 elsif card =~ /2/ 2 elsif card =~ /3/ 3 elsif card =~ /4/ 4 elsif card =~ /5/ 5 elsif card =~ /6/ 6 elsif card =~ /7/ 7 elsif card =~ /8/ 8 elsif card =~ /9/ 9 elsif card =~ /10/ 10 else 0 end end |
#fold_hand ⇒ Object
gives us a method for folding a busted hand
59 60 61 62 |
# File 'lib/black_jack/player.rb', line 59 def fold_hand @hand.each {|card| hand.delete(card)} @hand = [] end |
#hit(deck) ⇒ Object
this might be a bit redundant but I feel like it makes the code more intuitive
31 32 33 |
# File 'lib/black_jack/player.rb', line 31 def hit deck self.take_card(deck) end |
#take_card(deck) ⇒ Object
20 21 22 23 |
# File 'lib/black_jack/player.rb', line 20 def take_card deck self.hand << deck.deal_a_card self.hand end |
#take_specific_card(deck = @deck, index_number = 0) ⇒ Object
25 26 27 28 |
# File 'lib/black_jack/player.rb', line 25 def take_specific_card(deck=@deck, index_number=0) self.hand << deck.deal_specific_card(deck, index_number) self.hand end |
#to_s ⇒ Object
I feel like I should have a to_s method, not sure I’m doing it right
16 17 18 |
# File 'lib/black_jack/player.rb', line 16 def to_s "#{@name}'s hand is worth #{self.hand_value}" end |