1

这只是代码的一部分,所以如果您看不到数组或其他任何东西,请不要介意。这个想法是我有 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");
    }
}
4

8 回答 8

4

一方面,你的数组可能应该只有 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;
  }
 }

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

于 2009-03-04T01:30:32.063 回答
4

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

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

于 2009-03-04T03:55:11.513 回答
2

你是如何处理甲板上的西装的?如果您将卡片表示为简单的整数,那么我假设卡片的有效值为 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

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

假设,例如,在你的 52 张牌中,编号为 0 - 51,牌 0 - 12 代表从 A 到梅花 K,牌 13 - 25 代表红心,26 - 38 代表黑桃,39 - 51 代表钻石。

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

通过取每张卡的余数模数 13,您剩下 0、12、6、8、8

您可以看到最后两张牌是一对,红桃 9 和方块 9(请记住编号从 0 开始,因此减一)。

于 2009-03-04T04:05:29.227 回答
1

我假设您正在寻找与三种或四种不同的一对。在这种情况下,您最好的选择是遍历每张牌,并存储有多少 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 等)也是微不足道的。

于 2009-03-04T01:43:24.093 回答
0

非常规,但简洁:

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());
}

在此处获取导入的库

于 2009-03-04T05:55:01.007 回答
0

如果你对手进行排序,然后从左到右阅读,计算手的力量应该相对容易。

如果你的目标是简单,你可以尝试这样的事情(伪代码):

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
于 2009-03-04T04:07:18.223 回答
0

检查一对并返回一个布尔值几乎没有用。您需要检查从最高到最低的手牌(同花顺、4 种、满屋、同花、顺子等),并创建一种方法来排列您的手牌,以便确定获胜者。在我看来,这不是微不足道的。

于 2009-03-04T04:34:09.690 回答
0

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:-)

于 2010-02-13T09:40:20.180 回答