【发布时间】:2017-03-26 21:20:58
【问题描述】:
我做了一个快速扑克游戏。它生成 5 个随机数,并根据它们的值将这些数字转换为实际的卡片值和符号。但是,在进行手部评估时,我遇到了问题。
到目前为止,我只做了正确的冲洗,因为它真的很容易,但即使那样它也不是完美的(它打印出用户有 5 次冲洗......)如果有人可以帮助我,我将非常感激,两对,三对,直。之后我可以做剩下的事情,但我只需要提前了解如何做这些事情。
提前感谢您的帮助,这里是代码:
package tests;
import java.util.*;
public class TESTS {
public static void main(String[] args) {
boolean[] pack = new boolean[52]; // Array to not generate the same number twice
int[] cards = new int[5]; //The 5 unique random numbers are stored in here.
String[] cardsvalues = new String[5]; // This will assign the card's value based on the random number's value
char[] cardssymbols = new char[5];//This will assign the card's symbol based on the random number's value
char symbols[] = {'♥', '♦', '♣', '♠'}; // possible symbols that the random number can take
String values[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; // possible values that the random number can take
Random give = new Random();
for (int i = 0; i < cards.length; i++) { // Gives 5 unique random numbers
do {
cards[i] = give.nextInt(52);
} while (pack[cards[i]]);
pack[cards[i]] = true;
System.out.println(cards[i]);
}
for (int i = 0; i < cards.length; i++) { // This converts the number to a card symbol based on the number's value
final int numOfSymbol = cards[i] / 13;
cardssymbols[i] = symbols[numOfSymbol];
}
for (int i = 0; i < cards.length; i++) { // This converts the number to an actual card value based on the number's value.
final int numOfValues = cards[i] % 13;
cardsvalues[i] = values[numOfValues];
}
for (int i = 0; i < cardssymbols.length; i++) { // Prints the actual cards once they are converted
System.out.print(cardssymbols[i]);
System.out.println(cardsvalues[i]);
}
for (int i = 0; i < cardsvalues.length; i++) { //Here is the problem, i have no idea on how to make the handevaluator ...
if (cardsvalues[i] == cardsvalues[i] + 1) {
System.out.println("PAIR !!!");
} else if (cardsvalues[i] == cardsvalues[i] + 1 && cardsvalues[i] == cardsvalues[i] + 2) {
System.out.println("TRIPS !!!");
} else if (cardssymbols[0] == cardssymbols[1] && cardssymbols[1] == cardssymbols[2] && cardssymbols[2] == cardssymbols[3] && cardssymbols[3] == cardssymbols[4]) {
System.out.println("FLUSHHH");
}
}
}
【问题讨论】:
-
第一步。摆脱开关。
-
第二步:在你输入之前想一想:检查牌组本身有什么意义?
-
我不明白为什么要去掉开关?
-
只写
cardssymbols[i] = symbols[cards[i]/13]之类的东西,而不是在 switch 中解码它——对于值也是如此。这是第一步,在代码开始可用之前还有很多。 -
@Adrinn 你不需要。但是你需要充分理解你的代码,才能看出切换是错误的方法。
标签: java arrays conditional-statements poker