正则表达式来计算直接扑克牌?

发布于 2024-09-14 15:19:39 字数 827 浏览 0 评论 0原文

是否有正则表达式来计算直扑克牌?

我使用字符串来表示排序的牌,例如:

AAAAK#sssss = 4 aces and a king, all of spades.
A2345#ddddd = straight flush, all of diamonds.

在Java中,我使用这些正则表达式:

regexPair = Pattern.compile(".*(\\w)\\1.*#.*");
regexTwoPair = Pattern.compile(".*(\\w)\\1.*(\\w)\\2.*#.*");
regexThree = Pattern.compile(".*(\\w)\\1\\1.*#.*");
regexFour = Pattern.compile(".*(\\w)\\1{3}.*#.*");
regexFullHouse = Pattern.compile("((\\w)\\2\\2(\\w)\\3|(\\w)\\4(\\w)\\5\\5)#.*");
regexFlush = Pattern.compile(".*#(\\w)\\1{4}");

如何使用正则表达式计算直(序列)值?

编辑

我提出了另一个问题来解决同样的问题,但是使用 char 的 ascii 值, 正则表达式要短。详细信息此处

谢谢!

Is there a regex to calculate straight poker hand?

I'm using strings to represent the sorted cards, like:

AAAAK#sssss = 4 aces and a king, all of spades.
A2345#ddddd = straight flush, all of diamonds.

In Java, I'm using these regexes:

regexPair = Pattern.compile(".*(\\w)\\1.*#.*");
regexTwoPair = Pattern.compile(".*(\\w)\\1.*(\\w)\\2.*#.*");
regexThree = Pattern.compile(".*(\\w)\\1\\1.*#.*");
regexFour = Pattern.compile(".*(\\w)\\1{3}.*#.*");
regexFullHouse = Pattern.compile("((\\w)\\2\\2(\\w)\\3|(\\w)\\4(\\w)\\5\\5)#.*");
regexFlush = Pattern.compile(".*#(\\w)\\1{4}");

How to calculate straight (sequences) values with regex?

EDIT

I open another question to solve the same problem, but using ascii value of char,
to regex be short. Details here.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-09-21 15:19:39

我必须承认,正则表达式并不是我想到的第一个执行此操作的工具。我几乎可以保证,任何能够对未排序的手执行此操作的 RE 都将比等效的程序代码更加丑陋且可读性差得多。

假设卡片按面值排序(并且它们似乎否则您列出的正则表达式也不起作用),并且您必须使用正则表达式,您可以使用构造类似于

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

检测手的面值部分。

事实上,从我这里收集到的“标准”手上,应按优先级递减的顺序检查以下内容:

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)

基本上,这些与您的相同,除了我添加了皇家/同花顺和顺子。如果你按顺序检查它们,你应该从这手牌中得到最好的分数。高牌没有正则表达式,因为此时,它是您可以获得的唯一分数。

我还将钢轮(环绕式)直道从 A2345 更改为 2345A,因为它们将以这种方式排序。

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.

[浮城] 2024-09-21 15:19:39

我为此重写了正则表达式,因为我发现它令人沮丧和困惑。分组对于这种类型的逻辑更有意义。排序是使用 javascript 中的标准数组排序方法完成的,因此卡片的顺序很奇怪,它们按字母顺序排列。我是用 javascript 做的,但正则表达式可以应用于 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' },
  ];

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' },
  ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文