PlayingCard


SUBMITTED BY: Guest

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

FORMAT: Java

SIZE: 7.5 kB

HITS: 556

  1. /**
  2. *
  3. */
  4. package ats_jp.activity.cardgame;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Map.Entry;
  8. import java.util.Set;
  9. import ats_jp.activity.profile.PlayerProfile;
  10. /**
  11. * @author TMS
  12. *
  13. */
  14. public class PlayingCard implements Card
  15. {
  16. // constant field
  17. public static final int RANK=200;
  18. public static final int ACE = 1;
  19. public static final int TWO = 2;
  20. public static final int THREE = 3;
  21. public static final int FOUR = 4;
  22. public static final int FIVE = 5;
  23. public static final int SIX = 6;
  24. public static final int SEVEN = 7;
  25. public static final int EIGHT = 8;
  26. public static final int NINE = 9;
  27. public static final int TEN = 10;
  28. public static final int JACK = 11;
  29. public static final int QUEEN = 12;
  30. public static final int KING = 13;
  31. public static final int SUIT = 201;
  32. public static final int SPADES = 100;
  33. public static final int HEARTS = 101;
  34. public static final int DIAMOND = 102;
  35. public static final int CLUBS = 103;
  36. public static final int STR_RANK=202;
  37. public static final String STR_ACE = "Ace";
  38. public static final String STR_TWO = "Two";
  39. public static final String STR_THREE = "Three";
  40. public static final String STR_FOUR = "Four";
  41. public static final String STR_FIVE = "Five";
  42. public static final String STR_SIX = "Six";
  43. public static final String STR_SEVEN = "Seven";
  44. public static final String STR_EIGHT = "Eight";
  45. public static final String STR_NINE = "Nine";
  46. public static final String STR_TEN = "Ten";
  47. public static final String STR_JACK = "Jack";
  48. public static final String STR_QUEEN = "Queen";
  49. public static final String STR_KING = "King";
  50. public static final int STR_SUIT=203;
  51. public static final String STR_SPADES = "Spades";
  52. public static final String STR_HEARTS = "Hearts";
  53. public static final String STR_DIAMONDS = "Diamonds";
  54. public static final String STR_CLUBS = "Clubs";
  55. private static final HashMap<String, String> STR_RANK_MAP= new HashMap<String, String>() {{
  56. put("STR_ACE","Ace");
  57. put("STR_TWO","Two");
  58. put("STR_THREE","Three");
  59. put("STR_FOUR","Four");
  60. put("STR_FIVE","Five");
  61. put("STR_SIX","Six");
  62. put("STR_SEVEN","Seven");
  63. put("STR_EIGHT","Eight");
  64. put("STR_NINE","Nine");
  65. put("STR_TEN","Ten");
  66. put("STR_JACK","Jack");
  67. put("STR_QUEEN","Queen");
  68. put("STR_KING","King");
  69. }};
  70. private static final HashMap<String, String> STR_SUIT_MAP= new HashMap<String, String>() {{
  71. put("STR_SPADES","Spades");
  72. put("STR_HEARTS","Hearts");
  73. put("STR_DIAMONDS","Diamonds");
  74. put("STR_CLUBS","Clubs");
  75. }};
  76. // normal attribute
  77. private static HashMap<Integer, PlayingCard> myMapDeck = new HashMap<Integer, PlayingCard>();
  78. private int rank;
  79. private int suit;
  80. @Override
  81. public Object getCardAttribute(int attribute) throws CardException {
  82. Object resultObj=null;
  83. switch (attribute)
  84. {
  85. case PlayingCard.RANK:
  86. resultObj=(Integer) getRank();
  87. break;
  88. case PlayingCard.SUIT:
  89. resultObj=(Integer) getSuit();
  90. break;
  91. case PlayingCard.STR_RANK:
  92. resultObj= (String) getStrRank(getRank());
  93. // come faccio??? Utilizzo una EnumTable???
  94. break;
  95. case PlayingCard.STR_SUIT:
  96. resultObj=(String) getStrSuit(getSuit());
  97. // come faccio??? Problema analogo a sopra...
  98. break;
  99. default:
  100. throw new IllegalArgumentException("Invalid argument");
  101. }
  102. return resultObj;
  103. }
  104. private String getStrRank(int paramRank)
  105. {
  106. String result="";
  107. switch (paramRank)
  108. {
  109. case PlayingCard.ACE:
  110. result = PlayingCard.STR_ACE;
  111. break;
  112. case PlayingCard.TWO:
  113. result = PlayingCard.STR_TWO;
  114. break;
  115. case PlayingCard.THREE:
  116. result = PlayingCard.STR_THREE;
  117. break;
  118. case PlayingCard.FOUR:
  119. result = PlayingCard.STR_FOUR;
  120. break;
  121. case PlayingCard.FIVE:
  122. result = PlayingCard.STR_FIVE;
  123. break;
  124. case PlayingCard.SIX:
  125. result = PlayingCard.STR_SIX;
  126. break;
  127. case PlayingCard.SEVEN:
  128. result = PlayingCard.STR_SEVEN;
  129. break;
  130. case PlayingCard.EIGHT:
  131. result = PlayingCard.STR_EIGHT;
  132. break;
  133. case PlayingCard.NINE:
  134. result = PlayingCard.STR_NINE;
  135. break;
  136. case PlayingCard.TEN:
  137. result = PlayingCard.STR_TEN;
  138. break;
  139. case PlayingCard.JACK:
  140. result = PlayingCard.STR_JACK;
  141. break;
  142. case PlayingCard.QUEEN:
  143. result = PlayingCard.STR_QUEEN;
  144. break;
  145. case PlayingCard.KING:
  146. result = PlayingCard.STR_KING;
  147. break;
  148. default:
  149. throw new IllegalArgumentException("Invalid rank (doesn't exist!)");
  150. }
  151. return result;
  152. }
  153. private String getStrSuit(int paramSuit)
  154. {
  155. String result = "";
  156. switch (paramSuit)
  157. {
  158. case PlayingCard.SPADES:
  159. result = PlayingCard.STR_SPADES;
  160. break;
  161. case PlayingCard.HEARTS:
  162. result = PlayingCard.STR_HEARTS;
  163. break;
  164. case PlayingCard.DIAMOND:
  165. result = PlayingCard.STR_DIAMONDS;
  166. break;
  167. case PlayingCard.CLUBS:
  168. result = PlayingCard.STR_CLUBS;
  169. break;
  170. default:
  171. throw new IllegalArgumentException("Invalid suit (doesn't exist!)");
  172. }
  173. return result;
  174. }
  175. private PlayingCard(int rank, int suit)
  176. {
  177. if((rank>0 && rank<14) && (suit>99 && suit<104)){
  178. this.setRank(rank);
  179. this.setSuit(suit);
  180. } else {
  181. throw new IllegalArgumentException("Invalid rank's value or suit's value");
  182. }
  183. }
  184. /**
  185. * @return the rank
  186. */
  187. public int getRank() {
  188. return rank;
  189. }
  190. /**
  191. * @param rank the rank to set
  192. */
  193. private void setRank(int rank) {
  194. this.rank = rank;
  195. }
  196. /**
  197. * @return the suit
  198. */
  199. public int getSuit() {
  200. return suit;
  201. }
  202. /**
  203. * @param suit the suit to set
  204. */
  205. private void setSuit(int suit) {
  206. this.suit = suit;
  207. }
  208. public String toString()
  209. {
  210. String resultString="";
  211. try {
  212. resultString=getCardAttribute(PlayingCard.STR_RANK)+" of "+getCardAttribute(PlayingCard.STR_SUIT);
  213. } catch (CardException e) {
  214. e.printStackTrace();
  215. System.out.println("Invalid attribute");
  216. }
  217. return resultString;
  218. }
  219. public boolean equals(PlayingCard card)
  220. {
  221. boolean flag=false;
  222. try
  223. {
  224. if(getCardAttribute(PlayingCard.RANK)==card.getCardAttribute(PlayingCard.RANK)
  225. && getCardAttribute(PlayingCard.RANK)==card.getCardAttribute(PlayingCard.RANK))
  226. {
  227. flag=true;
  228. }
  229. } catch(CardException exc) {
  230. exc.printStackTrace();
  231. System.out.println("Invalid attribute");
  232. }
  233. return flag;
  234. }
  235. public static PlayingCard getCard(int paramRank, int paramSuit)
  236. {
  237. //**************************************//
  238. // start my beautiful code :D (I hope!) //
  239. //**************************************//
  240. // creo la carta coi parametri forniti
  241. PlayingCard paramPlayingCard = new PlayingCard(paramRank, paramSuit);
  242. // devo vedere se รจ presente
  243. Integer nextPos=null;
  244. if(!myMapDeck.containsValue(paramPlayingCard))
  245. {
  246. nextPos=new Integer(myMapDeck.size()+1);
  247. myMapDeck.put(nextPos, paramPlayingCard);
  248. } else {
  249. Set<Integer> tmpSet=myMapDeck.keySet();
  250. for(Integer pc : tmpSet)
  251. {
  252. if(myMapDeck.get(pc).equals(paramPlayingCard))
  253. {
  254. nextPos=pc;
  255. System.out.print("\n"+nextPos+"\n");
  256. break;
  257. }
  258. }
  259. }
  260. System.out.println(myMapDeck.get(nextPos));
  261. return myMapDeck.get(nextPos);
  262. }
  263. }

comments powered by Disqus