Anon017706349

d1

Sep 28th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. public class Deck
  2. {
  3.  
  4. // Heavily inspired by the code used in the solution to quiz 1
  5.  
  6. private Card[] deck; // Array of 52 cards representing the deck
  7. private int top;
  8. private int cardsUsed; // Number of cards used. Required to find remainder of cards in the deck
  9.  
  10. public Deck() // Constructor to create a new (un-shuffled) deck of cards
  11. {
  12. top = 52; // Represents the integer index value for the top of the deck
  13. deck = new Card[52];
  14.  
  15. int index = 0;
  16. for (int suit = 0; suit <= 3; suit++)
  17. {
  18. for (int rank = 1; rank <= 13; rank++)
  19. {
  20. deck[index] = new Card(rank, suit);
  21. index++;
  22. }
  23. }
  24.  
  25. cardsUsed = 0;
  26. }
  27.  
  28. // Method for shuffling the full deck of cards
  29. // Shuffling a non-full deck may result in duplicating cards
  30. public void shuffle()
  31. {
  32. for (int i = 51; i > 0; i--)
  33. {
  34. int rand = (int) (Math.random()*(i+1)); // Generates a random integer from 52 to 1
  35. Card temp = deck[i];
  36. deck[i] = deck[rand];
  37. deck[rand] = temp;
  38. }
  39.  
  40. // cardsUsed = 0;
  41. }
  42.  
  43. public void displayDeck() // Displays all cards in the deck
  44. {
  45. for (int i =0; i < top; i++)
  46. {
  47. System.out.println(deck[i]);
  48. }
  49. }
  50.  
  51. public Card deal() // Deals the top card from the deck
  52. {
  53. if (top <= 0)
  54. {
  55. return null;
  56. }
  57.  
  58. cardsUsed++;
  59.  
  60. return (deck[--top]);
  61. }
  62.  
  63. public int remainder() // Returns number of cards left in the deck
  64. {
  65. return 52 - cardsUsed;
  66.  
  67. }
  68.  
  69.  
  70.  
  71. }
Add Comment
Please, Sign In to add comment