1

我正在创建控制台德州扑克。我已经完成了这个游戏的制作,一切都按预期进行,期待一个完整的房子,我不确定是否有最好的方法来编写代码。

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

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

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

但这是不可能的,因为我正在处理一个问题(我在这里), isPair()并且isThreeOfAKind()会找到同一张牌,比如说"DA", "CA", "SA",所以我们有一对"DA"and "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;
    }
4

3 回答 3

0

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

...因为满屋只有 2 个不同的值(例如 AAA 7 7)

于 2013-05-31T21:43:48.470 回答
0

像这样的“幼稚”扑克牌评估器(即分别匹配每种手型的)应该按以下顺序测试手牌:同花顺,然后同花,顺子,四边形,满屋,然后三条,然后是两对,然后最后是单对。当您按该顺序执行它们时,您应该从该函数中返回。按等级对手牌进行排序会更容易。此外,使用文本作为您的内部表示是一个坏主意。查看我关于该主题的文章:Representing Cards in Software。在那篇文章中还有很多链接到更复杂的扑克代码,包括我自己的(它有一个 Java 绑定)。

于 2013-05-31T21:58:52.490 回答
0

如果没有三种isThreeOfKind类型,则返回卡值或空白字符。然后isPair应该接受一个卡值忽略。所以,你的满屋支票变成了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;
    }
于 2013-06-01T02:20:43.913 回答