gpt4 book ai didi

java - 德州扑克识别葫芦

转载 作者:行者123 更新时间:2023-12-01 04:39:14 25 4
gpt4 key购买 nike

我正在创建一个控制台德州扑克。我已经完成了这个游戏的制作,一切都按预期进行,期待满座,但我还没有决定编写代码的最佳方式。

这就是我呈现卡片的方式:“D5”、“S2”、“SA”...我知道呈现卡片是一个坏主意,但我目前并没有以 OOP 方式思考,我实际上是使用索引,这是一个很好的代码实践。

所以,问题不在于如何编写一对或三个类似的内容,我实际上有一个好主意来做这样的事情......

if (isPair() && isThreeOfKind()) {
//
}

但这是不可能的,因为我正在处理一个问题(我在这里), isPair()isThreeOfAKind()会找到同一张卡,比如说"DA", "CA", "SA" ,所以我们有一对 "DA""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;
}

最佳答案

获取isThreeOfKind以返回卡值,如果没有三种类型,则返回空白字符。然后 isPair 应该接受要忽略的卡值。因此,您对葫芦的检查变为 isPair(playerIndex, isThreeOfKind(playerIndex))

请注意,您正常的“三类”检查现在应该是 if (isThreeOfKind(playerIndex)!='') 那么它是三类。您正常的 Is Pair 检查变为 if (isPair(playerIndex,'')) 那么它是一对。

示例:

public boolean isPair(int playerIndex, char charToIgnore) {
boolean isPair = false;

if (hasSameRank(playerAndHand[playerIndex])) {
isPair = true;
} else {
for (int i = 0; i < TABLE_CARDS_LENGTH; i++) {
if (tableCards[i].charAt(1) == charToIgnore) continue;
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 char isThreeOfKind(int playerIndex) {
boolean isThreeOfKind = false;
char cardValue = '';

// 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)) {
cardValue = 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)) {
cardValue = 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)) {
cardValue = tableCards[j].charAt(1);
isThreeOfKind = true;
break;
}
} else {
continue;
}
}
if (isThreeOfKind) break;
}
}
}
return cardValue;
}

关于java - 德州扑克识别葫芦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16866368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com