Skip to content

Getting rid of delta transform and suggestion #3

Description

@shredding

I'm using your class as base for one of my projects (thanks!) and since i did not get my head around your delta_transform method I ended up like this, maybe that's easier?

def straight_flush?
    flush? and straight?
end

def straight?
        by_face

        # I named my version cards, it's the array of cards
        temp_cards = cards.dup

        if temp_cards[0].face == Card::FACE_VALUES['A'] and temp_cards[1].face != Card::FACE_VALUES['K']
          # it's not an ace high straight, so we move the ace to the end
          # to test for a low straight
          ace = temp_cards.shift
          temp_cards << Card.new("L#{Card::SUITS[ace.suit]}")
        end

        face_value = temp_cards[0].face

        temp_cards.each do |c|
            return false if face_value != c.face
          face_value -= 1
        end
        true
end

I as well added a by_groups func:

def by_groups
        # technically, we treat single cards as single group in this loop
        pairs = []

        until @cards.size == 0
          reference = @cards[0]
          pair, @cards = @cards.partition { |c| c.face == reference.face}
          pairs.push(pair)
        end
        pairs.sort_by! { |a| [a.size, a[0].face] }.reverse!
        pairs.each { |pair| pair.each { |c| @cards << c }}
        self
      end

It's needed for what I do, but maybe of interest here as well? I could form a pull request if you're interested at all :)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions