Advertisement
Guest User

Deck

a guest
Dec 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class Deck {
  5.  
  6. private ArrayList<Card> cards;
  7.  
  8. public Deck() {
  9. this.cards = new ArrayList<Card>();
  10. }
  11.  
  12. public void createFullDeck() {
  13. for(Suit cardSuit: Suit.values()) {
  14. for(Value cardValue: Value.values()) {
  15. this.cards.add(new Card(cardSuit,cardValue));
  16. }
  17. }
  18. }
  19.  
  20. public void moveToDeck(Deck moveTo) {
  21. int thisDeckSize= this.cards.size();
  22. for(int i=0; i< thisDeckSize; i++);{
  23. int i = 0;
  24. moveTo.addCard(this.getCard(i));
  25. }
  26. for(int i=0; i<thisDeckSize; i++) {
  27. this.removeCard(0);
  28. }
  29. }
  30.  
  31. public void shuffle() {
  32. ArrayList<Card> tmpDeck=new ArrayList<Card>();
  33. Random rand = new Random();
  34. int randomCardIndex = 0;
  35. int originalSize=this.cards.size();
  36. for (int i=0; i<originalSize; i++) {
  37. randomCardIndex=rand.nextInt((this.cards.size()-1 - 0) + 1) + 0;
  38. tmpDeck.add(this.cards.get(randomCardIndex));
  39. this.cards.remove(randomCardIndex);
  40. }
  41. this.cards=tmpDeck;
  42. }
  43.  
  44. public String toString() {
  45. String cardListOutput ="";
  46. for(Card aCard: this.cards) {
  47. cardListOutput += "\n " + aCard.toString();
  48. }
  49. return cardListOutput;
  50. }
  51.  
  52. public void removeCard(int i) {
  53. this.cards.remove(i);
  54. }
  55. public Card getCard(int i) {
  56. return this.cards.get(i);
  57. }
  58. public void addCard(Card addCard) {
  59. this.cards.add(addCard);
  60. }
  61. public void draw(Deck comingFrom) {
  62. this.cards.add(comingFrom.getCard(0));
  63. comingFrom.removeCard(0);
  64. }
  65.  
  66. public int deckSize() {
  67. return this.cards.size();
  68. }
  69.  
  70. public int cardsValue() {
  71. int totalValue = 0;
  72. int aces = 0;
  73.  
  74. for(Card aCard: this.cards) {
  75. switch(aCard.getValue()) {
  76. case TWO:
  77. totalValue +=2;
  78. break;
  79. case THREE:
  80. totalValue +=3;
  81. break;
  82. case FOUR:
  83. totalValue +=4;
  84. break;
  85. case FIVE:
  86. totalValue +=5;
  87. break;
  88. case SIX:
  89. totalValue +=6;
  90. break;
  91. case SEVEN:
  92. totalValue +=7;
  93. break;
  94. case EIGHT:
  95. totalValue +=8;
  96. break;
  97. case NINE:
  98. totalValue +=9;
  99. break;
  100. case TEN:
  101. totalValue +=10;
  102. break;
  103. case JACK:
  104. totalValue +=10;
  105. break;
  106. case QUEEN:
  107. totalValue +=10;
  108. break;
  109. case KING:
  110. totalValue +=10;
  111. break;
  112. case ACE:
  113. aces +=1;
  114. break;
  115. }
  116. }
  117. for(int i =0; i<aces; i++) {
  118. if(totalValue >10) {
  119. totalValue+=1;
  120. }
  121. else{
  122. totalValue+=11;
  123. }
  124. }
  125. return totalValue;
  126. }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement