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.

264 lines
11 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. package net.jrtechs.setgame;
  2. import javax.swing.JOptionPane;
  3. import java.awt.event.*;
  4. import javax.swing.Timer;
  5. /*
  6. jeffery R
  7. 12-26-15
  8. gui component for set
  9. */
  10. public class SetGame extends javax.swing.JFrame implements ActionListener
  11. {
  12. private SetButton[] cards = new SetButton[15];
  13. private Game game;
  14. public SetGame()
  15. {
  16. initComponents();
  17. initSetButtons();
  18. showButtons(false);
  19. cmdFindSet.setEnabled(false);
  20. cmdOpen3.setEnabled(false);
  21. //creates an event listener for the timer in the game class
  22. ActionListener actTimer = new ActionListener()
  23. {
  24. public void actionPerformed(ActionEvent e)
  25. {
  26. if(game != null)
  27. {
  28. game.timerTic();
  29. lblTime.setText("Time " + game.getTime());
  30. }
  31. }
  32. };
  33. Timer timer = new Timer(1000, actTimer);
  34. timer.start();
  35. }
  36. public void showButtons(Boolean set)
  37. {
  38. for(int i = 0; i < 12; i++)
  39. {
  40. cards[i].setVisible(set);
  41. }
  42. }
  43. private void initSetButtons()
  44. {
  45. //fills the array with new SetButtons and ands action listeners
  46. for(int i = 0; i < cards.length; i++)
  47. {
  48. cards[i] = new SetButton(i);
  49. cards[i].addActionListener(this);
  50. }
  51. //sets the location of each of the buttons on the absolute layout
  52. getContentPane().add(cards[0], new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 11, 146, 84));
  53. getContentPane().add(cards[1], new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 11, 146, 84));
  54. getContentPane().add(cards[2], new org.netbeans.lib.awtextra.AbsoluteConstraints(336, 11, 146, 84));
  55. getContentPane().add(cards[3], new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 11, 146, 84));
  56. getContentPane().add(cards[4], new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 121, 146, 84));
  57. getContentPane().add(cards[5], new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 121, 146, 84));
  58. getContentPane().add(cards[6], new org.netbeans.lib.awtextra.AbsoluteConstraints(336, 121, 146, 84));
  59. getContentPane().add(cards[7], new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 121, 146, 84));
  60. getContentPane().add(cards[8], new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 233, 146, 84));
  61. getContentPane().add(cards[9], new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 233, 146, 84));
  62. getContentPane().add(cards[10], new org.netbeans.lib.awtextra.AbsoluteConstraints(336, 233, 146, 84));
  63. getContentPane().add(cards[11], new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 233, 146, 84));
  64. getContentPane().add(cards[12], new org.netbeans.lib.awtextra.AbsoluteConstraints(664, 11, 146, 84));
  65. getContentPane().add(cards[13], new org.netbeans.lib.awtextra.AbsoluteConstraints(664, 121, 146, 84));
  66. getContentPane().add(cards[14], new org.netbeans.lib.awtextra.AbsoluteConstraints(664, 233, 146, 84));
  67. for(int i = 12; i < 15; i++)
  68. {
  69. cards[i].setVisible(false);
  70. }
  71. }
  72. public void actionPerformed(ActionEvent e)
  73. {
  74. //casts the object of e to a setButton then calls the getLoc to find its index location in the cards "setButtons" array
  75. SetButton clicked = (SetButton)e.getSource();
  76. cards[clicked.getLoc()].clicked();
  77. game.clicked(cards);
  78. updateBoard();
  79. }
  80. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  81. private void initComponents() {
  82. cmdPlay = new javax.swing.JButton();
  83. lblScore = new javax.swing.JLabel();
  84. lblSets = new javax.swing.JLabel();
  85. lblCardsLeft = new javax.swing.JLabel();
  86. lblSetsOnBoard = new javax.swing.JLabel();
  87. cmdExit = new javax.swing.JButton();
  88. cmdFindSet = new javax.swing.JButton();
  89. cmdOpen3 = new javax.swing.JButton();
  90. lblTime = new javax.swing.JLabel();
  91. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  92. setTitle("Set Game");
  93. setPreferredSize(new java.awt.Dimension(670, 500));
  94. getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  95. cmdPlay.setText("Play");
  96. cmdPlay.addActionListener(new java.awt.event.ActionListener() {
  97. public void actionPerformed(java.awt.event.ActionEvent evt) {
  98. cmdPlayActionPerformed(evt);
  99. }
  100. });
  101. getContentPane().add(cmdPlay, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 343, 95, 38));
  102. lblScore.setText("Score");
  103. getContentPane().add(lblScore, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 350, -1, -1));
  104. lblSets.setText("Sets");
  105. getContentPane().add(lblSets, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 350, -1, -1));
  106. lblCardsLeft.setText("Cards Left in Deck");
  107. getContentPane().add(lblCardsLeft, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 380, -1, -1));
  108. lblSetsOnBoard.setText("Sets on board");
  109. getContentPane().add(lblSetsOnBoard, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 380, -1, -1));
  110. cmdExit.setText("Exit");
  111. cmdExit.addActionListener(new java.awt.event.ActionListener() {
  112. public void actionPerformed(java.awt.event.ActionEvent evt) {
  113. cmdExitActionPerformed(evt);
  114. }
  115. });
  116. getContentPane().add(cmdExit, new org.netbeans.lib.awtextra.AbsoluteConstraints(124, 400, 95, 38));
  117. cmdFindSet.setText("Find a Set");
  118. cmdFindSet.setActionCommand("cmdFindaSet");
  119. cmdFindSet.addActionListener(new java.awt.event.ActionListener() {
  120. public void actionPerformed(java.awt.event.ActionEvent evt) {
  121. cmdFindSetActionPerformed(evt);
  122. }
  123. });
  124. getContentPane().add(cmdFindSet, new org.netbeans.lib.awtextra.AbsoluteConstraints(124, 343, 95, 40));
  125. cmdOpen3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
  126. cmdOpen3.setText("Open 3 Cards");
  127. cmdOpen3.setToolTipText("");
  128. cmdOpen3.addActionListener(new java.awt.event.ActionListener() {
  129. public void actionPerformed(java.awt.event.ActionEvent evt) {
  130. cmdOpen3ActionPerformed(evt);
  131. }
  132. });
  133. getContentPane().add(cmdOpen3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 400, 95, 38));
  134. lblTime.setText("Time");
  135. getContentPane().add(lblTime, new org.netbeans.lib.awtextra.AbsoluteConstraints(260, 410, -1, -1));
  136. pack();
  137. }// </editor-fold>//GEN-END:initComponents
  138. private void cmdPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPlayActionPerformed
  139. cmdFindSet.setEnabled(true);
  140. cmdOpen3.setEnabled(true);
  141. game = new Game();
  142. showButtons(true);
  143. for(int i = 12; i < 15; i++)
  144. {
  145. cards[i].setVisible(false);
  146. }
  147. cmdPlay.setText("New Game");
  148. game.updateBoard(cards);
  149. this.updateBoard();
  150. }//GEN-LAST:event_cmdPlayActionPerformed
  151. private void updateBoard()
  152. {
  153. lblScore.setText("Score " + game.getScore());
  154. lblSets.setText("Sets " + game.getNumSets());
  155. lblCardsLeft.setText("Cards Left in Deck " + game.getDeck().cardsLeft());
  156. lblSetsOnBoard.setText("Sets on board " + game.getSetsLeft());
  157. if(game.getSetsLeft() == 0)
  158. {
  159. cmdFindSet.setEnabled(false);
  160. endGame();
  161. }
  162. else if(game.getHand().length==12 && game.getDeck().cardsLeft() > 2)
  163. {
  164. cmdOpen3.setEnabled(true);
  165. }
  166. }
  167. private void cmdExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdExitActionPerformed
  168. System.exit(0);
  169. }//GEN-LAST:event_cmdExitActionPerformed
  170. private void endGame()
  171. {
  172. if(game.getHand().length ==12 && game.getDeck().cardsLeft() > 3)
  173. {
  174. JOptionPane.showMessageDialog(null,"There are no more sets left on the board. However, you can still open three cards.","",JOptionPane.WARNING_MESSAGE);
  175. }
  176. else
  177. {
  178. game = null;
  179. cmdOpen3.setEnabled(false);
  180. JOptionPane.showMessageDialog(null,"There are no more sets left on the board.","End of Game",JOptionPane.WARNING_MESSAGE);
  181. }
  182. }
  183. private void cmdFindSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdFindSetActionPerformed
  184. game.findSet(cards);
  185. lblScore.setText("Score " + game.modScore(-150));
  186. }//GEN-LAST:event_cmdFindSetActionPerformed
  187. private void cmdOpen3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdOpen3ActionPerformed
  188. game.openThree(cards);
  189. cmdOpen3.setEnabled(false);
  190. updateBoard();
  191. this.setBounds(0,0, 830, 480);
  192. if(game.getSetsLeft() == 0)
  193. {
  194. //end of game
  195. endGame();
  196. }
  197. else
  198. {
  199. //keep on playing game
  200. cmdFindSet.setEnabled(true);
  201. }
  202. }//GEN-LAST:event_cmdOpen3ActionPerformed
  203. public static void main(String args[]) {
  204. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  205. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  206. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  207. */
  208. try {
  209. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  210. if ("Nimbus".equals(info.getName())) {
  211. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  212. break;
  213. }
  214. }
  215. } catch (ClassNotFoundException ex) {
  216. java.util.logging.Logger.getLogger(SetGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  217. } catch (InstantiationException ex) {
  218. java.util.logging.Logger.getLogger(SetGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  219. } catch (IllegalAccessException ex) {
  220. java.util.logging.Logger.getLogger(SetGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  221. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  222. java.util.logging.Logger.getLogger(SetGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  223. }
  224. //</editor-fold>
  225. java.awt.EventQueue.invokeLater(new Runnable() {
  226. public void run() {
  227. new SetGame().setVisible(true);
  228. }
  229. });
  230. }
  231. // Variables declaration - do not modify//GEN-BEGIN:variables
  232. private javax.swing.JButton cmdExit;
  233. private javax.swing.JButton cmdFindSet;
  234. private javax.swing.JButton cmdOpen3;
  235. private javax.swing.JButton cmdPlay;
  236. private javax.swing.JLabel lblCardsLeft;
  237. private javax.swing.JLabel lblScore;
  238. private javax.swing.JLabel lblSets;
  239. private javax.swing.JLabel lblSetsOnBoard;
  240. private javax.swing.JLabel lblTime;
  241. // End of variables declaration//GEN-END:variables
  242. }