關於撲克牌排序和洗牌問題

編寫一個表示一副撲克牌的類(無大小王)。並有輸出一副撲克牌的方法和洗牌的方法。輸出一副撲克盤的方法結果如下圖所示:

洗牌方法即隨機生成一個撲克牌的排列。

牌面大小和花色可以存儲在字符串數組中,如下所示:

String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six","Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; //牌面大小

String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; //花色


package cards;
import java.util.Random;

public class DeckOfCards {
	private Card deck[];
    private int currentCard;
    private final int NUMBER_OF_CARDS = 52;
    private Random randomNumbers;

    public DeckOfCards() {
        String faces[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
            "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
        String suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        deck = new Card[NUMBER_OF_CARDS]; // create array of Card objects
        currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
        randomNumbers = new Random(); // create random number generator
        // populate deck with Card objects
        for (int count = 0; count < deck.length; count++) {
            deck[count] = new Card(faces[count % 13], suits[count / 13]);
        }
    }

    public void shuffle() {
        // after shuffling, dealing should start at deck[ 0 ] again
        currentCard = 0; // reinitialize currentCard

        // for each Card, pick another random Card and swap them
        for (int first = 0; first < deck.length; first++) {
            // select a random number between 0 and 51
            int second = randomNumbers.nextInt(NUMBER_OF_CARDS);

            // swap current Card with randomly selected Card
            Card temp = deck[first];
            deck[first] = deck[second];
            deck[second] = temp;
        } // end for
    } //

    public Card dealCard() {
// determine whether Cards remain to be dealt
        if (currentCard < deck.length) {
            return deck[currentCard++]; // return current Card in array
        } else {
            return null; // return null to indicate that all Cards were dealt
        }
    }

}



package cards;

public class Card {
	 private String face; // face of card ("Ace", "Deuce", ...)
	    private String suit; // suit of card ("Hearts", "Diamonds", ...)

	    public Card(String cardFace, String cardSuit) {
	        face = cardFace; // initialize face of card
	        suit = cardSuit; // initialize suit of card
	    } // end two-argument Card constructor

	    public String toString() {
	        return face + " of " + suit;
	    }

}

package cards;

public class DeckOfCardsTest {
	 public static void main( String args[] )
	    {
	        DeckOfCards myDeckOfCards = new DeckOfCards();
	        
	        for ( int i = 0; i < 13; i++ )
	        {
	            // deal and print 4 Cards
	            System.out.printf( "%-20s%-20s%-20s%-20s\n",
	                myDeckOfCards.dealCard(), myDeckOfCards.dealCard(),
	                myDeckOfCards.dealCard(), myDeckOfCards.dealCard() );
	        }
	        
	        myDeckOfCards.shuffle(); // place Cards in random order
	        
	        // print all 52 Cards in the order in which they are dealt
	        for ( int i = 0; i < 13; i++ )
	        {
	            // deal and print 4 Cards
	            System.out.printf( "%-20s%-20s%-20s%-20s\n",
	                myDeckOfCards.dealCard(), myDeckOfCards.dealCard(),
	                myDeckOfCards.dealCard(), myDeckOfCards.dealCard() );
	        } // end for
	    } // end main

}


運行結果:

Ace of Hearts       Deuce of Hearts     Three of Hearts     Four of Hearts      
Five of Hearts      Six of Hearts       Seven of Hearts     Eight of Hearts     
Nine of Hearts      Ten of Hearts       Jack of Hearts      Queen of Hearts     
King of Hearts      Ace of Diamonds     Deuce of Diamonds   Three of Diamonds   
Four of Diamonds    Five of Diamonds    Six of Diamonds     Seven of Diamonds   
Eight of Diamonds   Nine of Diamonds    Ten of Diamonds     Jack of Diamonds    
Queen of Diamonds   King of Diamonds    Ace of Clubs        Deuce of Clubs      
Three of Clubs      Four of Clubs       Five of Clubs       Six of Clubs        
Seven of Clubs      Eight of Clubs      Nine of Clubs       Ten of Clubs        
Jack of Clubs       Queen of Clubs      King of Clubs       Ace of Spades       
Deuce of Spades     Three of Spades     Four of Spades      Five of Spades      
Six of Spades       Seven of Spades     Eight of Spades     Nine of Spades      
Ten of Spades       Jack of Spades      Queen of Spades     King of Spades     
Six of Hearts       Six of Clubs        Nine of Hearts      Nine of Spades      
Five of Clubs       Deuce of Clubs      Queen of Diamonds   Jack of Hearts      
Three of Spades     Seven of Diamonds   Eight of Diamonds   King of Hearts      
Jack of Spades      King of Spades      Eight of Hearts     Nine of Diamonds    
Deuce of Spades     Ace of Clubs        Four of Hearts      Five of Hearts      
King of Clubs       Seven of Hearts     Three of Diamonds   Deuce of Hearts     
Eight of Spades     Five of Diamonds    Ten of Hearts       Six of Diamonds     
Four of Spades      Ace of Diamonds     Three of Clubs      Four of Clubs       
Queen of Clubs      Deuce of Diamonds   Queen of Spades     Three of Hearts     
Queen of Hearts     Ten of Diamonds     Ace of Spades       Ten of Clubs        
Jack of Diamonds    Six of Spades       Jack of Clubs       Ten of Spades       
Four of Diamonds    Seven of Clubs      Ace of Hearts       Eight of Clubs      
Nine of Clubs       Seven of Spades     Five of Spades      King of Diamonds    



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章