我怎样才能打印所有可能的“满堂彩”?用 Java 玩扑克?

发布于 2024-09-26 13:48:27 字数 1629 浏览 2 评论 0原文

我想知道是否有人知道如何用java实现代码来打印满堂彩的所有情况。大约有 3700 种不同的情况。到目前为止我已经2700左右了,但是我在换衣服时遇到了麻烦,她就是我目前所拥有的。

public class FullHouseTest 
{//
static int count = 1;
static int [] cards ={1,2,3,4,5,6,7,8,9,10,11,12,13};
static int[] suit ={1,2,3,4};
static int[] suit2 ={2,3,4,1};
static int[] suit3 ={3,4,1,2};
 public static void main(String[] args) 
 { 
  for(int k = 0; k< 12; k++)
  {
   for(int i = 0; i < 3; i++)
   {
    for (int t = 0; t <3; t++)
    { 
     Card one = new Card(new Suit(suit[t]), new Pips(cards[k]));
     Card two = new Card(new Suit(suit2[t]), new Pips(cards[k]));
     Card three = new Card(new Suit(suit3[t]),new Pips(cards[k]));

     for (int j =0; j < 12; j++)
      { 
        Card four = new Card(new Suit(suit2[i]), new Pips(cards[j+1]));
        Card five = new Card(new Suit(suit[i]), new Pips(cards[j+1]));
        System.out.println("Hand:" + count + " | " + one + two + three + four + five);
        count ++;
      }
    }
   }
  }
  for(int i = 0; i < 3; i++)
  {
   for(int k = 0; k< 12; k++)
   {
     for(int s = 0; s<3; s++)
     {
      Card one = new Card(new Suit(suit[i]), new Pips(cards[k]));
      Card two = new Card(new Suit(suit2[i]), new Pips(cards[k]));
     for (int j =0; j < 12; j++)
     {

      Card three = new Card(new Suit(suit3[s]),new Pips(cards[j+1]));
      Card four = new Card(new Suit(suit2[s]), new Pips(cards[j+1]));
      Card five = new Card(new Suit(suit[s]), new Pips(cards[j+1]));
      System.out.println("Hand:" + count + " | " + one + two + three + four + five);
      count ++;


     }
    }
   }
  }

 }
}

I was wondering if anyone knew how to implement the code in java to print all cases of full house. There are roughly 3700 different cases. So far i'm around 2700 but I am having trouble changing the suits, her is what I have so far.

public class FullHouseTest 
{//
static int count = 1;
static int [] cards ={1,2,3,4,5,6,7,8,9,10,11,12,13};
static int[] suit ={1,2,3,4};
static int[] suit2 ={2,3,4,1};
static int[] suit3 ={3,4,1,2};
 public static void main(String[] args) 
 { 
  for(int k = 0; k< 12; k++)
  {
   for(int i = 0; i < 3; i++)
   {
    for (int t = 0; t <3; t++)
    { 
     Card one = new Card(new Suit(suit[t]), new Pips(cards[k]));
     Card two = new Card(new Suit(suit2[t]), new Pips(cards[k]));
     Card three = new Card(new Suit(suit3[t]),new Pips(cards[k]));

     for (int j =0; j < 12; j++)
      { 
        Card four = new Card(new Suit(suit2[i]), new Pips(cards[j+1]));
        Card five = new Card(new Suit(suit[i]), new Pips(cards[j+1]));
        System.out.println("Hand:" + count + " | " + one + two + three + four + five);
        count ++;
      }
    }
   }
  }
  for(int i = 0; i < 3; i++)
  {
   for(int k = 0; k< 12; k++)
   {
     for(int s = 0; s<3; s++)
     {
      Card one = new Card(new Suit(suit[i]), new Pips(cards[k]));
      Card two = new Card(new Suit(suit2[i]), new Pips(cards[k]));
     for (int j =0; j < 12; j++)
     {

      Card three = new Card(new Suit(suit3[s]),new Pips(cards[j+1]));
      Card four = new Card(new Suit(suit2[s]), new Pips(cards[j+1]));
      Card five = new Card(new Suit(suit[s]), new Pips(cards[j+1]));
      System.out.println("Hand:" + count + " | " + one + two + three + four + five);
      count ++;


     }
    }
   }
  }

 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉默的熊 2024-10-03 13:48:27

在继续之前,请在代码中添加一些注释。它将帮助您了解发生了什么,特别是当您使用单字符变量名称嵌套 4 层循环时。

接下来,分解问题:满堂彩的真正独特之处是什么?两个点数都是唯一的,但不能相同。 3 同种有 3 种不同的花色(或者只是缺少一种),而配对有 2 种不同的花色。

total_pips * (total_pips-1) * number_suits *  (possible combinations of 2 suits )  = 3744
     13            12               4                         6

想想你可能从这个列表中遗漏了什么。如果您有任何具体问题,只需编辑答案,我们就会立即解决:)

Add some comments to your code before you go any further. It'll help you understand what's going on, especially when you have loops nested 4 deep with single-char variable names.

Next, break the problem down: What's really unique about a full house? Both of the # of pips are unique but can't be the same. The 3 of a kind has 3 different suits (or just lacks one), and the pair has 2 different suits.

total_pips * (total_pips-1) * number_suits *  (possible combinations of 2 suits )  = 3744
     13            12               4                         6

Think about what you might be missing from this list. If you have any specific questions just edit the answer and we'll get right on it :)

昵称有卵用 2024-10-03 13:48:27

主方法中的代码太多。你需要在这里更好地使用方法。

创建一个名为 isFullHouse(Card[] card) 的方法,该方法接受 5 张牌的数组(或 ArrayList),并为您确定这手牌是否为葫芦。

然后,如何选择创建所有可能的手牌组合就取决于您了。每次你获得新牌时,请调用此方法。它会为您简化事情。 main 中的所有内容都很难阅读。

至于你如何存储你的牌组。存储一个 0-51 的数组,而不是所有这些数组。您可以在阵列上使用除法和取模运算符来确定您拥有哪张卡。神奇数字是 13。

i.e. The 47 card in the deck could be: 47/13=3 ; 47 % 13 = 8

如果您提前确定 0=红心、1=方块、2=梅花和 3=黑桃,那么您可以确定这张牌是黑桃 9(8 +1,因为没有卡的值为 0,所以加 1)

将所有这些想法存储在它们自己的方法中,您可以大大简化循环。

Way too much code in a main method. you need to use methods better here.

create a method called isFullHouse(Card[] card), which takes in an array (or ArrayList) of 5 cards and will determine for you if the hand is a full house.

Then how you choose to create all possible combinations of hands is up to you. Each time you get a new hand, call this method. It will simplify things for you. Everything in main is very hard to read.

As far as how you store your deck of cards. instead of all those arrays, store one which is 0-51. you can use divide and mod operators on the array to determine which card you have. The magic number is 13.

i.e. The 47 card in the deck could be: 47/13=3 ; 47 % 13 = 8

If you determine ahead of time that 0=hearts, 1= diamonds, 2=clubs and 3=spades, then you can determine that this card is the 9 of spades (8+1 due to no card having the value 0 so add one)

Store all these ideas in their own methods, and you can simplify your loops considerably.

泅渡 2024-10-03 13:48:27

前几天我看到了这个问题(生病时)。从那时起我就一直在争论是否要插话。一方面,这看起来像是家庭作业。 (这是一个简单的问题。你的代码很难理解,说明缺乏经验。)

另一方面,我不介意帮忙。我不会为您做您的工作,但我可以为您指明正确的方向...


第一步:定义问题。一旦明确定义,答案就会变得更加简单。

一个“葫芦”,大​​概是 5 张牌,由三张同种加一对组成。据推测,这是一款单牌游戏。大概这是一副标准牌(A、2、3、4、5、6、7、8、9、J、Q、K),搭配花色(黑桃、梅花、红心、方块)。 (下面缩写为 (A23456789JQK) 和 (SCHD)。)

您提到了 3700 种组合。因此,您大概认为手 (2S,2C,2H,3H,3D) 和 (3D,3H,2H,2C,2S) 是等效的而不是不同的。 (这实际上是非常重要的一点,正如 Sean 和 Loadmaster 在他们的评论中提到的那样。有 311,875,200 (52*51*50*49*48) 种可能的 5 张牌抽牌。然而,其中只有 2,598,960 手是不同的! )

我们有 (13 * 4) 种可能的三元组。例如,对于每张牌(例如 3),我们可以有 4 个三类牌({0S,3C,3H,3D}、{3S,0C,3H,3D}、{3S,3C,0H, 3D},{3S,3C,3H,0D})。 (也许您开始注意到一种模式:0111 1011 1101 1110。)

给出我们的三张,并假设这是单副牌和标准副牌游戏,我们的对一定是其他 12 个剩余牌组之一。对于每个卡牌等级,配对有六种可能性。例如,给定卡等级为 7,我们可以有 ({7S,7C,0H,0D}, {7S,0C,7H,0D}, {7S,0C,0H,7D}, {0S,7C,7H,0D },{0S,7C,0H,7D},{0S,0C,7H,7D})。 (再次,也许您会注意到这种模式:1100 1010 1001, 0110 0101, 0011。)

这给了我们 13 * 4 * 12 * 6 = 3744 种组合。

从这里开始,只需循环打印即可。

我建议您考虑更具描述性的变量名称。虽然有些地方和时间可以使用单字符循环变量,但这不是其中之一。编写良好的代码几乎是自我记录的,允许文档集中于更复杂的更高级别的抽象。您节省的几个额外字符最终会花费您大量的调试时间。如果需要的话,你可以像我一样偷懒,学一下emacs,用(require 'completion), (global-set-key "\C-\\" 'complete),输入前几个字符,让emacs自动帮你补全。

我建议您考虑支持(也许是私有)方法。例如,您可以执行以下操作:(自从我上次使用 Java 编码以来已经有一段时间了。)

for ( suit = 0;  suit < 4 ;  ++ suit )
    private_printThreeOfAKind( card, suit!=0, suit!=1, suit!=2, suit!=3 )

其中三个 (!=) 为 true,一个为 false。

关于打印对,您可能需要研究继续语句。参考: http://en.wikipedia.org/wiki/Java_syntax#continue_statement

例如这个将允许您跳过配对卡与三张卡具有相同等级的情况:

if ( threeOfAKindCard == pairCard )
    continue;

我建议您分部分构建软件。即使对于专家来说,尝试构建一个完整的系统也很少奏效。构建零件,测试它们,冲洗,重复。是的,这意味着编写您不会转动的脚手架代码。也许甚至是一个测试子类......但是小步骤更容易开始工作。随着你作为程序员的进步,你将能够迈出更大的步伐......

I saw this problem the other day (while out sick). I've been debating about chiming in ever since. On one hand, it seems like homework. (It's a simple problem. Your code is difficult to understand, indicating of a lack of experience.)

On the other hand, I don't mind helping out. I'm not going to do your work for you, but I can point you in the right direction...


First step: Define the problem. Once clearly defined, answers become far more straightforward.

A "full house", presumably 5 cards consisting of a three-of-a-kind plus a pair. Presumably this is a single-deck game. Presumably this is a standard deck (Ace,2,3,4,5,6,7,8,9,Jack,Queen,King) with suits (Spades,Clubs,Hearts,Diamonds). (Abbreviated below as (A23456789JQK) and (SCHD).)

You mention 3700 combinations. Presumably you therefore consider the hands (2S,2C,2H,3H,3D) and (3D,3H,2H,2C,2S) as being equivalent rather than distinct. (That's actually quite an important point, as touched upon by Sean & Loadmaster in their comments. There are 311,875,200 (52*51*50*49*48) possible 5-card drawings. However, only 2,598,960 of those hands are distinct!)

We have (13 * 4) possible three-of-a-kinds. E.g. For each rank card (such as 3), we can have 4 three-of-a-kinds ({0S,3C,3H,3D}, {3S,0C,3H,3D}, {3S,3C,0H,3D}, {3S,3C,3H,0D}). (Perhaps you begin to notice a pattern: 0111 1011 1101 1110.)

Give our three-of-a-kind, and presuming it's a single deck and standard deck game, our pair must be one of the other 12 remaining card ranks. For each card rank, there are six possibilities for a pair. E.g. Given a card rank of 7, we could have ({7S,7C,0H,0D}, {7S,0C,7H,0D}, {7S,0C,0H,7D}, {0S,7C,7H,0D}, {0S,7C,0H,7D}, {0S,0C,7H,7D}). (Again, perhaps you notice the pattern: 1100 1010 1001, 0110 0101, 0011.)

This gives us 13 * 4 * 12 * 6 = 3744 combinations.

From here it is a simple matter of looping to print them out.

I suggest you consider more descriptive variable names. While there are places and times to use single character loop variables, this is not one of them. Well written code is nearly self-documenting, allowing documentation to concentrate on the more complex higher-level abstractions. The few extra characters you save will wind up costing you a fortune in debugging time. If desired, you can be lazy like me, learn emacs, use (require 'completion), (global-set-key "\C-\\" 'complete), type the first few characters and let emacs auto-complete for you.

I suggest you consider supporting, and perhaps private, methods. For instance, you might be able to do something like: (It's been a while since I last coded in Java.)

for ( suit = 0;  suit < 4 ;  ++ suit )
    private_printThreeOfAKind( card, suit!=0, suit!=1, suit!=2, suit!=3 )

Three of those (!=) would be true, one would be false.

With respect to printing pairs, you may want to investigate the continue statement. Ref: http://en.wikipedia.org/wiki/Java_syntax#continue_statement

E.g. This would allow you to skip over the case where the pair card is the same rank as the three-of-a-kind:

if ( threeOfAKindCard == pairCard )
    continue;

I suggest you build your software in parts. Trying to build a complete system rarely works, even for the experts. Build parts, test them, rinse, repeat. Yes, that means writing scaffolding code that you won't turn it. Perhaps even a testing subclass... But small steps are easier to get working. As you improve as a programmer, you'll be able to take larger steps...

花辞树 2024-10-03 13:48:27

又快又脏。还有很大的改进空间,但如果我的数学是正确的,这应该会给你葫芦的所有有效手牌组合。

公共类 PrintFullHouse {

enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
enum Rank {Ace, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

public static void main(String[] args) {

     String[] ranks = new String[14];
     String[] suits = new String[4];

    populateSuits(suits);
    populateCards(ranks);

    int numberOfCombos = 0;

    //Loop over all card values. This outer for loop is for the 3 of kind values. 3 Aces, 3 Twos, 3 Threes, etc
    for(int card = 0; card < ranks.length; card++)
    {
        String firstCard = ranks[card]; 

        for(int suit1 = 0; suit1 < suits.length; suit1++)
        {
            for(int suit2 = suit1+1; suit2 < suits.length; suit2++)
            {
                for(int suit3 = suit2+1; suit3 < suits.length; suit3++)
                {
                    //Loop over all card values that aren't equal to the firstCard.So we won't have 3 Aces and 2 Aces
                    for(int card2 = 0; card2 < ranks.length; card2++)
                    {
                        String secondCard = ranks[card2]; 

                        //Dont Compare the 3 of a Kind and 2 pair when they are the same rank. ie Aces and Aces
                        if(firstCard.compareTo(secondCard) != 0){
                            for(int othersuit1 = 0; othersuit1 < suits.length; othersuit1++)
                            {
                                for(int othersuit2 = othersuit1+1; othersuit2 < suits.length; othersuit2++)
                                {
                                    //Found a valid combo if 3 of a kind have different suits, 2 pair have different suits, and card1 is not equal to card2
                                    numberOfCombos++;
                                    System.out.println(numberOfCombos+". "+firstCard+" "+suits[suit1]+" "+firstCard+" "+suits[suit2]+" "+firstCard+" "+suits[suit3]+ " "
                                                       +secondCard+" "+suits[othersuit1]+" "+secondCard+" "+suits[othersuit2]);
                                }
                            }
                        }
                    }               
                }
            }
        }
    }
}

private static void populateSuits(String[] suits) {

    int index = 0;
    for(Suit suit: Suit.values())
    {
        suits[index++] = suit.toString();
    }
}

private static void populateCards(String[] ranks) {

    int index = 0;
    for(Rank rank: Rank.values())
    {
        if(index != ranks.length)
        ranks[index++] = rank.toString();
    }
}

}

Quick and dirty. There is plenty of room for improvement but if my math is correct this should give you all the valid hand combinations of a full house.

public class PrintFullHouse {

enum Suit {CLUBS, DIAMONDS, HEARTS, SPADES}
enum Rank {Ace, One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}

public static void main(String[] args) {

     String[] ranks = new String[14];
     String[] suits = new String[4];

    populateSuits(suits);
    populateCards(ranks);

    int numberOfCombos = 0;

    //Loop over all card values. This outer for loop is for the 3 of kind values. 3 Aces, 3 Twos, 3 Threes, etc
    for(int card = 0; card < ranks.length; card++)
    {
        String firstCard = ranks[card]; 

        for(int suit1 = 0; suit1 < suits.length; suit1++)
        {
            for(int suit2 = suit1+1; suit2 < suits.length; suit2++)
            {
                for(int suit3 = suit2+1; suit3 < suits.length; suit3++)
                {
                    //Loop over all card values that aren't equal to the firstCard.So we won't have 3 Aces and 2 Aces
                    for(int card2 = 0; card2 < ranks.length; card2++)
                    {
                        String secondCard = ranks[card2]; 

                        //Dont Compare the 3 of a Kind and 2 pair when they are the same rank. ie Aces and Aces
                        if(firstCard.compareTo(secondCard) != 0){
                            for(int othersuit1 = 0; othersuit1 < suits.length; othersuit1++)
                            {
                                for(int othersuit2 = othersuit1+1; othersuit2 < suits.length; othersuit2++)
                                {
                                    //Found a valid combo if 3 of a kind have different suits, 2 pair have different suits, and card1 is not equal to card2
                                    numberOfCombos++;
                                    System.out.println(numberOfCombos+". "+firstCard+" "+suits[suit1]+" "+firstCard+" "+suits[suit2]+" "+firstCard+" "+suits[suit3]+ " "
                                                       +secondCard+" "+suits[othersuit1]+" "+secondCard+" "+suits[othersuit2]);
                                }
                            }
                        }
                    }               
                }
            }
        }
    }
}

private static void populateSuits(String[] suits) {

    int index = 0;
    for(Suit suit: Suit.values())
    {
        suits[index++] = suit.toString();
    }
}

private static void populateCards(String[] ranks) {

    int index = 0;
    for(Rank rank: Rank.values())
    {
        if(index != ranks.length)
        ranks[index++] = rank.toString();
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文