-1

请,任何帮助将不胜感激,我相信这应该是一个简单的问题。

我是一名大学生,对 Java 很陌生,正在尝试一些实验室课程。

要求是一个非常简单的扑克程序,它只需要: 按指定顺序生成一副 52 张牌(已经可以使用) 读取玩家数量和玩家姓名(已经可以使用) 向每 (2) 名玩家发 5 张牌和牌堆顶部的庄家(不确定是否有效)然后通过以下输出显示牌:

玩家人数:2 玩家 1:本垒打 玩家 2:Marge

Homer 有 AH 4H 7H 10H KH Marge 有 2H 5H 8H JH AD 庄家有 3H 6H 9H QH 2D


有几个类别 - 经销商,玩家,甲板,手,牌

现在我得到的输出是:

玩家人数:2 玩家 1:本垒打 玩家 2:Marge

荷马有 KH KH KH KH KH Marge 有 AD AD AD AD AD 经销商有 2D 2D 2D 2D 2D


Dealer类的相关代码:

public class Dealer {
    public static void main(String[] args)
    {   new Dealer();  }

    private Deck deck = new Deck();
    private Hand hand = new Hand();
    public ArrayList<Player> players = new ArrayList<Player>();

    ....

    private void deal()
    {
        for(int i = 0; i < 5; i ++) {
            for (Player player : players)
                player.add(next());
            add(next());  
        }
    }    //  deal five cards to each player and the dealer
         //  in the stated order

    private void add(Card card)
    {   hand.add(card);    }

    private Card next()
    {   return deck.next(); }

    private void show()
    {   
        for (Player player : players)
            System.out.println(player.getName() + " has " + player.showHand());
        System.out.println("Dealer has " + hand.toString());
    }
}

以下是Player类中的相关代码:

public class Player{
    private String name;
    private int number;
    private Hand hand = new Hand();
    ....
    public void add(Card card)
    {   hand.add(card); }

    public String showHand()
    {
        String show = "" + hand.toString();
        return show;
    }

以下是Hand类中的相关代码:

public class Hand {
    private Card[] cards = new Card[5];
    ....
    public void add(Card card)
    {
        for (int i = 0; i < cards.length; i++)
            cards[i] = new Card(card.valueReturn(), card.suitReturn());
    }

    public String toString()
    {
        String display = "";
        display = cards[0].toString() + cards[1].toString() + cards[2].toString() + cards[3].toString() + cards[4].toString();
        return display;
    }
}

以下是Deck类的相关代码:

public class Deck{
    private Card[] cards = new Card[52];
    private int i = 0, deck = 0;
    private char[] suits = {'H', 'D', 'C', 'S'};

    public Deck()
    {
        for (int suit = 0; suit < 4; suit++){
            for(int value = 1; value <= 13; value++){
                 cards[deck++] = new Card(value, suits[suit]);
            } 
        }        
    }  // create the cards in the stated order and add them to the deck

    //  This function works by side effect
    //  It uses the standard "next" pattern you will see next week
    public Card next()
    {   return cards[i++];    }
}

最后是Card类的相关代码:

public class Card{

    private int cardValue;
    private char suit;

    public Card(int cardValue, char suit)
    {   
        this.cardValue = cardValue;
        this.suit = suit;   
    }

    public String valueDisplay()
    {
        switch (cardValue)
        {
            case 1: return "A";
            case 11: return "J";
            case 12: return "Q";
            case 13: return "K";
            default: return "" + cardValue;
        }
    }

    public int valueReturn()
    {
        this.cardValue = cardValue;
        return cardValue;
    }

    public char suitReturn()
    {
        this.suit = suit;
        return suit;
    }

    public String toString()
    {
        return valueDisplay() + suit + " ";
    }
}
4

1 回答 1

2

Hand.add()

for (int i = 0; i < cards.length; i++)
   cards[i] = new Card(card.valueReturn(), card.suitReturn());
}

您正在创建五张具有完全相同值的新卡。

人们会期待类似的东西(丑陋但快速修复):

for (int i = 0; i < cards.length; i++)
   if (cards[i] == null) {
     cards[i] = card;
     return;
   } 
}

此外,对于这些情况,学习使用调试器并逐步观察系统状态非常有用。

于 2012-08-22T13:02:11.967 回答