Last active
July 1, 2018 21:02
-
-
Save CodePint/7e999820ecdb63f6b4b574be5d22e128 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, :check_low, :duplicates, :highest_hand, :flushes | |
# 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 | |
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 << {value: 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[:value] | |
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[:value].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| | |
binding.pry | |
if (card[:value] += 1) == (@cards[index+1][:value]) | |
straight << card | |
end | |
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") | |
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 | |
#hand_straight.find_straights | |
low_straight_hand.find_straights |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
poker_simple.rb:118:in
block in find_straights': undefined method
[]' for nil:NilClass (NoMethodError)