2

我想知道是否有人知道如何在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 ++;


     }
    }
   }
  }

 }
}
4

4 回答 4

2

在您继续之前,请在您的代码中添加一些注释。它将帮助您了解正在发生的事情,尤其是当您使用单字符变量名嵌套 4 深的循环时。

接下来,分解问题:满屋子的真正独特之处是什么?两个点数都是唯一的,但不能相同。3 of a kind 有 3 种不同的花色(或只是缺少一个),并且这对有 2 种不同的花色。

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

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

于 2010-10-05T23:46:44.377 回答
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.

于 2010-10-06T01:06:05.227 回答
1

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

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


第一步: 定义问题。一旦明确定义,答案就会变得更加直截了当。

一张“满屋”,大概是 5 张牌,由 3 种加一对组成。想必这是一个单牌游戏。大概这是带有花色(黑桃,梅花,红心,钻石)的标准牌组(Ace,2,3,4,5,6,7,8,9,Jack,Queen,King)。(以下缩写为 (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 )

其中三个 (!=) 为真,一个为假。

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

例如,这将允许您跳过配对卡与三类相同等级的情况:

if ( threeOfAKindCard == pairCard )
    continue;

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

于 2010-10-10T17:17:50.473 回答
0

又快又脏。有很大的改进空间,但如果我的数学是正确的,这应该给你一个满堂彩的所有有效手牌组合。

公共类 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();
    }
}

}

于 2010-10-07T21:35:16.763 回答