Created
July 7, 2014 04:59
-
-
Save domgetter/b344fe502e76332e3a3e 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 PokerHandRank | |
RANKS = %w(high pair tpair trip stright flush full four royal) | |
def initialize(hand) | |
@hand = hand | |
end | |
def flush? | |
@hand.suits.any? {|k, v| v >= 5 } | |
end | |
def level | |
end | |
end | |
class Hand | |
include Comparable | |
def initialize | |
@cards = [] | |
end | |
def to_s | |
@cards.sort.join(", ") | |
end | |
def deal(cards = []) | |
@cards += cards | |
end | |
def rank | |
@rank ||= PokerHandRank.new(self) | |
end | |
def <=>(other_hand) | |
@rank.level <=> other_hand.rank.level | |
end | |
def suits | |
suit_array = [] | |
@cards.each do |card| | |
suit_array << card.suit | |
end | |
outhash = Hash.new(0) | |
suit_array.map {|suit| outhash[suit] += 1 } | |
outhash | |
end | |
end | |
class Card | |
include Comparable | |
attr_reader :rank, :suit, :rank_level | |
def initialize(rank, suit, rank_level) | |
@rank = rank | |
@suit = suit | |
@rank_level = rank_level | |
end | |
def to_s | |
"#@rank#@suit" | |
end | |
def <=>(other_card) | |
self.rank_level <=> other_card.rank_level | |
end | |
end | |
class Deck | |
RANKS = %w(2 3 4 5 6 7 8 9 T J Q K A) | |
SUITS = %w(s h c d) | |
def initialize | |
@cards = [] | |
RANKS.product(SUITS).map { |rank, suit| @cards << Card.new(rank, suit, RANKS.index(rank)+2) } | |
shuffle | |
end | |
def shuffle | |
@cards.shuffle! | |
end | |
def deal(n = 1) | |
output = [] | |
n.times { output << @cards.pop } | |
output | |
end | |
end | |
hand = Hand.new | |
deck = Deck.new | |
deck.shuffle #this is redundant but you can shuffle again if you want | |
hand.deal(deck.deal 7) # deck.deal returns an array that gets added to the hand's @cards array | |
puts hand.rank.flush? | |
puts hand |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment