PlayingCard


SUBMITTED BY: Guest

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

FORMAT: Java

SIZE: 7.2 kB

HITS: 601

  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","");
  57. put("STR_TWO","");
  58. put("STR_THREE","");
  59. put("STR_FOUR","");
  60. put("STR_FIVE","");
  61. put("STR_SIX","");
  62. put("STR_SEVEN","");
  63. put("STR_EIGHT","");
  64. put("STR_NINE","");
  65. put("STR_TEN","");
  66. put("STR_JACK","");
  67. put("STR_QUEEN","");
  68. put("STR_KING","");
  69. }}
  70. // normal attribute
  71. private static HashMap<Integer, PlayingCard> myMapDeck = new HashMap<Integer, PlayingCard>();
  72. private int rank;
  73. private int suit;
  74. @Override
  75. public Object getCardAttribute(int attribute) throws CardException {
  76. Object resultObj=null;
  77. switch (attribute)
  78. {
  79. case PlayingCard.RANK:
  80. resultObj=(Integer) getRank();
  81. break;
  82. case PlayingCard.SUIT:
  83. resultObj=(Integer) getSuit();
  84. break;
  85. case PlayingCard.STR_RANK:
  86. resultObj= (String) getStrRank(getRank());
  87. // come faccio??? Utilizzo una EnumTable???
  88. break;
  89. case PlayingCard.STR_SUIT:
  90. resultObj=(String) getStrSuit(getSuit());
  91. // come faccio??? Problema analogo a sopra...
  92. break;
  93. default:
  94. throw new IllegalArgumentException("Invalid argument");
  95. }
  96. return resultObj;
  97. }
  98. private String getStrRank(int paramRank)
  99. {
  100. String result="";
  101. switch (paramRank)
  102. {
  103. case PlayingCard.ACE:
  104. result = PlayingCard.STR_ACE;
  105. break;
  106. case PlayingCard.TWO:
  107. result = PlayingCard.STR_TWO;
  108. break;
  109. case PlayingCard.THREE:
  110. result = PlayingCard.STR_THREE;
  111. break;
  112. case PlayingCard.FOUR:
  113. result = PlayingCard.STR_FOUR;
  114. break;
  115. case PlayingCard.FIVE:
  116. result = PlayingCard.STR_FIVE;
  117. break;
  118. case PlayingCard.SIX:
  119. result = PlayingCard.STR_SIX;
  120. break;
  121. case PlayingCard.SEVEN:
  122. result = PlayingCard.STR_SEVEN;
  123. break;
  124. case PlayingCard.EIGHT:
  125. result = PlayingCard.STR_EIGHT;
  126. break;
  127. case PlayingCard.NINE:
  128. result = PlayingCard.STR_NINE;
  129. break;
  130. case PlayingCard.TEN:
  131. result = PlayingCard.STR_TEN;
  132. break;
  133. case PlayingCard.JACK:
  134. result = PlayingCard.STR_JACK;
  135. break;
  136. case PlayingCard.QUEEN:
  137. result = PlayingCard.STR_QUEEN;
  138. break;
  139. case PlayingCard.KING:
  140. result = PlayingCard.STR_KING;
  141. break;
  142. default:
  143. throw new IllegalArgumentException("Invalid rank (doesn't exist!)");
  144. }
  145. return result;
  146. }
  147. private String getStrSuit(int paramSuit)
  148. {
  149. String result = "";
  150. switch (paramSuit)
  151. {
  152. case PlayingCard.SPADES:
  153. result = PlayingCard.STR_SPADES;
  154. break;
  155. case PlayingCard.HEARTS:
  156. result = PlayingCard.STR_HEARTS;
  157. break;
  158. case PlayingCard.DIAMOND:
  159. result = PlayingCard.STR_DIAMONDS;
  160. break;
  161. case PlayingCard.CLUBS:
  162. result = PlayingCard.STR_CLUBS;
  163. break;
  164. default:
  165. throw new IllegalArgumentException("Invalid suit (doesn't exist!)");
  166. }
  167. return result;
  168. }
  169. private PlayingCard(int rank, int suit)
  170. {
  171. if((rank>0 && rank<14) && (suit>99 && suit<104)){
  172. this.setRank(rank);
  173. this.setSuit(suit);
  174. } else {
  175. throw new IllegalArgumentException("Invalid rank's value or suit's value");
  176. }
  177. }
  178. /**
  179. * @return the rank
  180. */
  181. public int getRank() {
  182. return rank;
  183. }
  184. /**
  185. * @param rank the rank to set
  186. */
  187. private void setRank(int rank) {
  188. this.rank = rank;
  189. }
  190. /**
  191. * @return the suit
  192. */
  193. public int getSuit() {
  194. return suit;
  195. }
  196. /**
  197. * @param suit the suit to set
  198. */
  199. private void setSuit(int suit) {
  200. this.suit = suit;
  201. }
  202. public String toString()
  203. {
  204. String resultString="";
  205. try {
  206. resultString=getCardAttribute(PlayingCard.STR_RANK)+" of "+getCardAttribute(PlayingCard.STR_SUIT);
  207. } catch (CardException e) {
  208. e.printStackTrace();
  209. System.out.println("Invalid attribute");
  210. }
  211. return resultString;
  212. }
  213. public boolean equals(PlayingCard card)
  214. {
  215. boolean flag=false;
  216. try
  217. {
  218. if(getCardAttribute(PlayingCard.RANK)==card.getCardAttribute(PlayingCard.RANK)
  219. && getCardAttribute(PlayingCard.RANK)==card.getCardAttribute(PlayingCard.RANK))
  220. {
  221. flag=true;
  222. }
  223. } catch(CardException exc) {
  224. exc.printStackTrace();
  225. System.out.println("Invalid attribute");
  226. }
  227. return flag;
  228. }
  229. public static PlayingCard getCard(int paramRank, int paramSuit)
  230. {
  231. //**************************************//
  232. // start my beautiful code :D (I hope!) //
  233. //**************************************//
  234. // creo la carta coi parametri forniti
  235. PlayingCard paramPlayingCard = new PlayingCard(paramRank, paramSuit);
  236. // devo vedere se รจ presente
  237. Integer nextPos=null;
  238. if(!myMapDeck.containsValue(paramPlayingCard))
  239. {
  240. nextPos=new Integer(myMapDeck.size()+1);
  241. myMapDeck.put(nextPos, paramPlayingCard);
  242. } else {
  243. Set<Integer> tmpSet=myMapDeck.keySet();
  244. for(Integer pc : tmpSet)
  245. {
  246. if(myMapDeck.get(pc).equals(paramPlayingCard))
  247. {
  248. nextPos=pc;
  249. System.out.print("\n"+nextPos+"\n");
  250. break;
  251. }
  252. }
  253. }
  254. System.out.println(myMapDeck.get(nextPos));
  255. return myMapDeck.get(nextPos);
  256. }
  257. }

comments powered by Disqus