Created
July 1, 2018 23:35
-
-
Save CodePint/8530eda05597cfe5931292598ef793fa 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
## 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, :remaining_cards | |
# runs the card transformation, analyses the cards, checks for possible hands | |
def setup | |
puts "setup running" | |
@cards = @cards.split(" ") | |
@check_low = self.check_low_straight | |
self.numerical_cards_map | |
self.transform_values | |
self.analyze_cards | |
self.check_possible_hands | |
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 the remaining cards after duplicates/high card | |
def find_remaining_cards | |
@remaining_cards = @duplicates.select {|key, value| value == 1}.map{|k,v| k}.sort | |
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 | |
# finding the highest card for all hands not relying on duplicates | |
def find_highest_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 full house | |
def is_full_house? | |
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 | |
# check if the player has two of a kind | |
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_cards: @remaining_cards } | |
return true | |
end | |
end | |
# check if the player has two pairs | |
def is_two_pair? | |
twos = @duplicates.select {|key, value| value == 2} | |
if twos.length == 2 | |
@possible_hands << {type: "two_pair", contents: twos, high_cards: @remaining_cards} | |
return true | |
end | |
end | |
# check if the player has three of a kind | |
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_cards: @remaining_cards} | |
return true | |
end | |
end | |
# check if the player has four of a kind | |
def is_four_of_a_kind? | |
fours = @duplicates.select {|key, value| value == 4} | |
if !fours.empty? | |
@possible_hands << {type: "four_of_a_kind", contents: fours, high_cards: @remaining_cards} | |
return true | |
end | |
end | |
# setting the high card value and leaving the remaining cards as kickers | |
def high_card | |
@possible_hands << {type: "high_card", contents: @remaining_cards.last, high_cards: @remaining_cards[0..3] } | |
end | |
def analyze_cards | |
self.find_duplicates | |
self.find_remaining_cards | |
self.find_flushes | |
self.find_straights | |
self.find_highest_card | |
end | |
def check_possible_hands | |
self.is_flush? | |
self.is_straight? | |
self.is_straight_flush? | |
self.is_royal_flush? | |
self.is_two_of_a_kind? | |
self.is_two_pair? | |
self.is_three_of_a_kind? | |
self.is_four_of_a_kind? | |
self.high_card | |
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") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment