[英]Checking for an an enum of the current enum
In making a card game I came to a Straight, the numbers were set as an enum ace, two, three etc. and I wrote it like this 在制作纸牌游戏时,我遇到了一个顺子,数字被设定为枚举王牌,二,三等,我这样写
List<Number> number = Arrays.asList(Number.two, Number.two, Number.three, Number.four, Number.five);
private void Straight() {
for(int currentCard = 0; currentCard < 4; currentCard++){
for(int searchCard = 0; searchCard < 4; searchCard++){
if (number.get(searchCard) == number.get(currentCard ++)){
if (number.get(searchCard ++) == number.get(currentCard + 2)){
if (number.get(searchCard + 2) == number.get(currentCard + 3)){
}
}
}
}
Looking at this I see currentCard will loop through the hand and not the enum set like I would like it to do. 看着这个,我看到currentCard将遍历该手,而不是像我希望的那样遍历枚举集。 Which way would be most efficient to do this
哪种方法最有效
Edit: I tried writing up some code like this 编辑:我试图写一些这样的代码
Number currentCard = number.get(0);
but like I expected it was not equal to an integer value and now I am wondering if there is someway to put it equal to an integer. 但是就像我期望的那样,它不等于整数值,现在我想知道是否有某种方式可以使它等于整数。
One of the primary things that make enums special is that they can be compared with == like primitive type. 使枚举特别的主要事情之一就是可以将它们与==进行比较,例如原始类型。 Try looping through your enum tokens instead of ints.
尝试遍历枚举令牌而不是ints。
List<Number> playerHand = Arrays.asList(Number.two, Number.two, Number.three, Number.four, Number.five);
public boolean isStraight(List<Number> hand) {
int straightPosition = 0;
boolean inStraightRun = false;
Number lastStraightCard;
for (int i = 0; i < Number.values().length; i++) {
Number card = Number.values()[i];
if (!inStraightRun) {
if (card == hand[straightPosition]) {
inStraightRun = true;
lastStraightCard = card;
straightPosition++;
}
else if (card == Number.ten) {
break;//straight no longer possible
}
}
else {
if (card == hand[straightPosition] &&
lastStraightCard == Number.values()[i - 1]) {
lastStraightCard = card;
straightPosition++;
}
else {
break;//straight no longer possible
}
if (straightPosition > hand.length() - 1) {
break;//straight! Stop looping
}
}
}
return straightPosition == hand.length();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.