Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.util.Random;
- /**
- *
- * @author Terry
- */
- public class DeckOfCard {
- public static final int DECK_SIZE = 52; //Number of cards in a deck
- private final Random generator = new Random(); //Random number generator
- private Card[] deck; //Deck of cards
- private int cardsLeft; //Number of cards left in deck (not dealt)
- DeckOfCard() {
- deck = new Card[DECK_SIZE];
- cardsLeft = DECK_SIZE;
- int currentCard = 0;
- for (int suit = 1; suit <= Card.NUM_SUITS; suit++) {
- for (int face = 1; face <= Card.NUM_FACES; face++) {
- deck[currentCard++] = new Card(face, suit);
- }
- }
- }
- //Fisher-Yates shuffle algorithm
- public void shuffle() {
- Card tempCard;
- int j;
- for (int i = deck.length - 1; i >= 1; i--) {
- j = generator.nextInt(i);
- tempCard = deck[j];
- deck[j] = deck[i];
- deck[i] = tempCard;
- }
- }
- public Card deal() {
- return deck[--cardsLeft];
- }
- public int cardsLeft() {
- return cardsLeft;
- }
- /**
- * @param args Command line arguments aren't supported in this project.
- */
- public static void main(String[] args) {
- DeckOfCard myDeck = new DeckOfCard();
- myDeck.shuffle();
- for (int i = 0; i < DeckOfCard.DECK_SIZE; i++) {
- System.out.println(myDeck.deal().toString());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment