Java Implementation of the game set.
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.

213 lines
5.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. /*
  2. jeffery r
  3. 12-26-15
  4. Game class keeps track of the score hand and deck
  5. */
  6. package net.jrtechs.setgame;
  7. import java.awt.Color;
  8. import java.util.ArrayList;
  9. public class Game
  10. {
  11. //properties
  12. private Deck deck;
  13. private Hand hand;
  14. private int sets;
  15. private int score;
  16. private int setsLeft;
  17. private int currentDisplay = 12;
  18. private int time;
  19. private int timeOnSet;
  20. //constructor
  21. Game()
  22. {
  23. deck = new Deck();
  24. deck.shuffle();
  25. hand = new Hand(deck.dealMany(12));
  26. setsLeft = hand.setsLeft();
  27. sets = 0;
  28. time = 0;
  29. score = 0;
  30. currentDisplay = 12;
  31. timeOnSet = 0;
  32. }
  33. public void timerTic()
  34. {
  35. time++;
  36. timeOnSet++;
  37. }
  38. public Card[] getHand()
  39. {
  40. return hand.getHand();
  41. }
  42. public void clicked(SetButton[] cards)
  43. {
  44. ArrayList<Card> sel;
  45. sel = selected(cards);
  46. if(sel.size() == 3)
  47. {
  48. //there is a set selected
  49. if(Card.isSet(sel.get(0),sel.get(1), sel.get(2)))
  50. {
  51. sets++;
  52. //calculates score to be given baised on time took and cards on board
  53. if(currentDisplay ==12)
  54. {
  55. int temp = 100 - timeOnSet;
  56. if(temp > 0)
  57. {
  58. score +=temp;
  59. }
  60. }
  61. score += 50; //base score
  62. timeOnSet = 0;
  63. Card[] remove = {sel.get(0),sel.get(1), sel.get(2)};
  64. if(deck.cardsLeft() > 2)
  65. {
  66. hand.removePair(remove, deck);
  67. if(currentDisplay > 12)
  68. {
  69. currentDisplay -= 3;
  70. hand.resize(currentDisplay, deck);
  71. for(int i = 12; i < 15; i ++)
  72. {
  73. cards[i].setVisible(false);
  74. }
  75. }
  76. updateBoard(cards);
  77. }
  78. else
  79. {
  80. for(SetButton elem : cards)
  81. {
  82. if(elem.getPressed())
  83. {
  84. hand.disableCard(elem.getCard());
  85. elem.setEnabled(false);
  86. elem.setBackground(Color.black);
  87. }
  88. }
  89. }
  90. if(deck.cardsLeft() < 3)
  91. {
  92. setsLeft = 0;
  93. }
  94. else
  95. {
  96. setsLeft= hand.setsLeft();
  97. }
  98. }
  99. diselectCards(cards);
  100. }
  101. }
  102. public void openThree(SetButton[] cards)
  103. {
  104. if(currentDisplay == 12)
  105. {
  106. currentDisplay += 3;
  107. hand.resize(currentDisplay, deck);
  108. //hand.display();
  109. for(int i = 12; i < cards.length; i++)
  110. {
  111. cards[i].setVisible(true);
  112. }
  113. this.updateBoard(cards);
  114. }
  115. else
  116. {
  117. }
  118. }
  119. public void updateBoard(SetButton[] cards)
  120. {
  121. int count = 0;
  122. for(Card elem : hand.getHand())
  123. {
  124. if(count >= currentDisplay)
  125. {
  126. }
  127. else
  128. {
  129. cards[count].setCard(elem);
  130. count ++;
  131. }
  132. }
  133. for(SetButton elem : cards)
  134. {
  135. elem.update();
  136. elem.setBackground(Color.white);
  137. elem.setSelected(false);
  138. elem.setEnabled(true);
  139. }
  140. }
  141. public void findSet(SetButton[] cards)
  142. {
  143. Card[] set = hand.findSet();
  144. for(Card elem : set)
  145. {
  146. int loc = hand.location(elem);
  147. //row * (4) + col
  148. cards[loc].setBackground(Color.PINK);
  149. }
  150. }
  151. public int getTime()
  152. {
  153. return time;
  154. }
  155. public int getNumSets()
  156. {
  157. return sets;
  158. }
  159. public int getScore()
  160. {
  161. return score;
  162. }
  163. public int modScore(int change)
  164. {
  165. score +=change;
  166. return score;
  167. }
  168. public Deck getDeck()
  169. {
  170. return deck;
  171. }
  172. public int getSetsLeft()
  173. {
  174. setsLeft = hand.setsLeft();
  175. return setsLeft;
  176. }
  177. public void setScore(int newScore)
  178. {
  179. score = newScore;
  180. }
  181. private void diselectCards(SetButton[] cards)
  182. {
  183. for(SetButton elem : cards)
  184. {
  185. if(elem.getPressed())
  186. {
  187. elem.setBackground(Color.white);
  188. elem.setPressed(false);
  189. }
  190. }
  191. }
  192. private ArrayList<Card> selected(SetButton[] cards)
  193. {
  194. ArrayList<Card> selectedCards = new ArrayList<Card>();
  195. for(SetButton elem : cards)
  196. {
  197. if(elem.getPressed())
  198. {
  199. selectedCards.add(elem.getCard());
  200. }
  201. }
  202. return selectedCards;
  203. }
  204. }