Card game rule: the hand is drawn from a pack of cards (no jokers). Play cards ONLY when they are

huangapple go评论84阅读模式
英文:

Card game rule: the hand is drawn from a pack of cards (no jokers). Play cards ONLY when they are

问题

帮我学习如何获取输入。我无法理解在主方法中添加什么。

卡牌游戏规则:从一副牌中抽取手牌(没有大小王)。只有在以下情况下才能打出牌:三张相同的牌(...AAA...)或四张相同的牌(...AAAA...)。三张或更多牌组成的顺子(...JQK...或...A23456…...,同花色)。找出玩家是否能够打完整副手牌。

// 输入一个4x13的矩阵,表示4种花色和13种牌面大小的牌。如果手牌中有这张牌,则将cards[suit][rank]设置为1。
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) { // 如果手牌中有cards[suit][rank]
                cards[suit][rank] = 0; hand--;
                int smallerRank = rank == 0 ? 12 : rank - 1; // 寻找以这张牌结尾的顺子
                // 特别注意Ace作为***QKA和A23***两种情况都成立的情况
                if (cards[suit][smallerRank] == 1) {
                    cards[suit][smallerRank] = 0; hand--;
                    int r = smallerRank - 1;
                    for (; r >= 0 && cards[suit][r] == 1; r--) {   // 尝试打出找到的顺子
                        cards[suit][r] = 0; hand--;
                        if (handClear(cards, hand)) return true;
                    }
                    r++;
                    for (; r <= smallerRank; r++) {  // 如果打出未成功,则回溯
                        cards[suit][r] = 1; hand++;
                    }
                }
                // 寻找三/四张大小相同的牌(3/4 of a kind)
                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; // 尝试打出三/四张相同的牌
                    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;   // 如果打出未成功,则回溯
                    cards[(suit + 2) % 4][rank] = tmp2;
                    cards[(suit + 3) % 4][rank] = tmp3;
                    hand += n;
                }
                cards[suit][rank] = 1; hand++;
            }
        }
    }
    return false;
}
英文:

Help me how to take the input. I can not understand what to add in the main method

Card game rule: the hand is drawn from a pack of cards (no jokers). Play cards ONLY when they are 3 of a kind (...AAA... ) or 4 of a kind(...AAAA...). a straight flush of 3 or more cards(...JQK... or ...A23456…... in the same suit). Find out whether the player will be able to play the whole hand

//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 &gt;= 0; rank--) {
for(int suit = 0; suit &lt; 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 &gt;= 0 &amp;&amp; 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 &lt;= 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;
}

答案1

得分: 1

我相信你只想要一个关于如何初始调用你在问题中贴出的方法的解释。

方法参数 cards 代表一副牌中的所有牌。
一副牌有四种花色,即梅花、方块、红心和黑桃。
显然,每种花色都有一个关联的索引。根据我理解你贴出的代码,将索引与花色任意关联都是可以的。因此,让我们假设以下关联:

0 = 梅花
1 = 方块
2 = 红心
3 = 黑桃

因此,数组 cards 的维度应为 4 和 13,因为每种花色有十三张牌。所以你需要创建一个二维数组,例如:

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

现在你需要将一个花色中的一张牌与一个索引关联起来。假设 Ace 的索引为 0,King 的索引为 12。

这意味着 cards[0][0] 代表梅花 A,cards[2][10] 代表红心 J。

我的扑克知识有点生疏,所以我可能有错,但一手扑克由五张牌组成。

所以你需要初始化 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 是一个递归方法。这意味着方法会调用自身。所有递归方法必须有一个终止递归的条件。在 handClear 方法中,这个条件是当方法参数 hand 等于 0 时。这意味着 hand 的值必须是“发”出的手中的牌数。正如我之前所说,对于扑克,我认为是五张牌。但这并不重要,因为方法 handClear 将处理它接收到的任何参数 hand 的值。

因此,为了最初调用 handClear 方法,你需要确定一手牌有多少张牌,例如扑克中的 5 张。然后你需要创建这个二维数组,并像我上面解释的那样进行初始化,最后你需要在 cards 中选择 hand 个不同的元素,并将元素值设置为 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; // 梅花 A
cards[1][0] = 1; // 方块 A
cards[2][0] = 1; // 红心 A
cards[3][0] = 1; // 黑桃 A
cards[0][1] = 1; // 梅花 2
if (handClear(cards, hand)) {
    System.out.println("你有一手获胜的牌!");
}
英文:

I believe you just want an explanation of how to initially call the method, that you posted in your question.

The method parameter cards represents all the cards in a deck.
There are four suits, i.e. Clubs, Diamonds, Hearts and Spades.
Obviously each suit has an associated index. As I understand the code you posted, it is OK to simply associate an index with a suit, arbitrarily. So let's assume the following:

0 = Clubs
1 = Diamonds
2 = Hearts
3 = Spades

Hence the dimensions of the array cards should be 4 and 13, since there are thirteen cards in each suit. So you need to create a two-dimensional array, e.g.

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

Now you need to associate a card in a suit with an index. Let's assume that the Ace has index 0 (zero) and that the King has index 12 (twelve).

This means that cards[0][0] represents the Ace of Clubs and cards[2][10] represents the Jack of Hearts.

My Poker knowledge is a bit rusty, so I may be wrong, but a Poker hand consists of five cards.

So you need to initialize cards and set each element to 0 (zero), i.e.

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

Now you need to "deal" a hand. You need to set the value in cards to 1 (one) for the index of the card. How you deal the cards randomly, I don't know but let's say you dealt yourself a 7 of Spades, that would mean you need to do the following.

cards[3][6] = 1;

Note that the method you posted, i.e. handClear, is a recursive method. That means the method calls itself. All recursive methods must have a condition that terminates the recursion. In method handClear that condition is when the [method] parameter hand equals 0 (zero). That means that the value of hand must be the number of cards in the hand that was "dealt". As I said earlier, for Poker, I believe it is five. But it doesn't matter since the method handClear will handle any value of parameter hand that it receives.

So in order to initially call method handClear, you need to determine how many cards make up a hand, for example 5 for Poker. Then you have to create the two-dimensional array and initialize it as I explained above, and finally you have to select hand different elements in cards and set the element value to 1 (one). Then you can call method handClear.

A very simple example of the above:

int[][] cards = new int[4][13];
for (int i = 0; i &lt; 4; i++) {
    for (int j = 0; j &lt; 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(&quot;You have a winning hand!&quot;);
}

huangapple
  • 本文由 发表于 2020年8月20日 22:42:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63507531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定