java - Java 扑克骰子程序

标签 java methods dice poker

[编辑:我修复了两对和葫芦的混淆,但是我的最终得分没有对,并且显示最高值根本没有触发]

我正在开发一个玩骰子扑克游戏的项目。我所有的“计分”功能都在工作,除非骰子掷出两对[编辑:固定两对],它说这是一个葫芦。

以下是说明:

In this lab you will write a Java program that plays the game Poker Dice. In this game, five dice are rolled and scored as if they were a hand of playing cards. The game goes through two separate phases. In the first phase, a "hand" of dice is presented to the player. The player then selects which dice he wants to keep and which he wants to re-roll. Once that decision is finished, all of the dice that the player has marked to re-roll are re-rolled and the final "hand" of dice is displayed. The hand should then be scored according to the rules of Poker Dice (given below) and the result displayed on the screen.

评分规则如下: enter image description here

这是我的评分方法代码:

private static String getResult(int[] dice) {
    int i = 0;

    String results = "";

    int endResultsCheck = 0;

    int[] count = getCounts(dice);

    // Checks for 5 of a kind
    while (i < count.length) {
        if (count[i] == 5) {
            results = "Five of a kind!";
            endResultsCheck = 7;
        }
        i++;
    }

    // Checks for four of a kind
    i = 0;
    if (endResultsCheck != 7) {
        while (i < count.length) {
            if (count[i] == 4) {
                results = "Four of a kind!";
                endResultsCheck = 6;
            }
            i++;
        }
    }
    if (endResultsCheck < 6) {
        // Checks for full house
        i = 0;
        int check = 0;
        while (i < count.length) {
            if (count[i] == 3) {
                check++;
            }
            if (count[i] == 2) {
                check++;
            }
            i++;
        }
        if (check == 2) {
            results = "Full house!";
            endResultsCheck = 5;
        }
    }
    if (endResultsCheck < 5) {
        // Checks for three of a kind
        i = 0;
        while (i < count.length) {
            if (count[i] == 3) {
                results = "Three of a kind!";
                endResultsCheck = 4;
            }
            i++;
        }
    }
    if (endResultsCheck < 4) {
        // Checks for two pairs
        i = 0;
        int check = 0;
        while (i < count.length) {
            if (count[i] == 2) {
                check++;
            }
            i++;
        }
        if (check == 2) {
            results = "Two pairs!";
            endResultsCheck = 3;
        }
    }
    if (endResultsCheck < 3) {
        i = 0;
        while (i < count.length) {
            if (count[i] == 2) {
                results = "One pair!";
            }
            i++;
        }
        endResultsCheck = 2;
    }
    if (endResultsCheck == 0) {
        i = 0;
        int max = 0;

        while (i < dice.length) {
            if (max < dice[i]) {
                max = dice[i];
            }
            i++;
        }
        results = "Highest number is " + max + "!";
    }

    return results;

}

当引用方法getCounts()时,就是这样的代码:

private static int[] getCounts(int[] dice) {
    int[] count = new int[10];
    int i = 0;

    int one = 0;
    while (i < dice.length) {
        if (dice[i] == 1) {
            one++;
        }
        i++;
    }
    int two = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 2) {
            two++;
        }
        i++;
    }
    int three = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 3) {
            three++;
        }
        i++;
    }
    int four = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 4) {
            four++;
        }
        i++;
    }
    int five = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 5) {
            five++;
        }
        i++;
    }
    int six = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 6) {
            six++;
        }
        i++;
    }
    int seven = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 7) {
            seven++;
        }
        i++;
    }
    int eight = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 8) {
            eight++;
        }
        i++;
    }
    int nine = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 9) {
            nine++;
        }
        i++;
    }
    int ten = 0;
    i = 0;
    while (i < dice.length) {
        if (dice[i] == 10) {
            ten++;
        }
        i++;
    }

    count[0] = one;
    count[1] = two;
    count[2] = three;
    count[3] = four;
    count[4] = five;
    count[5] = six;
    count[6] = seven;
    count[7] = eight;
    count[8] = nine;
    count[9] = ten;

    return count;

}

最佳答案

首先尝试调试(使用实际 Debug模式并放置断点或放置打印值)下面的代码部分。您的目标是找到该代码块内逻辑的漏洞。接下来尝试更新逻辑。

if (endResultsCheck < 6) {
    // Checks for full house
    i = 0;
    int check = 0;
    while (i < count.length) {
        if (count[i] == 3) {
            check++;
        }
        if (count[i] == 2) {
            check++;
        }
        i++;
    }
    if (check == 2) {
        results = "Full house!";
        endResultsCheck = 5;
    }
}
<小时/>

首先尝试自己做。寻找缺失的和平非常有趣。

if (count[i] == 2) { 当你有两对时,条件将两次为真。结果 check == 2 将为 true,您将收到“Full house”。

可能的解决方案之一:

if (endResultsCheck < 6) {
    // Checks for full house
    i = 0;
    boolean hasPair = false;
    boolean hasTriplet = false;
    while (i < count.length) {
        if (count[i] == 3) {
            hasTriplet = true;
        }
        if (count[i] == 2) {
            hasPair = true;
        }
        i++;
    }
    if (hasPair && hasTriplet) {
        results = "Full house!";
        endResultsCheck = 5;
    }
}

关于java - Java 扑克骰子程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53200188/

相关文章:

java - 如何从InputStream读取字符并将其返回到Supplier<Integer> (Java)

C:掷骰子/骰子游戏

c++ - 模拟掷 2 个骰子的程序

python - 卡住骰子滚动?

java - 获取在 map 上单击的位置的经纬度 forge 0.5.1 MapView

java - HashMap 是如何占用内存的?

python - Method that applys to a instance of a class in python//Apply a method to an instance of a class in python

java - Android应用登录注册报错

java - 将列表转换为其他类型

java - 我试图将双变量从一种方法调用到另一种方法中