Advertisement
Guest User

Untitled

a guest
May 25th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // Michael Lee
  2. // Tuesday, April 14, 2015
  3. // Sample program: StackDeck.java
  4. //
  5. // This class represents a deck of cards, which you can shuffle
  6. // and manipulate.
  7.  
  8. import java.util.List;
  9. import java.util.ArrayList;
  10. import java.util.NoSuchElementException;
  11. import cse143.util.Stack;
  12. import cse143.util.ArrayStack;
  13.  
  14. public class StackDeck implements Deck {
  15. private Stack<String> cards;
  16.  
  17. // Creates an empty deck of cards.
  18. public StackDeck() {
  19. this.cards = new ArrayStack<String>();
  20. }
  21.  
  22. // Returns the number of cards in the deck.
  23. public int size() {
  24. return this.cards.size();
  25. }
  26.  
  27. // Returns 'true' if the deck contains no cards, returns 'false' otherwise.
  28. public boolean isEmpty() {
  29. return this.cards.isEmpty();
  30. }
  31.  
  32. // Removes and returns the card currently at the top of the deck.
  33. //
  34. // Preconditions:
  35. // - The deck is not empty (otherwise throws NoSuchElementException)
  36. public String dealCard() {
  37. this.failIfUnableToRemove(1);
  38. return this.cards.pop();
  39. }
  40.  
  41. // Returns the requested amount of cards from the top of the deck.
  42. //
  43. // Preconditions:
  44. // - "amount" must not be negative (otherwise throws IllegalArgumentException)
  45. // - "amount" must not exceed the size of the deck
  46. // (otherwise throws NoSuchElementException)
  47. //
  48. // Postconditions:
  49. // - Returns a new Deck containing the cards, with the card at the
  50. // top of this deck located at the bottom of the returned one.
  51. public Deck dealCards(int amount) {
  52. this.failIfUnableToRemove(amount);
  53. Deck output = new StackDeck();
  54. this.transfer(this.cards, output.cards, amount);
  55. return output;
  56. }
  57.  
  58. // Adds a card to the top of the deck.
  59. //
  60. // Preconditions:
  61. // - The card is not null
  62. public void addCard(String card) {
  63. this.cards.push(card);
  64. }
  65.  
  66. // Moves all the cards in the other deck to this one.
  67. //
  68. // Preconditions:
  69. // - "other" must not be null
  70. //
  71. // Postconditions:
  72. // - The other deck will be emptied of all cards
  73. public void mergeDeck(Deck other) {
  74. this.transfer(other.cards, this.cards);
  75. }
  76.  
  77. // Randomly shuffles the cards in the deck in place.
  78. public void shuffle() {
  79. // Implements a riffle shuffle -- splits the deck in two,
  80. // and splices the two together.
  81. // Note: this is an _imperfect_ shuffle. It doesn't actually
  82. // randomly shuffle the deck in any way, making this a rigged deck.
  83.  
  84. int half = this.size() / 2;
  85. Stack<String> temp1 = new ArrayStack<String>();
  86. Stack<String> temp2 = new ArrayStack<String>();
  87.  
  88. // Split the deck in two
  89. this.transfer(this.cards, temp1, half);
  90. this.transfer(this.cards, temp2); // move everything else
  91.  
  92. // Take one card at a time from each half and add it back
  93. for (int j = 0; j < half; j++) {
  94. this.cards.push(temp1.pop());
  95. this.cards.push(temp2.pop());
  96. }
  97.  
  98. // Take care of the last card if we have an odd number of cards
  99. if (!temp2.isEmpty()) {
  100. this.cards.push(temp2.pop());
  101. }
  102. }
  103.  
  104. // Returns a copy of the contents of the card as a list without modifying
  105. // the deck itself.
  106. //
  107. // Postconditions:
  108. // - The card at the top of the deck will be placed at the end of the list.
  109. public List<String> toList() {
  110. Stack<String> temp = new ArrayStack<String>();
  111. List<String> output = new ArrayList<String>();
  112.  
  113. // Move cards to temp stack, reversing it. The bottom card is now
  114. // at the top of the stack.
  115. this.transfer(this.cards, temp);
  116.  
  117. // Moves everything back, and adds to the list in the correct order
  118. while (!temp.isEmpty()) {
  119. String card = this.move(temp, this.cards);
  120. output.add(card);
  121. }
  122.  
  123. return output;
  124. }
  125.  
  126. // Returns a string representing this deck.
  127. //
  128. // Postconditions:
  129. // - Each card will be separated by comma, surrounded by brackets
  130. // - The card at the top of the deck will be placed at the end of the string.
  131. //
  132. // Example of output:
  133. //
  134. // [King of Spades, 9 of Hearts, Ace of Clubs, 4 of Spades, ...]
  135. //
  136. public String toString() {
  137. if (this.isEmpty()) {
  138. return "[]";
  139. } else {
  140. Stack<String> temp = new ArrayStack<String>();
  141. String output = this.move(this.cards, temp);
  142.  
  143. // Note: adds to the back of the string, not the front
  144. // (so I can fencepost)
  145. while (!this.cards.isEmpty()) {
  146. String card = this.move(cards, temp);
  147. output = card + ", " + output;
  148. }
  149.  
  150. // Restores the stack
  151. this.transfer(temp, this.cards);
  152.  
  153. return "[" + output + "]";
  154. }
  155. }
  156.  
  157. // Transfers everything in the 'src' stack to the 'dest' stack.
  158. //
  159. // Preconditions:
  160. // - Neither stack should be null
  161. private void transfer(Stack<String> src, Stack<String> dest) {
  162. this.transfer(src, dest, src.size());
  163. }
  164.  
  165. // Transfers the top 'amount' of elements from the 'src' stack to
  166. // the 'dest' stack.
  167. //
  168. // Preconditions:
  169. // - Neither stack should be null
  170. // - The src stack must have at least 'amount' number of elements
  171. // - The amount must not be negative
  172. private void transfer(Stack<String> src, Stack<String> dest, int amount) {
  173. for (int i = 0; i < amount; i++) {
  174. dest.push(src.pop());
  175. }
  176. }
  177.  
  178. // Moves one element from 'src' to 'dest', but returns that one element.
  179. //
  180. // Preconditions:
  181. // - Neither stack should be null.
  182. // - The 'src' stack must have at least one element.
  183. private String move(Stack<String> src, Stack<String> dest) {
  184. String card = src.pop();
  185. dest.push(card);
  186. return card;
  187. }
  188.  
  189. // Throws an NoSuchElementException if it is not possible to remove
  190. // "n" cards from the deck, otherwise does nothing.
  191. private void failIfUnableToRemove(int n) {
  192. if (this.size() < n) {
  193. throw new NoSuchElementException("Deck doesn't contain enough cards");
  194. }
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement