Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created February 19, 2013 05:40
Show Gist options
  • Save mfpiccolo/4983384 to your computer and use it in GitHub Desktop.
Save mfpiccolo/4983384 to your computer and use it in GitHub Desktop.
Finds what hand you have in yachtzee.
class Yach_hand
attr_reader :array
def initialize(array)
@array = array
end
def read_hand
if yacht?
"Thats a yachtzee"
elsif four?
"Thats a four of a kind"
elsif full?
"Thats a full house"
elsif three?
"Thats a three of a kind"
end
end
private
def yacht?
@array.select{|element| @array.count(element) > 4 }.length > 4
end
def four?
@array.select{|element| @array.count(element) > 3 }.length > 3
end
def full?
@array.uniq.map! {|element| @array.count(element) }.sort!.reverse!-[1] == [3,2]
end
def three?
@array.select{|element| @array.count(element) > 2 }.length > 2
end
end
# ------------- unit tests --------------------
hand = Yach_hand.new([1,1,1,1,1])
puts "#{hand.read_hand} should be a 'Thats a Yachtzee!'"
hand = Yach_hand.new([1,1,3,1,1])
puts "#{hand.read_hand} should be a 'Thats a four of a kind!'"
hand = Yach_hand.new([1,4,1,4,1])
puts "#{hand.read_hand} should be a 'Thats a full house!'"
hand = Yach_hand.new([1,3,4,1,1])
puts "#{hand.read_hand} should be a 'Thats a three of a kind!'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment