You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.7 KiB

6 years ago
  1. // The Deck class that represents decks
  2. // Do not make any changes to this file!
  3. // Xiwei Wang
  4. import java.io.*;
  5. public class Deck implements Serializable
  6. {
  7. // instance variables
  8. private Card[][] myCards;
  9. private int m_numCards; // the number of card that are picked up to build the partial deck
  10. private String m_sortedDeck;
  11. // constructor
  12. public Deck()
  13. {
  14. myCards = new Card[4][13];
  15. // populate the card array
  16. for (int i = 0; i < 4; i++)
  17. for (int j = 0; j < 13; j++)
  18. myCards[i][j] = new Card(i, j);
  19. }
  20. // shuffle the deck
  21. public void shuffle(int numShuffles)
  22. {
  23. for (int i = 0; i < numShuffles; i++)
  24. {
  25. // generate random positions
  26. int suit1 = (int)(Math.random() * 4);
  27. int suit2 = (int)(Math.random() * 4);
  28. int rank1 = (int)(Math.random() * 13);
  29. int rank2 = (int)(Math.random() * 13);
  30. // swap the cards
  31. Card temp = myCards[suit1][rank1];
  32. myCards[suit1][rank1] = myCards[suit2][rank2];
  33. myCards[suit2][rank2] = temp;
  34. }
  35. }
  36. // return a particular card in the deck
  37. public Card getCard(int suit, int rank)
  38. {
  39. return myCards[suit][rank];
  40. }
  41. // return a specific number of cards as a card array
  42. public Card[] getPartialDeck(int numCards)
  43. {
  44. Card[] partialDeck = new Card[numCards];
  45. int counter = 0;
  46. for (int i = 0; i < 4; i++)
  47. for (int j = 0; j < 13; j++)
  48. {
  49. partialDeck[counter] = myCards[i][j];
  50. counter++;
  51. if (counter == numCards)
  52. return partialDeck;
  53. }
  54. return partialDeck;
  55. }
  56. // return a string reprentation of the deck
  57. public String toString()
  58. {
  59. String deckContent = "";
  60. for (int i = 0; i < 4; i++)
  61. {
  62. for (int j = 0; j < 13; j++)
  63. deckContent += myCards[i][j].toString() + " ";
  64. deckContent += "\n";
  65. }
  66. return deckContent;
  67. }
  68. // set the number of cards that would be picked up and the sorted deck string
  69. public void setVars(int numCards, String sortedDeck)
  70. {
  71. m_numCards = numCards;
  72. m_sortedDeck = sortedDeck;
  73. }
  74. // return the number of cards that would be picked up
  75. public int getnumCards()
  76. {
  77. return m_numCards;
  78. }
  79. // return the sorted deck string
  80. public String getSortedDeck()
  81. {
  82. return m_sortedDeck;
  83. }
  84. }