Guest User

Untitled

a guest
Aug 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1.  
  2. public class CardDeck extends java.util.Stack<Card> {
  3. public CardDeck() {
  4. for(int i = 0; i < 4; i++) {
  5. for (int j = 0; j < 13; j++) {
  6. this.push(new Card(i, j+1));
  7. }
  8. }
  9. }
  10. private void swapCards(int i, int j) {
  11. Card tempCard = get(i);
  12. set(i, get(j));
  13. set(j, tempCard);
  14. }
  15. public void shuffle() {
  16. for (int i = 0; i < this.size(); i++) {
  17. int j = (int) (Math.random() * 52);
  18. swapCards(i, j);
  19. }
  20. }
  21. public Card dealCard() {
  22. return pop();
  23. }
  24. public String toString() {
  25. StringBuilder sb = new StringBuilder();
  26. for (Card c : this) {
  27. sb.append(c.getSuit()).append("/").append(c.getValue()).append(", ");
  28. }
  29. return sb.toString();
  30. }
  31. }
Add Comment
Please, Sign In to add comment