java - 德州扑克识别葫芦

标签 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;
    }

最佳答案

获取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;
    }

关于java - 德州扑克识别葫芦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16866368/

相关文章:

java - gerResources().getStringArray(R.array.string_name); <-- 不工作

java - Android java.lang.IllegalStateException : Could not execute method of the activity

c# - 抑制 Console.Write 在引用包中

c# - 从 ASP.Net Web 应用程序在 Azure 虚拟机中远程运行 .exe

PHP 获取扑克的所有独特棋盘结果

algorithm - 进行扑克游戏的伪代码

java - JOptionPane 不存储字符串

c# - 如何从 Windows 服务运行控制台应用程序?

在手中找到街道和同类的算法

java - 如何在 for 循环中调用函数?