Last active
December 21, 2015 07:58
-
-
Save kmazanec/6274771 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PokerHand | |
attr_reader :hand, :score | |
def initialize (hand) | |
@hand = hand | |
@straight = @flush = @full_house = @quad = @triple = @two_pair = @pair = false | |
@score = 0 | |
end | |
def evaluate | |
hands = @hand.scan(/[cdsh]/) # Creates array for suits | |
handv = @hand.scan(/[\dAKQJT]/).sort # Creates array for values | |
if hands.uniq.length == 1 # Flush check | |
@flush = true | |
end | |
if !handv.join.scan(/(.)\1{3}/).empty? # 4-of-a-kind check | |
@quad = true | |
elsif !handv.join.scan(/(.)\1{2}/).empty? # Three-of-a-kind check | |
@triple = true | |
elsif !handv.join.scan(/(.)\1/).empty? # Pair | |
@pair = true | |
end | |
# Need to test this | |
straight_check = handv.map do |x| | |
case x | |
when "A" then 14 | |
when "K" then 13 | |
when "Q" then 12 | |
when "J" then 11 | |
when "T" then 10 | |
else x.to_i # Not even sure if you can do this | |
end | |
end | |
if straight_check.sort!.each_cons(2).all? { |x,y| y.to_i == x.to_i + 1 } | |
@straight = true | |
end | |
if !@quad and handv.uniq.length == 2 | |
@full_house = true | |
end | |
if !@triple and handv.uniq.length == 3 | |
@two_pair = true | |
end | |
# ------------ | |
@score = score_hand | |
end | |
def score_hand | |
if @straight && @flush | |
9 | |
elsif @quad | |
8 | |
elsif @full_house | |
7 | |
elsif @flush | |
6 | |
elsif @straight | |
5 | |
elsif @triple | |
4 | |
elsif @two_pair | |
3 | |
elsif @pair | |
2 | |
else | |
1 | |
end | |
end | |
end | |
jake = PokerHand.new | |
puts jake.evaluate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment