Advertisement
Guest User

Deck.java

a guest
Jan 29th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. import java.util.ArrayList;
  2. public class Deck
  3. {
  4. private ArrayList<Card> deck;
  5.  
  6. /**
  7. * Fills a deck with the 52 unique cards
  8. * @param Sets the suit rank and value of the 52 cards
  9. */
  10. public Deck()
  11. {
  12. deck = new ArrayList<Card>();
  13. String suit = "";
  14. String rank = "";
  15. int value;
  16. for(int i = 1; i<=13;i++)
  17. {
  18. for(int j =1; j<=4;j++)
  19. {
  20. if (j == 1)
  21. suit = "Spades";
  22. else if(j == 2)
  23. suit = "Diamonds";
  24. else if (j==3)
  25. suit = "Clubs";
  26. else if (j == 4)
  27. suit = "Hearts";
  28.  
  29. if (i == 1)
  30. rank = "Ace";
  31. else if(i == 2)
  32. rank = "Two";
  33. else if (i == 3)
  34. rank = "Three";
  35. else if (i == 4)
  36. rank = "Four";
  37. else if (i == 5)
  38. rank = "Five";
  39. else if (i == 6)
  40. rank = "Six";
  41. else if (i == 7)
  42. rank = "Seven";
  43. else if (i == 8)
  44. rank = "Eight";
  45. else if (i == 9)
  46. rank = "Nine";
  47. else if (i == 10)
  48. rank = "Ten";
  49. else if (i == 11)
  50. rank = "Jack";
  51. else if (i == 12)
  52. rank = "Queen";
  53. else if (i == 13)
  54. rank = "King";
  55.  
  56. if (i == 1)
  57. value = 11;
  58. else if (i<=10)
  59. value = i;
  60. else
  61. value = 10;
  62. deck.add(new Card(value, rank, suit));
  63. }
  64. }
  65. }
  66.  
  67. /**
  68. * Returns the deck
  69. * @return the ArrayList deck
  70. */
  71. public ArrayList<Card> getDeck()
  72. {
  73. return deck;
  74. }
  75.  
  76. /**
  77. * Gets a specific card in the deck based off its position in the Array List
  78. * @return a Card in the deck
  79. */
  80. public Card getCardInDeck(int n)
  81. {
  82. return deck.get(n);
  83. }
  84.  
  85. /**
  86. * @returns A String representation of the deck.
  87. */
  88. public String toString()
  89. {
  90. String showDeck = "";
  91. for (int i=0; i<52; i++)
  92. {
  93. showDeck += deck.get(i) + "\n";
  94. }
  95. return showDeck;
  96. }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. public class DeckTester
  108. {
  109. public static void main (String[] args)
  110. {
  111. Deck d = new Deck();
  112. System.out.println(d);
  113. System.out.println();
  114. System.out.println(d.getCardInDeck(32));
  115. System.out.println(d.getCardInDeck(1));
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement