Search code examples
c#regexpokerdice

Dice Question (Full House and Straight recognition)


I'm making a dice game. There are 5 dice in which I have all the values for and I need to determine if they make up a full house (3 of one and 2 of another), small straight (1-4, 2-6 or 3-6) or a large straight (1-5, 2-6).

Perhaps the best way to approach this seems to be to use regular expressions.

Does anyone know how I would go about representing these rules in regex?

Or if you can provide a better solution, I'd appreciate that.

Examples:

  • Full house = 44422 or 11166 or 12212 etc.
  • Small Straight = 12342 or 54532 etc.
  • Large Straight = 12345 or 52643 etc.

Edit
Changed wording to highlight that this is my inexperienced opinion.

I know how to achieve this using code, but it seems like such a long winded solution, I'm wondering if there's a more simplistic approach.


Solution

  • Don't know about c#, but in a scripting language I'd take the regexp route. For each side, calculate how many times it occurs in the combination and join the results together. For example, for the combination 12342 the counter string will be 121100. Then match the counter string against these patterns:

    /5/         = Five of a kind
    /4/         = Four of a kind
    /20*3|30*2/ = Full house
    /1{5}/      = Large Straight
    /[12]{4}/   = Small Straight
    /3/         = Three of a kind
    /2[013]*2/  = Two pair
    /2/         = One pair