fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main {
  5. public static void main(String[] args) {
  6. Deck deck = new Deck();
  7. for (int i = 0 ; i != 52 ; i++) {
  8. Card playerCard = deck.takeRandomCard();
  9. System.out.println("The card is: " + playerCard.toString());
  10. }
  11. }
  12. }
  13.  
  14. class Deck {
  15. private static final int HIGH_SUIT=3;
  16. private static final int HIGH_VALUE=12;
  17. private boolean[] taken = new boolean[52];
  18. int cardCount = 52;
  19. Card[] fullDeck = new Card[52];
  20.  
  21. public Deck() {
  22. int i = 0;
  23. for(int s = 0; s <= HIGH_SUIT; s++) {
  24. //for loop to determine the suit
  25. for (int v = 0; v <= HIGH_VALUE; v++) {
  26. //construct all 52 cards and print them out
  27. fullDeck[i++] = new Card(s, v);
  28. }
  29. }
  30. }
  31.  
  32. public Card takeRandomCard() {
  33. if (cardCount == 0) return null;
  34. int r;
  35. do {
  36. r = (int)(Math.random() * 52);
  37. } while (taken[r]);
  38. taken[r] = true;
  39. cardCount--;
  40. return fullDeck[r];
  41. }
  42. }
  43.  
  44. class Card {
  45. private int cardValue;
  46. private String cardSuit;
  47. private String stringValue;
  48. static final String[] SUIT = {"Clubs", "Diamonds", "Hearts", "Spades"};
  49. static final String[] VALUE = {"Ace","Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
  50.  
  51.  
  52. public Card (int cardS, int cardV) {
  53. cardSuit = SUIT[cardS];
  54. stringValue = VALUE[cardV];
  55. }
  56.  
  57. public String getCardSuit() {
  58. return cardSuit;
  59. }
  60.  
  61. public String getValue() {
  62. return stringValue;
  63. }
  64.  
  65. public String toString() {
  66. return String.format("%s of %s", stringValue, cardSuit);
  67. }
  68. }
  69.  
Success #stdin #stdout 0.05s 245952KB
stdin
Standard input is empty
stdout
The card is: Four of Spades
The card is: Five of Spades
The card is: Six of Hearts
The card is: Seven of Spades
The card is: Two of Diamonds
The card is: Three of Diamonds
The card is: Ace of Spades
The card is: Five of Hearts
The card is: Three of Clubs
The card is: Nine of Clubs
The card is: Two of Clubs
The card is: King of Spades
The card is: Nine of Hearts
The card is: Eight of Clubs
The card is: Queen of Clubs
The card is: Ten of Diamonds
The card is: King of Clubs
The card is: Seven of Hearts
The card is: Eight of Spades
The card is: Ace of Hearts
The card is: Nine of Spades
The card is: Two of Spades
The card is: Seven of Diamonds
The card is: King of Hearts
The card is: Eight of Diamonds
The card is: Ace of Diamonds
The card is: Nine of Diamonds
The card is: Three of Hearts
The card is: Five of Clubs
The card is: Four of Clubs
The card is: King of Diamonds
The card is: Ten of Clubs
The card is: Five of Diamonds
The card is: Ten of Spades
The card is: Six of Diamonds
The card is: Jack of Diamonds
The card is: Four of Diamonds
The card is: Seven of Clubs
The card is: Jack of Spades
The card is: Six of Spades
The card is: Queen of Hearts
The card is: Eight of Hearts
The card is: Jack of Clubs
The card is: Four of Hearts
The card is: Three of Spades
The card is: Queen of Diamonds
The card is: Two of Hearts
The card is: Ten of Hearts
The card is: Jack of Hearts
The card is: Ace of Clubs
The card is: Queen of Spades
The card is: Six of Clubs