我正在创建控制台德州扑克。我已经完成了这个游戏的制作,一切都按预期进行,期待一个完整的房子,我不确定是否有最好的方法来编写代码。
这就是我展示卡片的方式:“D5”、“S2”、“SA”......我知道表示卡片是个坏主意,但我目前没有以 OOP 方式思考,我实际上是在玩索引,这是一个很好的代码实践。
所以,问题不在于如何写一对或三个同类,我实际上有一个好主意来做这样的事情......
if (isPair() && isThreeOfKind()) {
//
}
但这是不可能的,因为我正在处理一个问题(我在这里),
isPair()
并且isThreeOfAKind()
会找到同一张牌,比如说"DA", "CA", "SA"
,所以我们有一对"DA"
and "CA"
,但也"DA", "CA", "SA"
有一个三.
代码更新:
public boolean isPair(int playerIndex) {
boolean isPair = false;
if (hasSameRank(playerAndHand[playerIndex])) {
isPair = true;
} else {
for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
for (int j = 0; j < HAND_CARDS_LENGTH; j++) {
if (playerAndHand[playerIndex][j].charAt(1) == tableCards[i].charAt(1)) {
isPair = true;
break;
}
}
if (isPair) break;
}
}
return isPair;
}
public boolean isThreeOfKind(int playerIndex) {
boolean isThreeOfKind = false;
// 2 from player hand 1 from table
if (hasSameRank(playerAndHand[playerIndex])) {
for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
isThreeOfKind = true;
break;
}
}
} else {
for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
// first card in player hand and 2 more on table
if (playerAndHand[playerIndex][0].charAt(1) == tableCards[i].charAt(1)) {
for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
if (j != i) {
if (playerAndHand[playerIndex][0].charAt(1) == tableCards[j].charAt(1)) {
isThreeOfKind = true;
break;
}
} else {
continue;
}
}
if (isThreeOfKind) break;
// second card in player hand and 2 more on table
} else if (playerAndHand[playerIndex][1].charAt(1) == tableCards[i].charAt(1)) {
for (int j = 0; j < TABLE_CARDS_LENGTH; j++) {
if (j != i) {
if (playerAndHand[playerIndex][1].charAt(1) == tableCards[j].charAt(1)) {
isThreeOfKind = true;
break;
}
} else {
continue;
}
}
if (isThreeOfKind) break;
}
}
}
return isThreeOfKind;
}