java 纸牌游戏规则:手是从一包纸牌中抽出的(不要开玩笑)打牌时,只有当他们是

mlnl4t2r  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(213)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

5个月前关门了。
改进这个问题
帮助我如何接受输入。我不明白在主方法中添加什么
纸牌游戏规则:手是从一包纸牌中抽出的(不要开玩笑)。只有当他们是一种类型的3(…aaa…)或者4个(…aaaa…)。三张或三张以上的牌(…jqk。。。或者……a23456…。。。穿同一套衣服)。看看这名选手是否能打全手

//input a 4X13 matrix with 4 suits and 13 ranks of cards. set cards[suit][rank] to 1 if this card in hand.
public static boolean handClear(int[][] cards, int hand) {
    if(hand == 0) return true;
    for(int rank = 12; rank >= 0; rank--) {
        for(int suit = 0; suit < 4; suit++) {
            if(cards[suit][rank] == 1) { //if cards[suit][rank] in hand
                cards[suit][rank] = 0; hand--;
                int smallerRank = rank == 0 ? 12: rank - 1; // look for straight flush that end with this card
                // watch for Ace as a special case that***QKA and A23***both valid
                if(cards[suit][smallerRank] == 1) {
                    cards[suit][smallerRank] = 0; hand--;
                    int r = smallerRank - 1;
                    for(; r >= 0 && cards[suit][r] == 1; r--) {   //try playing the straight flush found
                        cards[suit][r] = 0; hand--;
                        if(handClear(cards, hand)) return true;
                    }
                    r++;
                    for(; r <= smallerRank; r++) {  //backtrack if play did not work
                        cards[suit][r] = 1; hand++;
                    }
                }
                //look for 3/4 of a kind for cards[suit][rand]
                int n = cards[0][rank] + cards[1][rank] + cards[2][rank] + cards[3][rank];
                if(n == 3 || n == 2) {
                    int tmp1 = cards[(suit + 1) % 4][rank],
                        tmp2 = cards[(suit + 2) % 4][rank],
                        tmp3 = cards[(suit + 3) % 4][rank];
                    cards[(suit + 1) % 4][rank] = 0; //try playing the 3/4 of a kind
                    cards[(suit + 2) % 4][rank] = 0;
                    cards[(suit + 3) % 4][rank] = 0;
                    hand -= n;
                    if(handClear(cards, hand)) return true;
                    cards[(suit + 1) % 4][rank] = tmp1;   //backtrack if play did not work
                    cards[(suit + 2) % 4][rank] = tmp2;
                    cards[(suit + 3) % 4][rank] = tmp3;
                    hand += n;
                }
                cards[suit][rank] = 1; hand++;
            }
        }
    }
    return false;
}
to94eoyn

to94eoyn1#

我相信您只需要解释一下如何最初调用您在问题中发布的方法。
方法参数 cards 表示牌组中的所有牌。
有四套西装,即梅花、钻石、红桃和黑桃。
显然每件衣服都有一个相关的索引。据我所知,你发布的代码,可以简单地将索引与西装任意关联。所以我们假设如下:
0=球杆
1=钻石
2=心形
3=黑桃
因此阵列的尺寸 cards 应该是4和13,因为每一套有13张牌。所以你需要创建一个二维数组。

int[][] cards = new int[4][13];

现在你需要把一张西装卡片和一个索引联系起来。假设王牌有索引0(0),国王有索引12(12)。
这意味着 cards[0][0] 代表俱乐部的王牌 cards[2][10] 代表红心杰克。
我的扑克知识有点生疏了,所以我可能错了,但是一张扑克手是由五张牌组成的。
所以你需要初始化 cards 并将每个元素设置为0(零),即。

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 13; j++) {
        cards[i][j] = 0;
    }
}

现在你需要“交易”一只手。您需要在中设置值 cards 卡的索引为1(一)。你是如何随机发牌的,我不知道,但是假设你给自己发了7张黑桃,那就意味着你需要做以下的事情。

cards[3][6] = 1;

请注意您发布的方法,即。 handClear ,是一种递归方法。这意味着该方法调用自身。所有递归方法都必须具有终止递归的条件。in方法 handClear 当[method]参数 hand 等于0(零)。这意味着 hand 必须是手中“已发”的牌数。正如我之前所说的,对于扑克来说,我相信是5。但这不重要,因为方法 handClear 将处理参数的任何值 hand 它收到的。
所以为了最初调用 handClear ,您需要确定一手牌有多少张牌,例如扑克有5张牌。然后你必须创建二维数组并初始化它,就像我上面解释的那样,最后你必须选择 hand 不同的元素 cards 并将元素值设置为1(一)。然后可以调用方法 handClear .
上面的一个非常简单的例子:

int[][] cards = new int[4][13];
for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 13; j++) {
        cards[i][j] = 0;
    }
}
int hand = 5;
cards[0][0] = 1; // Ace of Clubs
cards[1][0] = 1; // Ace of Diamonds
cards[2][0] = 1; // Ace of Hearts
cards[3][0] = 1; // Ace of Spades
cards[0][1] = 1; // Two of Clubs
if (handClear(cards, hand)) {
    System.out.println("You have a winning hand!");
}

相关问题