清空(a)Java中的卡片组

时间:2014-07-04 01:16:10

标签: java

我正在寻找一些有关如何在我创建的套牌用完纸牌时知道我的纸牌游戏的见解。

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CardDealer {


    public static void main(String[] args) {

        System.out.println("Welcome to Shuffle!");

        Scanner console = new Scanner(System.in);

        Map<Integer, Deck> decks = new HashMap<Integer, Deck>();

        int deckNum = getNumberOfDecks(console);
        int cardNum = getNumberOfCards(console);

        String readString = console.nextLine();
        while(readString!=null)
        {
            System.out.println(readString);
            if(readString.equals(""))
                for(int i = 0; i < deckNum; i++){
                    decks.put(i, new Deck(cardNum));
                }

                for(int key : decks.keySet()){
                    System.out.println("Deck # "+ key + " " +decks.get(key).toString());
                        if(key >= 52)
                            key = key + cardNum;
                        else if(key == 51)
                            System.out.println("All GONE!");
                }
            if(console.hasNextLine())
                readString = console.nextLine();
            else
                readString = null;
        }
    }   


    /**
     * Get number of decks 
     * @param console
     * @return
     */
    public static int getNumberOfDecks(Scanner console){
        int tempNumDecks = 0;
        boolean isOk = false;

        System.out.println("How many decks would you like to use?");

        do{
            try{
                String userInput = console.nextLine();
                tempNumDecks = Integer.parseInt(userInput);
                } catch(Exception e){
                    System.out.println ("Invalid input. Enter a number > 0");
                    System.out.println("Enter an integer greater than 0:");
            }
            isOk = true;
        }while(isOk == false);
        return tempNumDecks;
    }

    /**
     * Get number of cards
     * @param console
     * @return
     */
    public static int getNumberOfCards(Scanner console){
        int tempNumCards = 0;
        boolean isOk = false;

        System.out.println("How many cards would you like to deal?");

        do{
            try{
                String userInput = console.nextLine();
                tempNumCards = Integer.parseInt(userInput);

                if(tempNumCards <= 0) {
                    System.out.println ("Invalid input. Enter a number > 0");
                } else{
                    isOk = true;
                }//end else
            }//end try
            catch(Exception e){
                System.out.println("Enter an integer greater than 0:");
            }//end catch
        }while(isOk == false);
        return tempNumCards;
    }//end getNumberOfCards()
    } 

甲板课程:

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Random;


public class Deck {
    /** Cards instance **/
    private List<Card> cards;

    /** Random generator **/
    private Random r;

       public Deck(int numberOfCards)
    {
        cards = new ArrayList<Card>(numberOfCards);

        int tempCardIndex = 0;

        for (int a=0; a<=3; a++)
        {
            for (int b=0; b<=12; b++)
             {
               cards.add( new Card(a,b) );
             }
        }

        /** Shuffle this deck **/
        shuffle(cards);

        /** Slice the deck by our number of cards **/
        cards = cards.subList(0, numberOfCards);
    }

    /**
     *  Shuffle a list 
     * @param list
     */
    public void shuffle(List<Card> list) {
        if (r == null) {
            r = new Random();
        }
        shuffle(list, r);
    }

/**
 * Shuffle a list with Random rnd
 * @param list
 * @param rnd
 */
    public void shuffle(List<Card> list, Random rnd) {
        int size = list.size();

            Object[] arr = list.toArray();

            // Shuffle array
            for (int i=size; i>1; i--)
                swap(arr, i-1, rnd.nextInt(i));

            // Dump array back into list
            ListIterator<Card> it = list.listIterator();
            for (int i=0; i<arr.length; i++) {
                it.next();
                it.set((Card) arr[i]);
        }
    }

    /**
     * Swap elements in List
     * @param list
     * @param i
     * @param j
     */
    public void swap(List<Card> list, int i, int j) {
        final List l = list;
        l.set(i, l.set(j, l.get(i)));
    }

    /**
     * Swap Elements in Array
     * @param arr
     * @param i
     * @param j
     */
    private static void swap(Object[] arr, int i, int j) {
        Object tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    /**
     * Draw a card 
     * @return
     */
    public Card drawFromDeck()
    {      
        return cards.remove(0);
    }

    /** Get total Cards 
     * 
     * @return cards size
     */
    public int getTotalCards()
    {
        return cards.size();
    }

    /**
     * to String override
     */
    public String toString(){
        return cards.toString();
    }

}

卡类:

public class Card
{
   private int rank, suit;

   private final String[] suits = { "hearts", "spades", "diamonds", "clubs" };
   private final String[] ranks  = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };


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

   public @Override String toString()
   {
         return ranks[rank] + " of " + suits[suit];
   }

   public int getRank() {
        return rank;
   }

   public int getSuit() {
       return suit;
   }

}

因此,这会在用户指定数量的套牌中打印用户指定数量的卡片的哈希图。

像这样:

Deck # 0 [Jack of hearts, 3 of spades, 7 of spades, 6 of hearts]
Deck # 1 [6 of hearts, 9 of clubs, King of spades, 2 of clubs]
Deck # 2 [5 of spades, King of clubs, Queen of clubs, King of spades]
Deck # 3 [5 of spades, 6 of hearts, 9 of clubs, 5 of diamonds]

每次用户按Enter键时都会这样做。我的问题是,我希望它停止允许用户在每个牌组的所有牌都用完时从牌组中抽出更多的牌。这可能吗?一位同事告诉我,以某种方式使用布尔值来完成这项工作,但我很难过。

感谢帮助人员。

1 个答案:

答案 0 :(得分:1)

您可以在调用getTotalCards()之前调用检查卡片数量的方法,drawFromDeck() 。然后,如果它是0,你知道没有卡片。

请注意,有了这些问题,最好向我们展示您尝试解决问题的一部分,否则您只是在学习机会中欺骗自己,并阻止我们看到你的逻辑可能有什么问题。