在扑克手中找到一个偶然的直线

时间:2014-11-16 22:38:05

标签: java arrays poker

所以我的教授决定聪明一点,为我的扑克项目组成了一个名为EVEN STRAIGHT的手。它就像一个直线,除了卡需要连续的偶数。例如,8,6,2,4,10是平直的。此外,ace(值为1)可以用作“高端”或“低端”,这意味着它可以是1或14,具体取决于其他卡。我无法按升序排序数组中卡的值,因为我们无法在此项目中使用.sort方法。有人能帮我吗?它也是一个介绍性的Java类,所以请尽量使它尽可能简单。谢谢。 这就是我到目前为止......

//假设只有5张牌作为参数传入。

public static boolean hasEvenStraight(Card [] cards) {  
        boolean evenCard = false;
        int [] value = new int[cards.length];
        for(int i = 0; i<cards.length; i++){
            Card myCard = cards[i];
            value[i] = myCard.getValue();
            if(value[i]%2 == 0){
                evenCard = true;
            }
            else
                evenCard = false;
        }
        if(evenCard){
            //This is where I am stuck
        }
        return false; 
    }

1 个答案:

答案 0 :(得分:3)

首先,代码中存在逻辑错误。在for循环中,您正在检查当前卡是偶数还是奇数。但是当循环结束时,你剩下的就是你看到的最后一张牌。你需要检查它们是否都是均匀的。

    for(int i = 0; i<cards.length; i++){
        Card myCard = cards[i];
        value[i] = myCard.getValue();
        if ( value[i] == 1 ) {
           value[i] = 14;
        }
        if(value[i]%2 != 0)
            return false
        }
    }

这个循环,如果它发现卡片不均匀,它会立即返回false,因为即使是一张奇数卡片也意味着你没有平直。所以在它结束之后你知道你已经全部都是。

但是,这当然是不够的。您想知道这些卡是否连续。现在就是诀窍:你实际上不必保存value数组中的所有卡值。您只需保留最高和最低的一个。完成循环后,如果你知道它们都是均匀的,那么你的最高 - 你的最低= 8,这意味着你有连续的平均值,即平直。

为什么呢?因为如果它们不是连续的,那么即使在中间也是如此,对吧?但是如果最低值比最低值低8,那么你就无法在它们之间推出3张牌,这样它们就会变得均匀。

但我们需要注意相同数量的牌,例如2个黑桃和2个心。他们将破坏这一原则。如果它们存在,无论如何它都不是平直的。

为了检查这一点,我们必须为我们处理的每张卡片值保留一个标记,说&#34;我们是否已处理此卡片值&#34;?我们可以使用像Set这样的东西。在检查每个号码后,我们会问:&#34;我们已经检查了这组数字中的这个数字了吗?&#34;。如果是这样,那么当然,这不是一个平直的。如果没有,我们将当前数字添加到集合中,以便可以检查以下数字。

对于小integers的范围,我们可以在没有实际Java Set的情况下执行此操作。我们使用一组名为alreadyThere的布尔值或类似的东西。索引i处的元素表示&#34;我们检查了其值为i&#34;的卡片。它有点浪费空间,因为我们永远不会使用奇数索引或零和一个索引,但它很容易实现。只需检查alreadyThere[cardValue]是否为true。当然,在我们检查后它是一个唯一的数字后,我们会将alreadyThere[cardValue]设置为true以进行下一次迭代检查。

所以让我们修改你的方法:

public static boolean hasEvenStraight(Card [] cards) {  

    int low = 20; // There is no 20 card.
    int high = 0; // There is no 0 card.

    // This array is a bit wasteful as it won't all be used,
    // but it's straightforward this way.

    boolean[] alreadyThere = new boolean[15];

    for(int i = 0; i<cards.length; i++){
        Card myCard = cards[i];
        int currValue = myCard.getValue();

        // Handle ace. If it's 1 it's not an
        // even hand anyway, so assume it's 14.
        if ( currValue == 1 ) {
           currValue = 14;
        }

        // If any card is not even, this is not an Even Straight.
        if(currValue%2 != 0){
            return false;
        }

        // We have two cards of the same number
        // (E.g. 2 of spades and 2 of hearts). So
        // not a straight.
        if ( alreadyThere[currValue] ) {
           return false;
        }
        alreadyThere[currValue] = true;

        // To get the lowest and highest, compare each value to
        // existing lowest and highest and change them accordingly.
        if ( currValue > high ) {
           high = currValue;
        }
        if ( currValue < low ) {
           low = currValue;
        }
    }

    // Loop finished. All the numbers are even, now check if they
    // are consecutive.
    return ( high - low ) == 8;
}