有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

爪哇德州扑克,满座

我正在创建一个控制台德州扑克。我已经完成了这个游戏,一切正常,除了满屋子的人,我还没有决定如何最好地编写代码

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

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

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

但这是不可能的,因为我正在处理一个问题(我在这里就是为了这个问题), isPair()和{}会找到一张相同的卡,比如说{},所以我们有一对{}和{},但也有{}会停留在一种三位

代码更新:

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) 个答案

  1. # 1 楼答案

    像这样一个“天真”的扑克手部评估器(即,一个单独匹配每种手部类型的评估器)应该按照以下顺序测试手部:直冲,然后冲,直冲,四边形,满屋,然后绊倒,然后两对,最后是单对。当您按照该顺序执行操作时,找到函数时,应该从该函数返回。按等级对手进行排序会更容易。此外,使用文本作为内部表示也是一个坏主意。看看我关于这个主题的文章:Representing Cards in Software。这篇文章中还有很多链接指向更复杂的扑克代码,包括我自己的(有Java绑定)

  2. # 2 楼答案

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

    。。。因为一个完整的房子只有两个不同的值(f.ex a 7)

  3. # 3 楼答案

    获取isThreeOfKind以返回卡值,如果没有三种类型,则返回空白字符。然后isPair应该接受一个要忽略的卡值。所以,你的满座支票变成了isPair(playerIndex, isThreeOfKind(playerIndex))

    注意,您的正常三类检查现在应该是if (isThreeOfKind(playerIndex)!=''),然后它是三类。正常情况下,对检查变成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;
        }