Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. package finalProject;
  2.  
  3. /***************************************************************************
  4. Copyright (c) 2000:
  5. University of Alberta,
  6. Deptartment of Computing Science
  7. Computer Poker Research Group
  8.  
  9. See "Liscence.txt"
  10. ***************************************************************************/
  11.  
  12. import java.util.Random;
  13.  
  14. /**
  15. * A Deck of 52 Cards which can be dealt and shuffled
  16. * @author Aaron Davidson
  17. */
  18.  
  19. public class Deck {
  20. public static final int NUM_CARDS = 52;
  21. private Card[] gCards = new Card[NUM_CARDS];
  22. private char position; // top of deck
  23. private Random r = new Random();
  24.  
  25. /**
  26. * Constructor.
  27. */
  28. public Deck() {
  29. position = 0;
  30. for (int i=0;i<NUM_CARDS;i++) {
  31. gCards[i] = new Card(i);
  32. }
  33. }
  34.  
  35. /**
  36. * Constructor w/ shuffle seed.
  37. * @param seed the seed to use in randomly shuffling the deck.
  38. */
  39. public Deck(long seed) {
  40. this();
  41. if (seed == 0) {
  42. seed = System.currentTimeMillis();
  43. }
  44. r.setSeed(seed);
  45. }
  46.  
  47. /**
  48. * Places all cards back into the deck.
  49. * Note: Does not sort the deck.
  50. */
  51. public synchronized void reset() { position = 0; }
  52.  
  53. /**
  54. * Shuffles the cards in the deck.
  55. */
  56. public synchronized void shuffle() {
  57. Card tempCard;
  58. int i,j;
  59. for (i=0; i<NUM_CARDS; i++) {
  60. j = i + randInt(NUM_CARDS-i);
  61. tempCard = gCards[j];
  62. gCards[j] = gCards[i];
  63. gCards[i] = tempCard;
  64. }
  65. position = 0;
  66. }
  67.  
  68. /**
  69. * Obtain the next card in the deck.
  70. * If no cards remain, a null card is returned
  71. * @return the card dealt
  72. */
  73. public synchronized Card deal() {
  74. return (position < NUM_CARDS ? gCards[position++] : null);
  75. }
  76.  
  77. /**
  78. * Obtain the next card in the deck.
  79. * If no cards remain, a null card is returned
  80. * @return the card dealt
  81. */
  82. public synchronized Card dealCard() {
  83. return extractRandomCard();
  84. }
  85.  
  86. /**
  87. * Find position of Card in Deck.
  88. */
  89. public synchronized int findCard(Card c) {
  90. int i = position;
  91. int n = c.getIndex();
  92. while (i < NUM_CARDS && n != gCards[i].getIndex())
  93. i++;
  94. return (i < NUM_CARDS ? i : -1);
  95. }
  96.  
  97. private synchronized int findDiscard(Card c) {
  98. int i = 0;
  99. int n = c.getIndex();
  100. while (i < position && n != gCards[i].getIndex())
  101. i++;
  102. return (n == gCards[i].getIndex() ? i : -1);
  103. }
  104.  
  105. /**
  106. * Remove all cards in the given hand from the Deck.
  107. */
  108. public synchronized void extractHand(Hand h) {
  109. for (int i=1;i<=h.size();i++)
  110. this.extractCard(h.getCard(i));
  111. }
  112.  
  113. /**
  114. * Remove a card from within the deck.
  115. * @param c the card to remove.
  116. */
  117. public synchronized void extractCard(Card c) {
  118. int i = findCard(c);
  119. if (i != -1) {
  120. Card t = gCards[i];
  121. gCards[i] = gCards[position];
  122. gCards[position] = t;
  123. position++;
  124. } else {
  125. System.err.println("*** ERROR: could not find card " + c);
  126. Thread.currentThread().dumpStack();
  127. }
  128. }
  129.  
  130. /**
  131. * Remove and return a randomly selected card from within the deck.
  132. */
  133. public synchronized Card extractRandomCard() {
  134. int pos = position+randInt(NUM_CARDS-position);
  135. Card c = gCards[pos];
  136. gCards[pos] = gCards[position];
  137. gCards[position] = c;
  138. position++;
  139. return c;
  140. }
  141.  
  142. /**
  143. * Return a randomly selected card from within the deck without removing it.
  144. */
  145. public synchronized Card pickRandomCard() {
  146. return gCards[position+randInt(NUM_CARDS-position)];
  147. }
  148.  
  149. /**
  150. * Place a card back into the deck.
  151. * @param c the card to insert.
  152. */
  153. public synchronized void replaceCard(Card c) {
  154. int i = findDiscard(c);
  155. if (i != -1) {
  156. position--;
  157. Card t = gCards[i];
  158. gCards[i] = gCards[position];
  159. gCards[position] = t;
  160. }
  161. }
  162.  
  163. /**
  164. * Obtain the position of the top card.
  165. * (the number of cards dealt from the deck)
  166. * @return the top card index
  167. */
  168. public synchronized int getTopCardIndex() {
  169. return position;
  170. }
  171.  
  172.  
  173. /**
  174. * Obtain the number of cards left in the deck
  175. */
  176. public synchronized int cardsLeft() {
  177. return NUM_CARDS-position;
  178. }
  179.  
  180. /**
  181. * Obtain the card at a specific index in the deck.
  182. * Does not matter if card has been dealt or not.
  183. * If i < topCardIndex it has been dealt.
  184. * @param i the index into the deck (0..51)
  185. * @return the card at position i
  186. */
  187. public synchronized Card getCard(int i) {
  188. return gCards[i];
  189. }
  190.  
  191. public String toString() {
  192. StringBuffer s = new StringBuffer();
  193. s.append("* ");
  194. for (int i=0;i<position;i++)
  195. s.append(gCards[i].toString()+" ");
  196. s.append("\n* ");
  197. for (int i=position;i<NUM_CARDS;i++)
  198. s.append(gCards[i].toString()+" ");
  199. return s.toString();
  200. }
  201.  
  202. private int randInt(int range) {
  203. return (int)(r.nextDouble()*range);
  204. }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement