2

我正在尝试找到一种将牌“发”到 4 只不同手牌的好方法。

         System.out.println("Deal to 4 Hands: "); 

         Hand hand1 = new Hand();
         Hand hand2 = new Hand();
         Hand hand3 = new Hand();
         Hand hand4 = new Hand();

         hand1.addSingleCard(Deck.deal());
         hand2.addSingleCard(Deck.deal());             
         hand3.addSingleCard(Deck.deal());
         hand4.addSingleCard(Deck.deal());

         hand1.addSingleCard(Deck.deal());
         hand2.addSingleCard(Deck.deal());            
         hand3.addSingleCard(Deck.deal());
         hand4.addSingleCard(Deck.deal());

         System.out.println("Cards left in deck: " + Deck.size());

         System.out.println("Player 1's Hand: \n" + hand1.getHand());
         System.out.println("Player 2's Hand: \n" + hand2.getHand());
         System.out.println("Player 3's Hand: \n" + hand3.getHand());
         System.out.println("Player 4's Hand: \n" + hand4.getHand());

有没有更简单的方法来对付双手?例如使用 For-Each 循环?

我试过这个:但它不起作用。我还没有真正使用过这种类型的循环非常必须......

         for(Hand card : hand1){
            System.out.println("Player 1's Hand: \n" + hand1);
         }

顺便说一句,这会将 2 张牌发给 4 个不同的手,然后打印每手。

4

8 回答 8

4

创建一个数组!

Hand[] hands = new Hand[4];

// create Hand object within the array and deal a single card to each hand
for(int i = 0; i < hands.length; i++) {
    hands[i] = new Hand();
    hands[i].addSingleCard(Deck.deal());
}

// deal an additional card to each hand (use a nested loop for dealing additional cards, if necessary)
for(int i = 0; i < hands.length; i++) {
    hands[i].addSingleCard(Deck.deal());
}

System.out.println("Cards left in deck: " + Deck.size());

for(int i = 0; i < hands.length; i++) {
    System.out.printf("Player %d's hand: \n %s\n", i+1, hands[i]);
}

该数组包含 4 个单独的 Hand 对象,并允许您通过遍历数组对所有手执行操作。

如果每只手有四个不同的命名变量(hand1、hand2 等),那么如果不在操作中显式使用变量名称(hand1.dealSingleCard() 等),就没有简单的方法可以对它们所代表的对象执行操作。

循环数组是要走的路:)

于 2012-11-14T23:02:40.340 回答
3

尝试这个:

 System.out.println("Deal to 4 Hands: "); 

 Hand[] hands=new Hand[4];
 for(int i=0;i<hands.length;i++)
      hands[i]=new Hand();
 for(Hand hand:hands)
      hand.addSingleCard(Deck.deal());
 for(Hand hand:hands)
      hand.addSingleCard(Deck.deal());
 System.out.println("Cards left in deck: " + Deck.size());
 for(int i=0;i<hands.length;i++)
      System.out.println("Player "+i+"'s Hand: \n" + hands[i].getHand());
于 2012-11-14T23:03:52.933 回答
1

您的 for 循环不起作用,因为您将 Hand 对象之一作为要迭代的项目。我不是 100% 熟悉这种 for 循环风格的所有应用程序,但根据我的经验,它用于在对象列表中移动。所以你想做这样的事情:

     System.out.println("Deal to 4 Hands: "); 

     Hand hand1 = new Hand();
     Hand hand2 = new Hand();
     Hand hand3 = new Hand();
     Hand hand4 = new Hand();

     List<Hand> handList = new ArrayList<Hand>();
     handList.add(hand1);
     handList.add(hand2);
     handList.add(hand3);
     handList.add(hand4);

     for (Hand currHand : handList) {
         currHand.addSingleCard(Deck.deal());
         currHand.addSingleCard(Deck.deal());
     }

我意识到我没有使用 for 循环来添加到列表中,但我正在工作,复制粘贴比从头开始编写更容易。

于 2012-11-14T23:10:56.870 回答
0

怎么样:

for(int j=0; j<MAX_NUMBER_OF_DEALS_PER_PLAYER;j++)
{
  for(int i=0; i<MAX_HAND_SIZE;i++)
  {
    handsArray[i].addSingleCard(Deck.deal());
  }
}

?

然后,您可以将交易数量设置为 2,例如,以复制您的示例。手的大小将取决于您要玩的游戏类型。

当然,您需要先初始化数组,例如:

Hand[] handsArray = new Hand[MAX_NUMBER_OF_PLAYERS];
for(int i=0;i<MAX_NUMBER_OF_PLAYERS;i++)
{
  handsArray[i] = new Hand();

}

这是你要找的吗?

如果您想使用 foreach 循环,您当然可以执行您发布的操作:

for(Hand card : hand1){
  System.out.println("Player 1's Hand: \n" + card); //notice it needs to be card here, NOT hand1
}

除了注意你需要在这里将hand1 更改为card,以打印手中的每张卡片。否则,您将为每张卡片打印相同的值(hand1),因此如果您有 3 张卡片,它将打印 hand1 的值 3 次。

于 2012-11-14T23:07:36.410 回答
0

将您的手存储在一个数组(或一个列表)中,并使用两个嵌套循环:

for (int i = 0; i < 2; i++) {
    for (Hand hand : hands) {
        hand.addSingleCard(Deck.deal());
    }
}
于 2012-11-14T23:03:11.027 回答
0

您只能在实现 Iterable 的类上使用 ForEach 循环

于 2012-11-14T23:04:52.287 回答
0

像这样的东西可以工作(虽然未经测试):

Hand hands[] = new Hand[] {hand1, hand2, hand3, hand4};
for (int i=0; i<2; i++) {               // deal 2 cards
    for (Hand currentHand: hands) {     // to each hand
         currentHand.addSingleCard(Deck.deal());
    }
 }

for (int i=0; i<hands.length; i++)  {
     System.out.println("Player " + i + "'s Hand: \n" + hands[i].getHand());
}
于 2012-11-14T23:06:32.207 回答
0

要使用for-each 循环,您需要某种类型的Iterable,可以是Collection(即List),也可以是数组。在这种情况下,您的代码可以大大简化:

private static final NUM_HANDS = 4;
System.out.println("Deal to 4 Hands: "); 
List<Hand> hands = new ArrayList<Hand>();
for(int i=0; i<NUM_HANDS) {
    hands.add(new Hand());
    hand.addSingleCard(Deck.deal());
}
int handNum = 1;
for (Hand hand: hands) {
    hand.addSingleCard(Deck.deal());
    System.out.println("Player " + handNum + "'s Hand: \n" + hand.getHand());
    handNum++;
}
System.out.println("Cards left in deck: " + Deck.size());
于 2012-11-14T23:14:23.303 回答