lib/hands/hand_detection.rb in hands-0.2.1 vs lib/hands/hand_detection.rb in hands-0.3.0

- old
+ new

@@ -33,64 +33,46 @@ self.kinds(3) end # @return [Array, Nil] Array of {Card} objects with the straight in decending order or `nil` if there isn't a straight in the {Hand} def straight - return nil unless self.cards.length == 5 + # Require at least 5 cards + return nil unless self.cards.length >= 5 + + # Sort cs = self.cards.sort.reverse # Ace's low - if cs.first.value == 'a' and cs[1].value == '5' - # Move ace to end - ace = cs.first - cs = cs[1..4] - cs << ace + return cs if cs.first.value == 'a' and cs[1].value == '5' - # Check succession - csr = cs.reverse - 4.times do |i| - next if i == 0 - return nil unless csr[i].value_index == i - 1 - end + # Check succession + return nil unless succession? - # Normal - else - # Check range - return nil unless cs.first.value_index - cs.last.value_index == 4 + # Avoid wrap-around + values = self.cards.collect(&:value) + return nil if values.include?('k') and values.include?(2) - # Check succession - 4.times do |i| - return nil unless cs[i].value_index == cs[i + 1].value_index + 1 - end - end cs end # @return [Array, Nil] Array of {Card} objects with the flush in decending order or `nil` if there isn't a flush in the {Hand} def flush # If all of the {Card}s are the same suit, we have a flush - return nil unless self.suits.length == 1 + return nil unless self.cards.length >= 5 and self.suits.length == 1 self.cards.sort.reverse end # @return [Array, Nil] Array of {Card} objects with the full house in decending order or `nil` if there isn't a full house in the {Hand} def full_house dupes = self.duplicates - return nil unless dupes.length == 2 - - a = [] - b = [] - - hand = self.cards.select do |card| - if dupes.first == card.value - a << card - elsif dupes.last == card.value - b << card - end + return nil unless self.cards.length >= 5 and dupes.length == 2 + + # Ensure all {Card}s are one of the duplicates + self.cards.each do |card| + return nil unless dupes.include? card.value end - return nil unless a.length + b.length == 5 self.cards.sort.reverse end # @return [Array, Nil] Array of {Card} objects with the four of a kind first the kicker in decending order or `nil` if there isn't four of a kind in the {Hand} def four_of_a_kind @@ -134,11 +116,11 @@ pairs.uniq.select{ |e| (pairs - [e]).size < pairs.size - 1 } end def pairs(min) dupes = self.duplicates - return nil if dupes.length < min + return nil unless dupes.length >= min hand = self.cards.select do |card| dupes.include?(card.value) end @@ -158,8 +140,16 @@ return nil unless hand.length == num hand = hand.sort.reverse hand << (self.cards - hand).sort.reverse hand.flatten + end + + def succession? + cs = self.cards.sort.reverse + 4.times do |i| + return false unless cs[i].value_index == cs[i + 1].value_index + 1 + end + true end end end