2016-11-10 60 views
0

我正在創建一張應該在內置卡片時顯示所有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, } 

套裝(枚舉)

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

不能確定其中的問題,但我現在已經改變了,是實現我後需要一個甲板的實例,我的教授幫助我理解,而不是甲板上的一副紙牌的方法,我需要一個構造函數。我相信我正走在正確的道路上,但我完全陷入了困境。任何幫助表示讚賞,如果你能解釋我的錯誤,所以我可能會學習和進步更多的感謝。

回答

0

好吧,夥計們。答案很簡單。我刪除了交易類,並將該方法移至了甲板類。我將計數器初始化爲一個私有的類級別int,它修復了重複問題。我將在下面發佈完成的代碼,以便您看到。

DeckRun類

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; 

    } 
} 

這就是我不得不修復,但我很欣賞的幫助。感謝大家。

0

main你打電話給DeckRun.run(),讓我們來看看你對這種方法的定義。它調用printDeck,它遍歷一系列卡片(d1),該卡片永遠不會填充。這就是爲什麼你會得到一堆NULL s。

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); 
    } 
} 

你還需要在橋面添加getter查詢卡

+0

我會給這個去,但我不完全確定我明白你的意思。不過,我會檢查這一點,並盡我所能。 –

+0

因此,通過這個和其他一些調整我已經完成了,除了不重複規則之外,我現在可以完成所有工作。另外,我不知道如何處理隨機播放選項。我想我應該創建一個甲板的新實例,但是,然後我需要一種方法來取代每一個調用第一個實例的命令與第二個...我現在很迷茫和困惑.. –

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

等等,造成問題。爲了使這更簡單,我會推薦一個二維數組,爲西服制作4列,爲隊伍製作13行,然後類似的循環將完美地工作。請讓我知道這可不可以幫你!

+0

我是仍然在學習,我只編寫了大約6周,所以我不熟悉2D陣列是什麼,對不起。然而,在我將這個框架改爲數組的構造函數之前,它工作正常,所以我很困惑爲什麼它現在不起作用。 –