不显示卡片

时间:2016-11-10 18:32:31

标签: java enums instance encapsulation

我正在创建一个卡片的实例卡片,它应该在构建时显示所有52张卡片,然后它应该随机播放卡片并进行处理5.此后菜单应该提示另外5张卡片被处理,或者甲板重新洗牌,将所有提取的卡片添加回来,或退出应用程序。但是,当我运行应用程序时,它只显示所有52张卡和每张卡的空值。在与我的导师交谈之前,该应用程序工作并几乎满足所有规范,而我现在所拥有的是我在谈话期间/之后得到的地方。我不确定我做错了什么,可以真正使用帮助。为了澄清,我将发布要求,然后是代码。我很感激帮助。

At startup, it constructs a Deck of Cards
52 distinct cards running from Ace - King, of suit Heart, Club, Diamond, or Spade
Shuffle the cards!!
For proof, print out all 52 cards to the console in a useful, readable way.
Then, deal the top five cards to the console (meaning print them to the console)
After all of this, allow the user choose between dealing the next five cards, reshuffling the deck, or quitting the application. 
If the user chooses to deal the next 5 cards, do so based on which cards have not yet been dealt. DO NOT RESHUFFLE.
If the user chooses to reshuffle, simply repeat the process of shuffling, printing the deck, and dealing the top five cards.
If the user chooses to quit the application, simply end the app.

我正在练习封装的代码,所以我将按照eclipse中的方式发布每个类。

驱动程序

public class Driver {

    public static void main(String[] args) throws IOException {

        DeckRun.run();

        }

    }

DeckRun

    public class DeckRun {

    static Card[] d1 = new Card[52];

    public static void run() throws IOException {

        printDeck();
        System.out.println("");
        Deal.dealCards(d1);
        System.out.println("");
        menu();

    }

    public static void printDeck() {

    for (Card c : d1) {
        System.out.println(c);
    }

    }

    public static void menu() throws IOException{

        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));

        int menu = 0;

        do {

        System.out.println("Press 1 to be dealt 5 random cards.");
        System.out.println("Press 2 to shuffle all the cards back into the deck.");
        System.out.println("Press 3 to quit the application.");

        String input = readRacer.readLine();
        menu = Integer.parseInt(input);

        switch (menu) {

        case 1: Deal.dealCards(d1);
            break;
        case 2: System.out.println("The deck has been shuffled.");
                Deck[] d1 = new Deck[52];
            break;
        case 3: System.out.println("I'm not bad, I'm just drawn that way.");
            break;
        }
        } while (menu != 3);
    }

}

public class Card {

    private Rank rank; // Variable to assign a card its rank.
    private Suit suit; // Variable to assign a card its suit.

    public Card (Rank rank, Suit suit) { // Constructor to build a card.
        this.rank = rank;
        this.suit = suit;
    }

    public Rank getRank() { // Retrieves the card's rank from the enum Rank.
        return rank;
    }

    public Suit getSuit() { // Retrieves the card's suit from the enum Suit.
        return suit;
    }

    @Override
    public String toString() { //
        return rank + " OF " + suit;
    }

}

甲板

public class Deck {

    private Card[] cards;

    public Deck() {

        int numberOfRanks = 13;
        int numberOfSuits = 4;
        int numberOfCards = numberOfRanks * numberOfSuits;

        Rank[] rank = Rank.values();
        Suit[] suit = Suit.values();

        cards = new Card[numberOfCards];

        for (int i = 0; i < numberOfRanks; i++) {
            for (int j = 0; j < numberOfSuits; j++) {

                cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
            }
        }
    }

    public void shuffleCards() {

        Random rand = new Random();

        for (int c = rand.nextInt(6) + 5; c > 0; c--) {
            for (int i = cards.length - 1; i > 0; i--) {

                int index = rand.nextInt(i + 1);
                Card card = cards[index];
                cards[index] = cards[i];
                cards[i] = card;
            }
        }
    }
}

交易

public class Deal {

    private static int counter = 0;

    public static void dealCards(Card[] d1) {

        for (int i = 0; i < 5; i++) {
            counter++;
            System.out.println(d1[counter]);
            if (counter == 50) {
                System.out.println("Almost all cards have been used, please reshuffle.");
            }
        }
    }
}

排名(枚举)

public enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING, }

诉讼(Enum)

public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES, }

不确定问题出在哪里,但我今天改变的是在意识到我需要一个甲板的实例之后,我的教授帮助我明白了,而不是甲板上甲板卡的方法,我需要构造函数。我相信我走的是正确的道路,但我只是完全被难倒了。任何帮助都表示赞赏,如果你能解释我的错误,那么我可能会更加学习和进步。

4 个答案:

答案 0 :(得分:0)

main您正在致电DeckRun.run(),让我们来看看您对此方法的定义。它调用printDeck,它遍历一组从未填充的卡片(d1)。这就是为什么你得到一堆NULL s。

答案 1 :(得分:0)

您的DeckRun应该有Deck的实例,而不是卡片数组

 public class DeckRun {
    private Deck deck = new Deck();
    public static void run() throws IOException {
        printDeck();
        System.out.println("");
        Deal.dealCards(deck.getCards());
        System.out.println("");
        menu();
    }
    public static void printDeck() {
       for (Card c : deck.getCards()) {
           System.out.println(c);
       }
    }

    public static void menu() throws IOException{
        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
        int menu = 0;
        do {
           System.out.println("Press 1 to be dealt 5 random cards.");
           System.out.println("Press 2 to shuffle all the cards back into the deck.");
           System.out.println("Press 3 to quit the application.");
           String input = readRacer.readLine();
           menu = Integer.parseInt(input);
           switch (menu) {
             case 1: 
                Deal.dealCards(deck.getCards());
                break;
             case 2: 
                System.out.println("The deck has been shuffled.");
                deck.shuffleCards();
                break;
             case 3: 
                System.out.println("I'm not bad, I'm just drawn that way.");
                break;
           }
        } while (menu != 3);
    }
}

您还需要在Deck

中添加卡片的getter

答案 2 :(得分:0)

所以你的问题就在这个循环中了

      for (int i = 0; i < numberOfRanks; i++) {
        for (int j = 0; j < numberOfSuits; j++) {

            cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
        }
    }

J * numOfRanks + i基本上是说0 * 4 = 0 + 0 = 0

等等,造成问题。为了使这更简单,我会推荐一个2D数组,为套装制作4列,为等级制作13行,然后类似的循环将完美地工作。请告诉我这是否有帮助!

答案 3 :(得分:0)

好的伙计们。答案很简单。我删除了交易类,并将方法移到了甲板类。我将计数器初始化为私有的类级别int,它修复了重复问题。我将在下面发布完成的代码,以便您可以看到。

DeckRun Class

public class DeckRun {

    static Deck d1 = new Deck();

    public static void run() throws IOException {

        printDeck();
        System.out.println("");
        d1.dealCards();
        System.out.println("");
        menu();

    }

    public static void printDeck() {

        System.out.println(d1.toString());
    }


    public static void menu() throws IOException{

        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));

        int menu = 0;

        do {

        System.out.println("Press 1 to be dealt 5 random cards.");
        System.out.println("Press 2 to shuffle all the cards back into the deck.");
        System.out.println("Press 3 to quit the application.");

        String input = readRacer.readLine();
        menu = Integer.parseInt(input);

        switch (menu) {

        case 1: d1.dealCards();
        System.out.println("");
            break;
        case 2: System.out.println("The deck has been shuffled.");
                    d1.shuffleCards();
                    System.out.println("");
            break;
        case 3: System.out.println("I'm not bad, I'm just drawn that way.");
            break;
        }
        } while (menu != 3);
    }

}

甲板等级

public class Deck {

    private Card[] cards;

    public Deck() {

        int numberOfRanks = 13;
        int numberOfSuits = 4;
        int numberOfCards = numberOfRanks * numberOfSuits;

        Rank[] rank = Rank.values();
        Suit[] suit = Suit.values();

        cards = new Card[numberOfCards];

        for (int i = 0; i < numberOfRanks; i++) {
            for (int j = 0; j < numberOfSuits; j++) {

                cards[i * numberOfSuits + j] = new Card(rank[i], suit[j]);
            }
        }
    }

    public void shuffleCards() {

        Random rand = new Random();
        counter = 0;

        for (int c = rand.nextInt(6) + 5; c > 0; c--) {
            for (int i = cards.length - 1; i > 0; i--) {

                int index = rand.nextInt(i + 1);
                Card card = cards[index];
                cards[index] = cards[i];
                cards[i] = card;
            }
        }
    }

    private int counter = 0;

    public void dealCards() {

        try{
        for (int i = 0; i < 5; i++) {
            counter++;
            System.out.println(cards[counter]);
            if (counter == 50) {
                System.out.println("Almost all cards have been used, please reshuffle.");
                // Either return 1 card or an array of 5 cards.
            }
        } 
        } catch (ArrayIndexOutOfBoundsException aioobe){
            System.out.println("Caught an ArrayIndexOutOfBoundsException. Reshuffling deck.");
            shuffleCards();
        }
    }

    @Override
    public String toString() {

        String deckOfCards = "";

        for (Card c : cards) {

            deckOfCards += c.toString() + "\n";

        }

        return deckOfCards;

    }
}

这就是我必须解决的问题,但我很感激帮助。谢谢大家。