Regex to calculate straight poker hand?

不想你离开。 提交于 2019-11-30 14:40:01

I have to admit that regular expressions are not the first tool I would have thought of for doing this. I can pretty much guarantee that any RE capable of doing that to an unsorted hand is going to be far more hideous and far less readable than the equivalent procedural code.

Assuming the cards are sorted by face value (and they seem to be otherwise your listed regexes wouldn't work either), and you must use a regex, you could use a construct like

2345A|23456|34567|...|9TJQK|TJQKA

to detect the face value part of the hand.

In fact, from what I gather here of the "standard" hands, the following should be checked in order of decreasing priority:

Royal/straight flush: "(2345A|23456|34567|...|9TJQK|TJQKA)#(\\w)\\1{4}"
Four of a kind:       ".*(\\w)\\1{3}.*#.*"
Full house:           "((\\w)\\2\\2(\\w)\\3|(\\w)\\4(\\w)\\5\\5)#.*"
Flush:                ".*#(\\w)\\1{4}"
Straight:             "(2345A|23456|34567|...|9TJQK|TJQKA)#.*"
Three of a kind:      ".*(\\w)\\1\\1.*#.*"
Two pair:             ".*(\\w)\\1.*(\\w)\\2.*#.*"
One pair:             ".*(\\w)\\1.*#.*"
High card:            (none)

Basically, those are the same as yours except I've added the royal/straight flush and the straight. Provided you check them in order, you should get the best score from the hand. There's no regex for the high card since, at that point, it's the only score you can have.

I also changed the steel wheel (wrap-around) straights from A2345 to 2345A since they'll be sorted that way.

I rewrote the regex for this because I found it frustrating and confusing. Groupings make much more sense for this type of logic. The sorting is being done using a standard array sort method in javascript hence the strange order of the cards, they are in alphabetic order. I did mine in javascript but the regex could be applied to java.

hands = [
    { regex: /(2345A|23456|34567|45678|56789|6789T|789JT|89JQT|9JKQT|AJKQT)#(.)\2{4}.*/g , name: 'Straight flush' },
    { regex: /(.)\1{3}.*#.*/g , name: 'Four of a kind' },
    { regex: /((.)\2{2}(.)\3{1}#.*|(.)\4{1}(.)\5{2}#.*)/g , name: 'Full house' },
    { regex: /.*#(.)\1{4}.*/g , name: 'Flush' },
    { regex: /(2345A|23456|34567|45678|56789|6789T|789JT|89JQT|9JKQT|AJKQT)#.*/g , name: 'Straight' },
    { regex: /(.)\1{2}.*#.*/g , name: 'Three of a kind' },
    { regex: /(.)\1{1}.*(.)\2{1}.*#.*/g , name: 'Two pair' },
    { regex: /(.)\1{1}.*#.*/g , name: 'One pair' },
  ];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!