【问题标题】:Poker Dice Program in JavaJava中的扑克骰子程序
【发布时间】:2018-11-08 09:42:24
【问题描述】:

[编辑:我修复了两对和满堂彩的混淆,但是我的最终得分在没有对子并且显示最高值的情况下根本没有触发]

我正在做一个玩骰子扑克游戏的项目。除了骰子掷出两对[编辑:固定两对]时,我所有的“得分”都在工作,它说它是满堂彩。

以下是说明:

在本实验中,您将编写一个玩扑克骰子游戏的 Java 程序。在这个游戏中,五个骰子被滚动并得分,就好像它们是一手扑克牌一样。游戏经历了两个不同的阶段。在第一阶段,向玩家呈现骰子“手”。然后玩家选择他想要保留的骰子以及他想要重新掷的骰子。一旦该决定完成,玩家标记为重新掷骰子的所有骰子都会重新掷骰子,并显示最后的骰子“手”。然后应根据扑克骰子的规则(如下所示)对手牌进行评分,结果将显示在屏幕上。

以下是评分规则:

这是我的评分方法代码:

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;

}

【问题讨论】:

  • 你尝试调试了吗?
  • 检查endResultsCheck = 2;if (endResultsCheck &lt; 3) 块中,你应该把它移到if (count[i] == 2)块中
  • 提示:如果它没有进入你想要的if块,你可以随时检查它之前的条件。例如在这种情况下,我在 if (endResultsCheck == 0) 块之前添加了一行 System.out.println(endResultsCheck);,结果是 2,然后我知道我应该查看 endResultsCheck 设置为 2 的循环。

标签: java methods dice poker


【解决方案1】:

首先尝试调试(使用实际调试模式并放置断点或放置打印值)下面的代码部分。你的目标是在这个代码块中找到一个逻辑裂缝。接下来尝试更新逻辑。

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 为真,您将收到“满座”。

一种可能的解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2014-11-04
    • 1970-01-01
    相关资源
    最近更新 更多