- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
[编辑:我修复了两对和葫芦的混淆,但是我的最终得分没有对,并且显示最高值根本没有触发]
我正在开发一个玩骰子扑克游戏的项目。我所有的“计分”功能都在工作,除非骰子掷出两对[编辑:固定两对],它说这是一个葫芦。
以下是说明:
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.
这是我的评分方法代码:
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/
一副扑克牌是 52 张牌 13阶4花色 致力于高效的手部表示和评估 A K Q J T 9 8 7 6 5 4 3 2 scdh
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我遇到了一个真正令人困惑的错误,在过去的几个小时里我一直试图解决这个错误,但没有成功。我正在研究扑克实现。最初,我通过迭代循环生成卡片。 const suits = ['Heart', 'Spade'
我是一名优秀的程序员,十分优秀!