关于扑克牌排序和洗牌问题

编写一个表示一副扑克牌的类(无大小王)。并有输出一副扑克牌的方法和洗牌的方法。输出一副扑克盘的方法结果如下图所示:

关于扑克牌排序和洗牌问题_第1张图片

洗牌方法即随机生成一个扑克牌的排列。

牌面大小和花色可以存储在字符串数组中,如下所示:

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    



你可能感兴趣的:(代码)