Permalink
Browse files

Refactor comparison method

  • Loading branch information...
camillebaldock committed Sep 8, 2014
1 parent 4bc43f9 commit d3c6f7d429a1f5af92be92b1ff56589ffd3a9f97
Showing with 22 additions and 39 deletions.
  1. +22 −39 lib/hand.rb
View
@@ -11,55 +11,20 @@ def initialize(card_input, hand_parser)
end
end
#TODO: this only implements comparison for cards of different types
#future things to handle include differentiating hands with the same type
def <=>(other_hand)
rank_type = POKER_RANKS.index(rank.fetch(:type))
other_hand_rank_type = POKER_RANKS.index(other_hand.rank.fetch(:type))
if rank_type == other_hand_rank_type
rank.has_key?(:value) && compare_value(rank, other_hand.rank) ||
rank.has_key?(:full_of) && compare_filler(rank, other_hand.rank) ||
rank.has_key?(:pairs) && compare_pairs(rank, other_hand.rank) ||
rank.has_key?(:cards) && compare_cards(rank, other_hand.rank) ||
compare_value(:value, rank, other_hand.rank) ||
compare_value(:full_of, rank, other_hand.rank) ||
compare_card_arrays(:pairs, rank, other_hand.rank) ||
compare_card_arrays(:cards, rank, other_hand.rank) ||
0
else
rank_type <=> other_hand_rank_type
end
end
#TODO: a lot of duplication here!!!
def compare_cards(rank, other_rank)
cards = rank.fetch(:cards)
other_cards = other_rank.fetch(:cards)
if cards != other_cards
(cards - other_cards).max <=> (other_cards - cards).max
end
end
def compare_pairs(rank, other_rank)
cards = rank.fetch(:pairs)
other_cards = other_rank.fetch(:pairs)
if cards != other_cards
(cards - other_cards).max <=> (other_cards - cards).max
end
end
def compare_value(rank, other_rank)
if rank.fetch(:value) != other_rank.fetch(:value)
rank.fetch(:value) <=> other_rank.fetch(:value)
end
end
def compare_filler(rank, other_rank)
rank.fetch(:full_of) <=> other_rank.fetch(:full_of)
end
def compare_highest(rank, other_rank)
if rank.fetch(:highest) != other_rank.fetch(:highest)
rank.fetch(:highest) <=> other_rank.fetch(:highest)
end
end
POKER_RANKS = [
:highest,
:pair,
@@ -94,6 +59,24 @@ def suits_per_occurence
results_per_occurence_number(cards.map(&:suit))
end
def compare_card_arrays(key, rank, other_rank)
if rank.has_key?(key)
cards = rank.fetch(key)
other_cards = other_rank.fetch(key)
if cards != other_cards
(cards - other_cards).max <=> (other_cards - cards).max
end
end
end
def compare_value(key, rank, other_rank)
if rank.has_key?(key)
if rank.fetch(key) != other_rank.fetch(key)
rank.fetch(key) <=> other_rank.fetch(key)
end
end
end
def highest
{
:type => :highest,

0 comments on commit d3c6f7d

Please sign in to comment.