Class: Cribbage::Hand

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards_array) ⇒ Hand

Input is an array of strings from the command line I.E. 4H JD QD KH 3H



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cribbage.rb', line 66

def initialize(cards_array)
  @cards = [ ]
  @points = { }
  cards_array.each { |c| @cards << Card.new(c) }

  find_fifteens
  find_runs
  find_pairs
  find_flush
  find_nobs
end

Instance Attribute Details

#cardsObject (readonly)

key: fifteens cards: [ [1,2], [3,4] ] MUST BE Nested array points: 4



62
63
64
# File 'lib/cribbage.rb', line 62

def cards
  @cards
end

#pointsObject (readonly)

Returns the value of attribute points.



57
58
59
# File 'lib/cribbage.rb', line 57

def points
  @points
end

Instance Method Details

#find_fifteensObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cribbage.rb', line 131

def find_fifteens
  found = nil
  [2,3,4,5].each do |combo_size|
    @cards.combination(combo_size).each do |card_combo|

      # Check for 15s
      card_count = 0
      card_combo.each { |card| card_count += card.value }
      if card_count == 15
        found ||= [ ]  
        found << card_combo
      end
    end
  end
  if found!=nil
    @points[:fifteens] = {cards: found, point_value: found.size*2}
  end
end

#find_flushObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cribbage.rb', line 193

def find_flush
  hand = @cards
  if @cards.map(&:suit).uniq.size == 1
    @points[:flush] = {cards: [@cards], point_value: 5}
    return true
  end
  if players_cards.map(&:suit).uniq.size == 1
    @points[:flush] = {cards: [ players_cards ], point_value: 4}
    return true
  end
  return nil
end

#find_heelsObject



206
207
208
209
210
211
# File 'lib/cribbage.rb', line 206

def find_heels
  # Awarded when the starter card is a Jack
  if starter_card.face_value=='J'
    @points[:heels] = {cards: [[starter_card]], point_value: 2}
  end
end

#find_nobsObject



213
214
215
216
217
218
219
220
# File 'lib/cribbage.rb', line 213

def find_nobs
  # One for nobs
  jacks = players_cards.keep_if {|c| c.face_value=='J'}
  if jacks.map(&:suit).include?(starter_card.suit)
    @points[:nobs] = {cards: [ jacks.keep_if { |j| starter_card.suit==j.suit } ], point_value: 1}
  end
  return nil
end

#find_pairsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cribbage.rb', line 179

def find_pairs
  found = [ ]
  @cards.combination(2).each do |pair|
    if pair.first.face_value == pair.last.face_value
      found << pair
    end
  end
  if found.size==0
    return nil
  else
    @points[:pairs] = {cards: found, point_value: found.size*2}
  end
end

#find_runsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cribbage.rb', line 150

def find_runs
  found = { }
  [5,4,3].each do |combo_size|
    @cards.combination(combo_size).each do |combo|
      combo = combo.sort { |x,y| x.index <=> y.index } # sort by index
      next unless combo.map(&:face_value).uniq.size == combo.size # all cards must be unique
      if (combo.last.index - combo.first.index) == (combo.size-1)
        found[combo_size] ||= [ ]
        found[combo_size] << combo
      end
    end
  end
  if found.has_key?(5)
    @points[:runs] = {cards: found[5], point_value: 5}
    return true
  end
  if found.has_key?(4)
    cards = found[4]
    @points[:runs] = {cards: cards, point_value: cards.first.size*cards.size}
    return true
  end
  if found.has_key?(3)
    cards = found[3]
    @points[:runs] = {cards: cards, point_value: cards.first.size*cards.size}
    return found[3]
  end
  return nil
end

#players_cardsObject



82
83
84
# File 'lib/cribbage.rb', line 82

def players_cards
  @cards[1...@cards.size]
end


126
127
128
129
# File 'lib/cribbage.rb', line 126

def print_cards(label, cards)
  str = cards.map { |c| c.card_str }.join " "
  ap "#{label}: #{str}"
end


116
117
118
119
120
121
122
123
124
# File 'lib/cribbage.rb', line 116

def print_points(key)
  if @points[key].nil?
    return false
  end
  cards = @points[key.to_sym][:cards] # should be array of array
  str = cards.map { |c| "" + c.join('') + "" }.join ", "
  title = key.to_s
  $stdout.printf "%-3s %-10s %-10s %-10s\n", '', title, "+#{@points[key][:point_value]}", str
end


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cribbage.rb', line 94

def print_score
  #  Your Hand
  #  5H | 6D 3D JH
  # 
  #  Total Points: +17
  #  Fifteens (+8): (5 5 5), (5 J), (5 J), (5 K)
  #  Runs:
  #  
  $stdout.printf "\n\n"
  $stdout.printf "%-3s %-21s %-10s\n\n", '', 'Hand:', starter_card.to_s + "" + players_cards.join('')

  print_points :fifteens
  print_points :runs
  print_points :pairs
  print_points :flush
  print_points :nobs

  $stdout.printf "\n%-3s %-10s %-10s\n", '', 'Total:', "+#{total_score}"

  $stdout.printf "\n\n"
end

#starter_cardObject



78
79
80
# File 'lib/cribbage.rb', line 78

def starter_card
  @cards.first
end

#total_scoreObject



86
87
88
89
90
91
92
# File 'lib/cribbage.rb', line 86

def total_score
  score = 0
  @points.each do |k,v|
    score += v[:point_value]
  end
  score
end