Skip to content

Instantly share code, notes, and snippets.

@CodePint
Created July 1, 2018 22:37
Show Gist options
  • Save CodePint/dfd1003316d8b0c1f16815c87249af20 to your computer and use it in GitHub Desktop.
Save CodePint/dfd1003316d8b0c1f16815c87249af20 to your computer and use it in GitHub Desktop.
## Simple poker hand comparison ##
require 'pry'
FACE = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K" ]
SUITS = ["S", "H", "D", "C"]
class Deck
attr_accessor :cards, :discards
def initialize
@cards = []
@discards = []
FACE.each do |face|
SUITS.each do |suit|
@cards << face+suit
end
end
self.shuffle
end
def shuffle
@cards = @cards.shuffle!
end
def count
@cards.count
end
def draw_hand
@cards.pop(5).join(" ")
end
end
class Hand
include Comparable
attr_accessor :cards, :possible_hands, :check_low, :duplicates, :flushes, :straight, :highest_card
# initial setup to make hands easier to work with
def setup
puts "setup running"
@cards = @cards.split(" ")
@check_low = self.check_low_straight
self.numerical_cards_map
self.transform_values
end
def initialize(cards)
@cards = cards
@possible_hands = []
self.setup
end
# implementing comparable operator for winning hand
def <=>(other)
self[:value] <=> other[:value]
end
# turning each card into a value/suit hash
def numerical_cards_map
card_map = []
@cards.map do |card|
card_map << {face: card[0], suit: card[1]}
end
@cards = card_map
end
# transforming the value into an integer
def transform_values
@cards.each do |card|
case card[:face]
when "T"
card[:value] = 10
when "J"
card[:value] = 11
when "Q"
card[:value] = 12
when "K"
card[:value] = 13
when "A"
if @check_low
card[:value] = 1
else
card[:value] = 14
end
else
card[:value] = card[:face].to_i
end
end
end
# checking if the hand contains a low straight (only instance where ace value should be low)
def check_low_straight
low_straight = ["A", "2", "3", "4", "5"]
number_only_cards = @cards.map {|c| c[0]}
if (number_only_cards & low_straight) == low_straight
return true
else
return false
end
end
# finding duplicate counts for cards in hand
def find_duplicates
@duplicates = @cards.each_with_object(Hash.new(0)) {|h1, h2| h2[h1[:value]] +=1}
end
# finding counts for flushes
def find_flushes
@flushes = @cards.each_with_object(Hash.new(0)) {|h1, h2| h2[h1[:suit]] +=1}
end
# finding out if the hand contains a straight and what its high is.
def find_straights
@straight = Array.new
@cards.each_with_index do |card, index|
if index == (@cards.length - 1)
straight << card
elsif (card[:value] + 1) == (@cards[index+1][:value])
@straight << card
end
end
end
def find_high_card
@highest_card = @cards.max_by {|card| card[:value]}
end
# check if the player has a flush
def is_flush?
if @flushes.length == 1
@possible_hands << {type: "flush", contents: @flushes, high_card: @highest_card}
return true
end
end
# check if the player has a straight
def is_straight?
if @straight.length == 5
@possible_hands << {type: "straight", contents: @straight, high_card: @highest_card}
return true
end
end
# check if the player has a straight flush
def is_straight_flush?
if is_straight? && is_flush?
@possible_hands << {type: "straight_flush", contents: @straight, high_card: @highest_card}
return true
end
end
# check if the player has a royal flush
def is_royal_flush?
if is_straight? && is_flush? && (@highest_card[:value] == 14)
@possible_hands << {type: "straight_flush", contents: @straight, high_card: @highest_card}
return true
end
end
def is_two_of_a_kind?
twos = @duplicates.select {|key, value| value == 2)
if twos.length == 1
@possible_hands << {type: "two_of_a_kind", contents: twos, high_card: @highest_card}
return true
end
end
def is_two_pair?
twos = @duplicates.select {|key, value| value == 2)
if twos.length == 2
@possible_hands << {type: "two_pair", contents: twos, high_card: @highest_card}
return true
end
end
def is_three_of_a_kind?
threes = @duplicates.select {|key, value| value == 3)
if !threes.empty?
@possible_hands << {type: "three_of_a_kind", contents: threes, high_card: @highest_card}
return true
end
end
def is_four_of_a_kind?
fours = @duplicates.select {|key, value| value == 2)
if !fours.empty?
@possible_hands << {type: "four_of_a_kind", contents: fours, high_card: @highest_card}
return true
end
end
end
deck = Deck.new
hand_1 = Hand.new(deck.draw_hand)
hand_2 = Hand.new(deck.draw_hand)
hand_straight = Hand.new("3C 4D 5S 6D 7H")
low_straight_hand = Hand.new("AC 2D 3S 4D 5H")
high_ace_hand = Hand.new("AS 5H KD JS 8H")
flush_hand = Hand.new("AS QS KS TS JS")
straight_flush = Hand.new("3S 4S 5S 6S 7S")
royal_flush = Hand.new("TH JH QH KH AH")
print hand_1.cards
puts "\n\n\n"
print hand_2.cards
puts "\n\n\n"
print low_straight_hand.cards
puts "\n\n\n"
print high_ace_hand.cards
hand_2.find_duplicates
hand_2.find_flushes
puts hand_2.flushes.length
#hand_straight.find_straights
low_straight_hand.find_straights
hand_straight.find_straights
hand_2.find_straights
print low_straight_hand.straight
puts "\n\n"
print hand_straight.straight
puts hand_straight.straight.length
puts "\n\n"
print hand_2.straight
puts hand_2.straight.length
print "------------------------------------------\n"
flush_hand.find_flushes
hand_1.find_flushes
puts "\n"
puts "\n"
puts flush_hand.is_flush?
puts hand_1.is_flush?
puts "\n\n"
print "-----------------------------------------\n"
puts low_straight_hand.is_straight?
binding.pry
puts hand_2.is_straight?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment