CardDeck


SUBMITTED BY: Guest

DATE: May 11, 2016, 3:17 p.m.

FORMAT: Text only

SIZE: 2.4 kB

HITS: 689

  1. /**
  2. *
  3. */
  4. package ats_jp.activity.cardgame;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Random;
  8. import java.util.concurrent.ArrayBlockingQueue;
  9. /**
  10. * @author TMS
  11. *
  12. */
  13. public abstract class CardDeck {
  14. /**
  15. * utlizzo una ArrayList in quanto più performante, non ho bisogno di tenere
  16. * gli elementi ordinati in quanto ho un unico metodo di ricerca (l'altro
  17. * preleva unicamente un elemento Random)
  18. */
  19. protected ArrayList<Card> store;
  20. /**
  21. * metodo per l'inizializzazione del mazzo con valori Random; tale metodo
  22. * dovrà essere obbligatoriamente sovrascritto nelle classi successive
  23. */
  24. protected abstract void initializeDeck();
  25. public CardDeck()
  26. {
  27. store=new ArrayList<Card>();
  28. initializeDeck();
  29. }
  30. /**
  31. * elimina e restituisce una carta random dal mazzo;
  32. * se il mazzo è vuoto restituisce null
  33. *
  34. * @return
  35. */
  36. public Card getCard()
  37. {
  38. Card returnCard=null;
  39. if(!store.isEmpty())
  40. {
  41. returnCard=store.remove(randomGenerator(0, store.size()-1));
  42. }
  43. return returnCard;
  44. }
  45. /**
  46. * elimina e restituisce la carta uguale a paramCard;
  47. * se non è presente o il mazzo è vuoto torna null
  48. *
  49. * @param card
  50. * @return
  51. */
  52. public Card getCard(Card paramCard)
  53. {
  54. Card returnCard=null;
  55. if(paramCard!=null)
  56. {
  57. if(!store.isEmpty() && store.contains(paramCard))
  58. {
  59. returnCard=store.remove(store.indexOf(paramCard));
  60. }
  61. } else {
  62. throw new IllegalArgumentException("Null argument");
  63. }
  64. return returnCard;
  65. }
  66. /**
  67. * inserisce paramCard nel mazzo;
  68. * nel caso paramCard==null solleva un'eccezione (IllegalArgumentException)
  69. *
  70. * @param paramCard
  71. * @return
  72. */
  73. public boolean put(Card paramCard)
  74. {
  75. boolean flag=false;
  76. if(paramCard!=null)
  77. {
  78. flag=store.add(paramCard);
  79. } else {
  80. throw new IllegalArgumentException("Null Argument");
  81. }
  82. return flag;
  83. }
  84. /**
  85. * restituisce il numero di carte nel mazzo
  86. *
  87. * @return
  88. */
  89. public int getCurrentCount()
  90. {
  91. return store.size();
  92. }
  93. private int randomGenerator(int min, int max)
  94. {
  95. int result=-1;
  96. if(min>max) {
  97. throw new IllegalArgumentException("Min value greatest!");
  98. } else {
  99. Random mRandom = new Random();
  100. result = (int) mRandom.nextInt(max-min+1)+min;
  101. }
  102. return result;
  103. }
  104. }

comments powered by Disqus