我有两个类,分别是BlackJackBlackJackPaneBlackJack有一个名为dealCards()的方法,该方法通过从第一个Player到最后一个Card循环两次,为名为Player的类提供了一个Player就像在二十一点的真实游戏中,每位玩家一次发一张卡。

BlackJackPane是一个包含JButton和JLabel的JFrame。单击JButton deal时,会有一个.actionCommand("deal")和一个带有Lambda表达式的ActionListener,该表达式调用blackjack中的实例变量BlackJackPane来处理卡片blackjack.dealCards()ActionListener还可以确定从常规的52张卡组中取出哪种类型的卡,以显示Player所具有的正确Card

我希望在将Player传递给dealCards()后在BlackJack中等待Card方法,以便在JFrame中创建和显示JLabel,但是,我不知道如何跨此工作类。我愿意接受其他创造性的选择或有关如何改进代码的提示。

我试图将类封装起来,并将I / O限制在Player中。

这是我的课程BlackJackPane

public class BlackJack
{

   private final Deck deck;
   private final ArrayList<Player> player;
   private final int numOfPlayers;

   public BlackJack(int players)
   {
       this.numOfPlayers = players + 1; // +1 for dealer, dealer is always first in arrayList
       this.deck = new Deck(Deck.TYPE[0]);
       this.player = new ArrayList();
       for (int i = 0; i < players + 1; i++)
       {
           this.player.add(new Player((i == 0))); //i == 0 is to say true to Player constructor that it is the dealer or cpu
       }
   }

   public void DealCards()
   {
       deck.shuffle();

       for (int i = 0; i < 2; i++)
       {
           for (int j = 0; j < player.size(); j++)
           {
               player.get(j).getHand().add(deck.getCard(0));
               deck.remove(0);
           }
       }
   }
}


这是BlackJack

    public class BlackJackPane extends JFrame
    {

       private BlackJack blackjack;
       private JLabel pokerTable;
       private JButton hit, stay, split, deal, hiddenCard;

       public BlackJackPane(BlackJack blackjack) throws IOException
       {
          this.blackjack = blackjack;
          display();
       }

       private void display() throws IOException
       {


        deal.addActionListener((event) ->
        {
            if (event.getActionCommand().equals("deal"))
            {
                blackjack.DealCards();

                for (int i = 0; i < 2; i++)
                {
                    for (int j = 0; j < blackjack.getNumOfPlayers(); j++)
                    {
                        if (blackjack.getPlayer(j).isCpu() && i != 1) //One card dealer has is face down
                        {

                        } else
                        {
                            if (blackjack.getPlayer(j).getHand().get(i).getFace() == 0) // face = 2
                            {
                                if (blackjack.getPlayer(j).getHand().get(i).getSuit() == 0) //clubs
                                {
                                   //Code to make and display the JLabel within the JFrame
                                } else if (blackjack.getPlayer(j).getHand().get(i).getSuit() == 1) //diamonds
                                {

                                } else if (blackjack.getPlayer(j).getHand().get(i).getSuit() == 2) //hearts
                                {

                                } else //spades
                                {

                                }
                            } else if() //and so on for 52 cards
                        }
                    }
                }
             }
        });
    }
}

最佳答案

您可以重新定义dealCards方法,以在发出每张卡后进行一些回调。

public void dealCards(BiConsumer<Player, Card> onDealtCallback) {
    for (Player player : players) {
        Card dealt = this.getNextCardToDeal();
        onDealtCallback.accept(player, dealt);
    }
}


然后用类似这样的东西调用它:

public void callIt() {
    this.blackJack.dealCards(this::drawLabel);
}

public void drawLabel(Player player, Card card) {
    // TODO
}


我不能说这是最面向对象的方法,它不能很好地将状态和UI分开。但是它应该可以做您想要做的事情。

(注意:这不是代码审查的地方,因此,我拒绝就“如何改进代码”提供进一步建议的要求。)

07-26 01:51