需要帮助确定扑克手牌(一对)

发布于 2024-07-14 07:29:41 字数 402 浏览 7 评论 0原文

这只是代码的一部分,因此如果您看不到数组或其他任何内容,请不要介意。 我的想法是,我有 5 张牌,我想确定哪些是成对的。 有人明白我的意思吗?

boolean IsOnePair=true;

int [] cont = new int [6];

for (int i=0;i<Game.length;i++)
{
    cont[Game[i].getValue()] ++;
}

for (int i=0;i<cont.length;i++)
{
    if (cont[Game[i].getValue()]==2)
    {
        IsOnePair=false;
        System.out.println(Game+" : "+cont[i]+" times");
    }
}

This is just part of the code, so never mind if you can't see the arrays or anything else. The idea is that I have 5 cards and I want to determine which ones are pairs. Does anyone understand what I mean?

boolean IsOnePair=true;

int [] cont = new int [6];

for (int i=0;i<Game.length;i++)
{
    cont[Game[i].getValue()] ++;
}

for (int i=0;i<cont.length;i++)
{
    if (cont[Game[i].getValue()]==2)
    {
        IsOnePair=false;
        System.out.println(Game+" : "+cont[i]+" times");
    }
}

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

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

发布评论

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

评论(8

倾听心声的旋律 2024-07-21 07:29:41

首先,如果您希望它成为真正的扑克牌,那么您的数组可能应该只有 5 个元素,而不是 6 个。

至于确定是否存在一对,我只需将每张卡与其右侧的所有其他卡进行检查即可。 这将在 O(n^2) 中运行,但只要手的大小保持在 5 左右,这是可以接受的。

这里有一些代码可以做到这一点:

for(i=0; i<5; i++)
{
  for(j=i+1; j<5; j++)
  {
    if(hand[i] == hand[j])
      return true;
  }
 }

此外,您的代码不起作用的原因是因为您试图访问等于卡值的索引,而不是卡的编号。 您也许可以使用字典来执行此操作,但上面的代码编程起来要简单得多,并且对于如此小的问题规模,这是可以接受的。

Well for one, your array should probably only have 5 elements, not 6 if you want it to be a true poker hand.

As for determining if there is a pair or not, I would simply check each card against every other card to its right. This will run in O(n^2), but this is acceptable as long as the hand size stays at around 5.

Here is some code to do that:

for(i=0; i<5; i++)
{
  for(j=i+1; j<5; j++)
  {
    if(hand[i] == hand[j])
      return true;
  }
 }

Also, the reason your code is not working is because you're trying to access an index equal to the cards value, not what number the card is. You might be able to use a dictionary to do this, but the code above is a lot simpler to program and for such a small problem size, it is acceptable.

记忆之渊 2024-07-21 07:29:41

如果您需要加快速度,您可能需要考虑重新评估您的方法。 或者,考虑使用现有的扑克手牌检测库,或者至少研究其中至少一个的来源以获得一些“灵感”。 Cactus Kev 对他相当好的5 张牌手牌检测算法有一篇很好的文章:

你可以还想阅读 http:// www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-8

If you need this to be fast, you might want to consider re-evaluating your approach. Or, consider using an existing poker hand detection library, or at least studying the source to at least one of them for some 'inspiration'. Cactus Kev has a good writeup of his rather good 5 card hand detection algorithm:

You may also want to read http://www.codingthewheel.com/archives/how-i-built-a-working-online-poker-bot-8

手长情犹 2024-07-21 07:29:41

你如何处理甲板上的套装? 如果您将卡片表示为简单的整数,那么我假设卡片的有效值为 0 - 51。如果是这种情况,那么我猜卡片 0 - 12 都是一套,13 - 25 是另一套,依此类推花色的分配可以是任意的,直到您需要将其考虑在内的牌局为止。

通过这种安排,您可以检测到一对,就像 samoz 写道,对您的比较操作进行了修改。 您需要确保卡片对模 13 一致。只需将行更改

if(hand[i] == hand[j])

if( (hand[i] % 13) == (hand[j] % 13) )

模运算符 (%) 返回除法后的余数,依此

 0 % 13 = 0
 1 % 13 = 1
 2 % 13 = 2
    ...
12 % 13 = 12
13 % 13 = 0
14 % 14 = 1

类推...它允许您判断序列何时环绕某个特定值value,模数,在本例中为 13,因为四种花色中的每一种都有 13 张不同的牌。

举例来说,在您的牌组中,编号为 0 - 51 的 52 张牌中,牌 0 - 12 代表 A 到梅花 K,牌 13 - 25 代表红桃,26 - 38 代表黑桃,39 - 51 代表红桃。钻石。

现在你拿到手牌: 0, 12, 32, 21, 47

通过对每张牌取余模 13,你剩下 0, 12, 6, 8, 8

你可以看到最后两张牌是对,红心 9 和方块 9(记住编号从 0 开始,所以相差 1)。

How are you handling the suits of the deck? If you're representing the cards as simple ints, then I assume that the valid values of the cards are 0 - 51. If that's the case, then I guess cards 0 - 12 are all one suit, 13 - 25 are another, etc. Assignment of the suits can be arbitrary until you need to score hands that take it into account.

With this arrangement you can detect a pair just as samoz wrote, with a modification to your comparison operation. You'll need to make sure the cards are congruent modulo 13. Just change the line

if(hand[i] == hand[j])

to

if( (hand[i] % 13) == (hand[j] % 13) )

The modulus operator (%) returns the remainder after division, so

 0 % 13 = 0
 1 % 13 = 1
 2 % 13 = 2
    ...
12 % 13 = 12
13 % 13 = 0
14 % 14 = 1

and so on... It allows you to tell when a sequence wraps around a certain value, the modulus, in this case 13, since there are 13 different cards in each of four suits.

Let's say, for example, that in your deck of 52 cards numbered 0 - 51 that the cards 0 - 12 represent the Ace through King of clubs, cards 13 - 25 represent the hearts, 26 - 38 represent spades, and 39 - 51 represent diamonds.

Now you're dealt the hand: 0, 12, 32, 21, 47

By taking the remainder modulus 13 of each card you're left with 0, 12, 6, 8, 8

You can see that the last two cards are a pair, the 9 of hearts and the 9 of diamonds (remember the numbering starts at 0, so it's off by one).

想念有你 2024-07-21 07:29:41

我假设您正在寻找一对,而不是三个或四个同类。 在这种情况下,最好的办法就是检查每张牌,并存储有多少张 A、多少张 2、多少张 3 等。 此解决方案将为您提供对数,以及是否有三/四对或葫芦。 当然,在寻找同花或顺子时,您必须进行不同的检查。

Card[] hand = new Card[numberOfCards];
int[] frequencies = new int[13]; // there are 13 card values
...
for (int i = 0; i < hand.Count; i++)
{
    frequencies[hand[i].CardNumber] += 1; // assume Ace = 0, King = 12
}
// Now look through the frequencies:
int numberOfPairs = 0;
bool hasTriple = false;
bool hasFour = false;
for (int f = 0; j < frequencies.Count; j++)
{
    switch (frequencies[f])
    {
         case 2:
             numberOfPairs++;
             break;
         case 3:
             hasTriple = true;
             break;
         case 4:
             hasFour = true;
             break;
         default:
             break;
    }
}
// Now you know how many pairs you have, and whether you have a triple or four-of-a-kind
if (numberOfPairs == 1 && hasTriple)
{
    // It's a full house
}

编辑:
修改它来记录构成对的数字(一对 A 或一对 Q 等)也很简单。

I assume that you're looking for a pair as distinct from a three or four-of-a-kind. In that case, your best bet for that is to go through each of the cards, and store how many Aces, how many 2s, how many 3s, etc. there are. This solution will give you the number of pairs, as well as whether there's a three/four-of-a-kind or a full house. You would of course have to do different checking when looking for a flush or a straight.

Card[] hand = new Card[numberOfCards];
int[] frequencies = new int[13]; // there are 13 card values
...
for (int i = 0; i < hand.Count; i++)
{
    frequencies[hand[i].CardNumber] += 1; // assume Ace = 0, King = 12
}
// Now look through the frequencies:
int numberOfPairs = 0;
bool hasTriple = false;
bool hasFour = false;
for (int f = 0; j < frequencies.Count; j++)
{
    switch (frequencies[f])
    {
         case 2:
             numberOfPairs++;
             break;
         case 3:
             hasTriple = true;
             break;
         case 4:
             hasFour = true;
             break;
         default:
             break;
    }
}
// Now you know how many pairs you have, and whether you have a triple or four-of-a-kind
if (numberOfPairs == 1 && hasTriple)
{
    // It's a full house
}

EDIT:
It would also be trivial to modify this to keep a record of what numbers constituted the pairs (pair of Aces or of Queens etc.)

巴黎夜雨 2024-07-21 07:29:41

如果你对这手牌进行排序,然后从左到右读取它,那么计算这手牌的强度应该相对容易。

如果您追求简单,可以尝试这样的操作(伪代码):

def handStrength(hand):
    sort(hand) //sorts hand in ascending order
    if hasPair(hand):
        if isTwoPair(hand):
            return "two pair"
        else if isTrips(hand):
            return "three of a kind"
        else if isBoat(hand):
            return "full house"
        else if isQuads(hand):
            return "four of a kind"
        else:
            return "pair"
    else:
        straightFlag = isStraight(hand)
        flushFlag = isFlush(hand)
        if straightFlag:
            if flushFlag:
                return "straight flush"
            else:
                return "straight"
        else if flushFlag:
            return "flush"
        else:
            return "high card"

def hasPair(hand): //requires sorted hand
    for i = 1 to 4:
        if hand[i].rank == hand[i-1].rank:
            return True
    return False

def isTwoPair(hand): //requires sorted hand
    return (hand[0].rank == hand[1].rank and hand[2].rank == hand[3].rank and hand[0].rank != hand[2].rank) or
           (hand[1].rank == hand[2].rank and hand[3].rank == hand[4].rank and hand[1].rank != hand[3].rank)

def isTrips(hand): //requires sorted hand
    return (hand[0].rank == hand[2].rank and hand[0].rank != hand[3].rank and hand[3].rank != hand[4].rank) or
           (hand[1].rank == hand[3].rank and hand[0].rank != hand[1].rank and hand[0].rank != hand[4]) or
           (hand[2].rank == hand[4].rank and hand[1].rank != hand[2].rank and hand[1].rank != hand[0].rank)

def isBoat(hand): //requires sorted hand
    return (hand[0].rank == hand[1].rank and hand[2].rank == hand[3].rank == hand[4].rank) or
           (hand[0].rank == hand[1].rank == hand[2].rank and hand[3].rank == hand[4].rank)

def isQuads(hand): //requires sorted hand
    return hand[1].rank == hand[2].rank == hand[3].rank and (hand[0].rank == hand[1].rank or hand[4].rank == hand[1].rank)

def isStraight(hand): //requires sorted hand with no pair
    return (hand[0].rank == hand[4].rank - 4) or (isAce(hand[0]) and hand[4].rank == 5)

def isFlush(hand):
    return hand[0].suit == hand[1].suit == hand[2].suit == hand[3].suit == hand[4].suit

If you sort the hand then read it left to right it should be relatively easy to compute the hand strength.

If you are aiming for simplicity, you can try something like this (pseudocode):

def handStrength(hand):
    sort(hand) //sorts hand in ascending order
    if hasPair(hand):
        if isTwoPair(hand):
            return "two pair"
        else if isTrips(hand):
            return "three of a kind"
        else if isBoat(hand):
            return "full house"
        else if isQuads(hand):
            return "four of a kind"
        else:
            return "pair"
    else:
        straightFlag = isStraight(hand)
        flushFlag = isFlush(hand)
        if straightFlag:
            if flushFlag:
                return "straight flush"
            else:
                return "straight"
        else if flushFlag:
            return "flush"
        else:
            return "high card"

def hasPair(hand): //requires sorted hand
    for i = 1 to 4:
        if hand[i].rank == hand[i-1].rank:
            return True
    return False

def isTwoPair(hand): //requires sorted hand
    return (hand[0].rank == hand[1].rank and hand[2].rank == hand[3].rank and hand[0].rank != hand[2].rank) or
           (hand[1].rank == hand[2].rank and hand[3].rank == hand[4].rank and hand[1].rank != hand[3].rank)

def isTrips(hand): //requires sorted hand
    return (hand[0].rank == hand[2].rank and hand[0].rank != hand[3].rank and hand[3].rank != hand[4].rank) or
           (hand[1].rank == hand[3].rank and hand[0].rank != hand[1].rank and hand[0].rank != hand[4]) or
           (hand[2].rank == hand[4].rank and hand[1].rank != hand[2].rank and hand[1].rank != hand[0].rank)

def isBoat(hand): //requires sorted hand
    return (hand[0].rank == hand[1].rank and hand[2].rank == hand[3].rank == hand[4].rank) or
           (hand[0].rank == hand[1].rank == hand[2].rank and hand[3].rank == hand[4].rank)

def isQuads(hand): //requires sorted hand
    return hand[1].rank == hand[2].rank == hand[3].rank and (hand[0].rank == hand[1].rank or hand[4].rank == hand[1].rank)

def isStraight(hand): //requires sorted hand with no pair
    return (hand[0].rank == hand[4].rank - 4) or (isAce(hand[0]) and hand[4].rank == 5)

def isFlush(hand):
    return hand[0].suit == hand[1].suit == hand[2].suit == hand[3].suit == hand[4].suit
吃颗糖壮壮胆 2024-07-21 07:29:41

非常规但简洁:

import fj.P;
import fj.data.Stream;
import static fj.P2.untuple;
import static fj.pre.Equal.intEqual;

public Stream<Integer> pairs(Stream<Integer> hand) {
  return hand.apply(hand.map(P.<Integer, Integer>p2()))
             .filter(untuple(intEqual.eq()))
             .map(P1.<Integer>__1());
}

在此处获取导入的库

Unconventional, but terse:

import fj.P;
import fj.data.Stream;
import static fj.P2.untuple;
import static fj.pre.Equal.intEqual;

public Stream<Integer> pairs(Stream<Integer> hand) {
  return hand.apply(hand.map(P.<Integer, Integer>p2()))
             .filter(untuple(intEqual.eq()))
             .map(P1.<Integer>__1());
}

Get the imported libraries here

摇划花蜜的午后 2024-07-21 07:29:41

检查一对并返回布尔值几乎没有用。 您需要从最高到最低检查手牌(同花、4 种、葫芦、同花、顺子等),并创建一种方法对您的手牌进行排名,以便确定获胜者。 在我看来,这并不是一件小事。

Checking for a pair and returning a boolean is pretty much useless. You need to check for the hands starting from the highest to lowest (straight flush, 4 kind, full house, flush, straight, etc...) and create a way to rank your hands so you can determine the winner. It is not trivial in my opinion.

爱要勇敢去追 2024-07-21 07:29:41

德州扑克游戏评估器的完整源代码可以在这里找到:

http://www.advancedmcode.org/poker-predictor.html Advancedmcode.org/poker-predictor.html

它是为 matlab 构建的,GUI id m 编码,但计算引擎是 c++。

它允许进行赔率和概率计算。 在我的 2.4Ghz 笔记本电脑上,它可以在 0.3 秒内完成 100000 名 10 名玩家的游戏计算。

准确的实时计算机:-)

Complete source code for Texas hold'em poker game evaluator can be found here:

http://www.advancedmcode.org/poker-predictor.html

It is built for matlab, the GUI id m-coded but the computational engine is c++.

It allows for odds and probability calculation. It can deal, on my 2.4Ghz laptop, with a 100000 10 players game computation in 0,3 seconds.

An accurate real time computer:-)

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