Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dice Question (Full House and Straight recognition)

Tags:

c#

regex

poker

dice

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.

like image 748
Lloyd Powell Avatar asked Jan 20 '23 09:01

Lloyd Powell


2 Answers

I would order all the numbers decreasing and then do some linear criteria matching on each value as you go along it whether it be in an int[] or a string.

like image 193
Craig White Avatar answered Jan 30 '23 15:01

Craig White


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
like image 26
user187291 Avatar answered Jan 30 '23 17:01

user187291