Skip to content

Instantly share code, notes, and snippets.

@pierrel
Created January 21, 2010 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrel/282511 to your computer and use it in GitHub Desktop.
Save pierrel/282511 to your computer and use it in GitHub Desktop.
require 'enumerator'
module Poker
def royal_flush?
high_in_flush = flush?
if high_in_flush
suite = high_in_flush.suite
if (@cards.sort.reverse.select { |card| card.suite == suite }.map { |card| card.number }.uniq)[0..5] == [14, 13, 12, 11, 10]
return Card.new("A" + suite)
else
return false
end
else
return false
end
end
def straight_flush?
straight? flush?
end
def straight?(flush_high = nil)
suite = flush_high.suite
cards = case suite
when nil then @cards
else @cards.select { |card| card.suite == suite }
end
# now choose the largest consecutive sequence of 5
sorted_numbers = cards.sort.reverse.select { |card| card.number }.uniq
differences = [] # holds differences between numbers in cards
sorted_numbers.each do |number|
end
end
# returns the high card if there are 5 cards of the same suite, false otherwise
def flush?
suites = @cards.map { |card| card.suite }.uniq
# if there are 5 cards of the same suite then return the high card
suites.each do |suite|
cards_of_suite = @cards.select { |card| card.suite == suite }
if cards_of_suite.length >= 5
return cards_of_suite.sort.last
end
end
# no 5 cards of same suite
return false
end
private
end
class Array
def in_straight_desc_order?
if self.length == 1 or self.empty?
return true
else
locally_desc = self.first == self[1] + 1
puts self[1..(self.length)]
return locally_desc and self[1..(self.length)]
end
end
end
class Card
attr_accessor :number, :suite
def initialize(card_string)
@the_string = card_string
card_arr = card_string.split('')
@number = case card_arr[0]
when "T" then 10
when "J" then 11
when "Q" then 12
when "K" then 13
when "A" then 14
else card_arr[0].to_i
end
@suite = card_arr[1]
end
def <=>(other_card)
self.number <=> other_card.number
end
def to_s
@the_string
end
end
class Hand
include Enumerable
include Poker
attr_reader :cards
def initialize(hand_string)
@cards = hand_string.split(' ').collect { |card_string| Card.new card_string }
end
# for enumerable mixin
def each
@cards.each do |card|
yield card
end
end
def to_s
@cards.map { |card| card.to_s }.join(' ')
end
end
# hands = "AC AD 7H 5D 5H TH JC\nKH QH 7H 5D 5H TH JC"
#
# c1 = Card.new "TC"
# c2 = Card.new "AD"
#
# h = Hand.new "AC AD 7H 5D 5H TH JC"
#
# flush = Hand.new "AC AH 7H 5D 5H TH JH"
# puts flush.flush?
#
# rflush = Hand.new "AH TH QH KH JH 5C 7S"
# puts rflush.royal_flush?
puts [2, 1].in_straight_desc_order?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment