Class: Hand

Inherits:
Object
  • Object
show all
Defined in:
lib/mblackjack/hand.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHand

Returns a new instance of Hand.



4
5
6
7
8
9
10
# File 'lib/mblackjack/hand.rb', line 4

def initialize
  @hand_no = hand_no
  @cards_in_hand = []
  @bet = 0
  @choices = []
  @hand_value = 0
end

Instance Attribute Details

#betObject

Returns the value of attribute bet.



2
3
4
# File 'lib/mblackjack/hand.rb', line 2

def bet
  @bet
end

#cards_in_handObject

Returns the value of attribute cards_in_hand.



2
3
4
# File 'lib/mblackjack/hand.rb', line 2

def cards_in_hand
  @cards_in_hand
end

#choicesObject

Returns the value of attribute choices.



2
3
4
# File 'lib/mblackjack/hand.rb', line 2

def choices
  @choices
end

#hand_noObject

Returns the value of attribute hand_no.



2
3
4
# File 'lib/mblackjack/hand.rb', line 2

def hand_no
  @hand_no
end

#hand_valueObject

Returns the value of attribute hand_value.



2
3
4
# File 'lib/mblackjack/hand.rb', line 2

def hand_value
  @hand_value
end

Instance Method Details

#calc_hand_valueObject

Could be improved



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mblackjack/hand.rb', line 13

def calc_hand_value
  @hand_value = 0
  cards_in_hand.each_with_index do |c,ind|
    if c != "ace"
      value = Deck.card_values.fetch(c)
      @hand_value += value
    elsif c == "ace" && cards_in_hand[ind -1] == "ace"
      value = Deck.card_values.fetch(c)[0]
      @hand_value += value
    elsif c == "ace"
      value = Deck.card_values.fetch(c)[1]
      @hand_value += value
    end
  end
  if @hand_value > 21 and cards_in_hand.include?("ace")
    @hand_value -= 10
  end
end