德州扑克扑克识别满堂红

时间:2013-05-31 21:35:14

标签: java console-application poker

我正在创建一个控制台德州扑克。我已经完成了这个游戏,一切都按照预期运行,期待一个完整的房子,我为此找不到最好的编写代码的方法。

这就是我出示卡片的方式:“D5”,“S2”,“SA”......我知道代表卡片是一个坏主意,但我目前没有以OOP方式思考,我实际上是玩索引,这是一个很好的代码实践。

所以,问题不在于如何写一对或三种,我实际上有一个好主意做这样的事情......

if (isPair() && isThreeOfKind()) {
   //
}

但这是不可能的,因为我正在处理一个问题(我在这里), isPair()isThreeOfAKind()会找到相同的卡片,比方说"DA", "CA", "SA",因此我们有一对"DA""CA",还有"DA", "CA", "SA"它只能保持三种。

代码更新:

public boolean isPair(int playerIndex) {
        boolean isPair = false;

        if (hasSameRank(playerAndHand[playerIndex])) {
            isPair = true;
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                for (int j = 0; j < HAND_CARDS_LENGTH; j++) {
                    if (playerAndHand[playerIndex][j].charAt(1) == tableCards[i].charAt(1)) {
                        isPair = true;
                        break;
                    }
                }
                if (isPair) break; 
            }
        }
        return isPair;
    }

public boolean isThreeOfKind(int playerIndex) {
        boolean isThreeOfKind = false;

        // 2 from player hand 1 from table
        if (hasSameRank(playerAndHand[playerIndex])) {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    isThreeOfKind = true;
                    break;
                }
            }
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                // first card in player hand and 2 more on table
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][0].charAt(1) == tableCards[j].charAt(1)) {
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                    // second card in player hand and 2 more on table   
                } else if (playerAndHand[playerIndex][1].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][1].charAt(1) == tableCards[j].charAt(1)) {
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                }                   
            }
        }
        return isThreeOfKind;
    }

3 个答案:

答案 0 :(得分:0)

  

if(isThreeOfKind()&amp;&amp; cardTypes()== 2&amp;&amp;!(isFourOfKind()))

...因为满屋只有2个不同的值(f。ex A A A 7 7)

答案 1 :(得分:0)

这样一个“天真”的扑克牌手评估员(也就是说,每个手牌分别匹配一个)应该按照这个顺序测试手:直冲,然后冲,直,四边形,满屋,然后是旅行,然后两个对,最后是单对。当您按顺序执行该功能时,应该从该功能返回。按等级对手进行排序使其更容易。此外,使用文本作为内部表示是一个坏主意。看看我关于这个主题的文章:Representing Cards in Software。在那篇文章中还有许多链接到更复杂的扑克代码,包括我自己的(具有Java绑定)。

答案 2 :(得分:0)

获取isThreeOfKind以返回卡片值,如果没有三种,则返回空白字符。然后isPair应该接受要忽略的卡值。因此,您对Full House的检查将变为isPair(playerIndex, isThreeOfKind(playerIndex))

注意,你正常的三种检查现在应该是if (isThreeOfKind(playerIndex)!=''),那么它是三种。您的正常Is Pair检查变为if (isPair(playerIndex,''))然后它是一对。

示例:

public boolean isPair(int playerIndex, char charToIgnore) {
        boolean isPair = false;

        if (hasSameRank(playerAndHand[playerIndex])) {
            isPair = true;
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                if (tableCards[i].charAt(1) == charToIgnore) continue;
                for (int j = 0; j < HAND_CARDS_LENGTH; j++) {                        
                    if (playerAndHand[playerIndex][j].charAt(1) == tableCards[i].charAt(1)) {
                        isPair = true;
                        break;
                    }
                }
                if (isPair) break; 
            }
        }
        return isPair;
    }

public char isThreeOfKind(int playerIndex) {
        boolean isThreeOfKind = false;
        char cardValue = '';

        // 2 from player hand 1 from table
        if (hasSameRank(playerAndHand[playerIndex])) {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    cardValue = tableCards[i].charAt(1);
                    isThreeOfKind = true;
                    break;
                }
            }
        } else {
            for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
                // first card in player hand and 2 more on table
                if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][0].charAt(1) == tableCards[j].charAt(1)) {
                                cardValue = tableCards[j].charAt(1);
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                    // second card in player hand and 2 more on table   
                } else if (playerAndHand[playerIndex][1].charAt(1) == tableCards[i].charAt(1)) {
                    for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
                        if (j != i) {
                            if (playerAndHand[playerIndex][1].charAt(1) == tableCards[j].charAt(1)) {
                                cardValue = tableCards[j].charAt(1);
                                isThreeOfKind = true;
                                break;
                            }
                        } else {
                            continue;
                        }
                    }
                    if (isThreeOfKind) break;
                }                   
            }
        }
        return cardValue;
    }
相关问题