Hand Ranking Logic - bensbits91/csnw-bb-poker GitHub Wiki

How I Coded the Hand Ranking Logic

Thought Process

  1. Initial Brainstorming:

    • Started by thinking through the logic for each hand type:
      • Flush: Simple to detect; use the high card as a tiebreaker.
      • Straight: Detect 5 sequential card ranks; use the high card as a tiebreaker.
      • Straight Flush: Combination of a straight and a flush.
      • Royal Flush: A straight flush with the high card being an Ace (rank 14, since Aces are always high in this game).
      • X of a Kind: Requires an array of unique ranks with counts to detect pairs, three-of-a-kind, and four-of-a-kind.
        • Use the rank (key) where the count (value) matches x.
      • Full House: A combination of a pair and three-of-a-kind.
      • Four of a Kind: Straightforward and can never result in a tie.
      • Three of a Kind: Straightforward and can never result in a tie.
      • Two Pair: Detect two pairs; requires a tiebreaker.
      • One Pair: Requires a tiebreaker since two players can have the same ranked pair.
      • High Card: The fallback hand type.
  2. Breaking Down the Logic:

    • Needed a utility to create a count of each rank for detecting "X of a Kind."
    • Used this utility to simplify logic for detecting pairs, three-of-a-kind, and four-of-a-kind.

Research and Collaboration

  • Googled for Standard Algorithms:
    • Researched common approaches to poker hand ranking and absorbed key concepts.
  • Discussed with Copilot:
    • Reviewed each hand type with Copilot.
    • Found most suggestions overly complex or verbose.

Simplification and Implementation

  • Simplified the Logic:

    • Instead of creating separate functions for detecting hand types and additional functions for tiebreakers, I consolidated the logic.
    • Created a utility map function to count the occurrences of each rank.
    • Used this map to directly determine hand types and tiebreaker ranks.
  • Readable and Maintainable Code:

    • Focused on making the logic concise and easy to follow.
    • Avoided unnecessary complexity while ensuring accuracy for all hand types.

This approach allowed me to implement the hand ranking logic in a way that is both efficient and easy to maintain, while ensuring the game adheres to standard poker rules.