Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Deck
- {
- // Heavily inspired by the code used in the solution to quiz 1
- private Card[] deck; // Array of 52 cards representing the deck
- private int top;
- private int cardsUsed; // Number of cards used. Required to find remainder of cards in the deck
- public Deck() // Constructor to create a new (un-shuffled) deck of cards
- {
- top = 52; // Represents the integer index value for the top of the deck
- deck = new Card[52];
- int index = 0;
- for (int suit = 0; suit <= 3; suit++)
- {
- for (int rank = 1; rank <= 13; rank++)
- {
- deck[index] = new Card(rank, suit);
- index++;
- }
- }
- cardsUsed = 0;
- }
- // Method for shuffling the full deck of cards
- // Shuffling a non-full deck may result in duplicating cards
- public void shuffle()
- {
- for (int i = 51; i > 0; i--)
- {
- int rand = (int) (Math.random()*(i+1)); // Generates a random integer from 52 to 1
- Card temp = deck[i];
- deck[i] = deck[rand];
- deck[rand] = temp;
- }
- // cardsUsed = 0;
- }
- public void displayDeck() // Displays all cards in the deck
- {
- for (int i =0; i < top; i++)
- {
- System.out.println(deck[i]);
- }
- }
- public Card deal() // Deals the top card from the deck
- {
- if (top <= 0)
- {
- return null;
- }
- cardsUsed++;
- return (deck[--top]);
- }
- public int remainder() // Returns number of cards left in the deck
- {
- return 52 - cardsUsed;
- }
- }
Add Comment
Please, Sign In to add comment