class Cribbage::Hand

Attributes

cards[R]

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

points[R]

Public Class Methods

new(cards_array) click to toggle source

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

# 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

Public Instance Methods

find_fifteens() click to toggle source
# 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_flush() click to toggle source
# 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_heels() click to toggle source
# 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_nobs() click to toggle source
# 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_pairs() click to toggle source
# 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_runs() click to toggle source
# 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_cards() click to toggle source
# File lib/cribbage.rb, line 82
def players_cards
  @cards[1...@cards.size]
end
print_cards(label, cards) click to toggle source
print_points(key) click to toggle source
print_score() click to toggle source
starter_card() click to toggle source
# File lib/cribbage.rb, line 78
def starter_card
  @cards.first
end
total_score() click to toggle source
# File lib/cribbage.rb, line 86
def total_score
  score = 0
  @points.each do |k,v|
    score += v[:point_value]
  end
  score
end