Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2010
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.91 KB | None | 0 0
  1. import javax.swing.text.AttributeSet;
  2. import javax.swing.text.BadLocationException;
  3. import javax.swing.text.PlainDocument;
  4. import java.util.Random;
  5.  
  6. //Maximum length of 1 char only
  7. class JTextFieldLimit extends PlainDocument {
  8.   private int limit;
  9.   JTextFieldLimit(int limit) {
  10.     super();
  11.     this.limit = limit;
  12.   }
  13.  
  14.   JTextFieldLimit(int limit, boolean upper) {
  15.     super();
  16.     this.limit = limit;
  17.   }
  18.  
  19.   public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
  20.     if (str == null)
  21.       return;
  22.  
  23.     if ((getLength() + str.length()) <= limit) {
  24.       super.insertString(offset, str, attr);
  25.     }
  26.   }
  27. }
  28.  
  29. public class TwoFourSeven extends javax.swing.JApplet {
  30.  
  31.     /** Initializes the applet TwoFourSeven */
  32.     public void init() {
  33.         try {
  34.             java.awt.EventQueue.invokeAndWait(new Runnable() {
  35.                 public void run() {
  36.                     initComponents();
  37.                 }
  38.             });
  39.         } catch (Exception ex) {
  40.             ex.printStackTrace();
  41.         }
  42.     }
  43.  
  44.     /** This method is called from within the init() method to
  45.      * initialize the form.
  46.      * WARNING: Do NOT modify this code. The content of this method is
  47.      * always regenerated by the Form Editor.
  48.      */
  49.     @SuppressWarnings("unchecked")
  50.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  51.     private void initComponents() {
  52.  
  53.         buttonGroup1 = new javax.swing.ButtonGroup();
  54.         jOptionPane1 = new javax.swing.JOptionPane();
  55.         jPanel1 = new javax.swing.JPanel();
  56.         box1 = new javax.swing.JTextField();
  57.         box2 = new javax.swing.JTextField();
  58.         box3 = new javax.swing.JTextField();
  59.         field1 = new javax.swing.JTextField();
  60.         field1.setDocument(new JTextFieldLimit(1));
  61.         takeButton = new javax.swing.JButton();
  62.         jScrollPane2 = new javax.swing.JScrollPane();
  63.         status = new javax.swing.JTextArea();
  64.         radio1 = new javax.swing.JRadioButton();
  65.         radio2 = new javax.swing.JRadioButton();
  66.         radio3 = new javax.swing.JRadioButton();
  67.  
  68.         setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
  69.         setStub(null);
  70.  
  71.         box1.setEditable(false);
  72.         box1.setFont(new java.awt.Font("Tahoma", 1, 48));
  73.         box1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  74.         box1.setText("2");
  75.  
  76.         box2.setEditable(false);
  77.         box2.setFont(new java.awt.Font("Tahoma", 1, 48));
  78.         box2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  79.         box2.setText("4");
  80.  
  81.         box3.setEditable(false);
  82.         box3.setFont(new java.awt.Font("Tahoma", 1, 48));
  83.         box3.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  84.         box3.setText("7");
  85.  
  86.         field1.setFont(new java.awt.Font("Tahoma", 1, 48));
  87.         field1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
  88.         field1.setScrollOffset(1);
  89.  
  90.         takeButton.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  91.         takeButton.setText("Get!");
  92.         takeButton.addActionListener(new java.awt.event.ActionListener() {
  93.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  94.                 takeButtonActionPerformed(evt);
  95.             }
  96.         });
  97.  
  98.         status.setColumns(20);
  99.         status.setFont(new java.awt.Font("Tahoma", 0, 13));
  100.         status.setLineWrap(true);
  101.         status.setRows(5);
  102.         status.setText("Please subtract a number from one box.");
  103.         status.setWrapStyleWord(true);
  104.         jScrollPane2.setViewportView(status);
  105.  
  106.         buttonGroup1.add(radio1);
  107.         radio1.setSelected(true);
  108.         radio1.setText("Box 1");
  109.  
  110.         buttonGroup1.add(radio2);
  111.         radio2.setText("Box 2");
  112.  
  113.         buttonGroup1.add(radio3);
  114.         radio3.setText("Box 3");
  115.  
  116.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  117.         jPanel1.setLayout(jPanel1Layout);
  118.         jPanel1Layout.setHorizontalGroup(
  119.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.             .addGroup(jPanel1Layout.createSequentialGroup()
  121.                 .addContainerGap()
  122.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  123.                     .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.                         .addGroup(jPanel1Layout.createSequentialGroup()
  125.                             .addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  126.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  127.                             .addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  128.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  129.                             .addComponent(box3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  130.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
  131.                         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  132.                             .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  133.                                 .addComponent(radio3)
  134.                                 .addComponent(radio2)
  135.                                 .addComponent(radio1))
  136.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                             .addComponent(field1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  138.                             .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  139.                             .addComponent(takeButton)
  140.                             .addGap(58, 58, 58)))
  141.                     .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE))
  142.                 .addContainerGap())
  143.         );
  144.         jPanel1Layout.setVerticalGroup(
  145.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  146.             .addGroup(jPanel1Layout.createSequentialGroup()
  147.                 .addContainerGap()
  148.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  149.                     .addComponent(box1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  150.                     .addComponent(box2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
  151.                     .addComponent(box3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
  152.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153.                     .addGroup(jPanel1Layout.createSequentialGroup()
  154.                         .addComponent(radio1)
  155.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  156.                         .addComponent(radio2)
  157.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  158.                         .addComponent(radio3))
  159.                     .addGroup(jPanel1Layout.createSequentialGroup()
  160.                         .addGap(11, 11, 11)
  161.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  162.                             .addComponent(takeButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  163.                             .addComponent(field1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
  164.                 .addGap(18, 18, 18)
  165.                 .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  166.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  167.         );
  168.  
  169.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  170.         getContentPane().setLayout(layout);
  171.         layout.setHorizontalGroup(
  172.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173.             .addGroup(layout.createSequentialGroup()
  174.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  175.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  176.         );
  177.         layout.setVerticalGroup(
  178.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  179.             .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  180.         );
  181.     }// </editor-fold>                        
  182.  
  183.     Random rand = new Random();
  184.     boolean set1Empty = false, set2Empty = false, set3Empty = false,
  185.             set1Selected = false, set2Selected = false, set3Selected = false;
  186.     int set1 = 2, set2 = 4, set3 = 7, whichSetRand, howManyRand,
  187.             update1, update2, update3, aiUpdate1, aiUpdate2, aiUpdate3;
  188.     int[] take = {0, 0, 0};
  189.  
  190.     private void takeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
  191.         resetTake();
  192.  
  193.         int selected = checkWhichSelected();
  194.  
  195.         if(checkIfZero()) {
  196.             status.setText("You can\'t subtract from a number which is already zero");
  197.             return;
  198.         }
  199.  
  200.         if(checkIfEmpty()) {
  201.             return;
  202.         } else {
  203.             loadTakeVars(selected, take);
  204.         }
  205.  
  206.         if (checkIfOver())
  207.             return;
  208.        
  209.         updateSets(take);
  210.         updateBoxText();
  211.         resetFieldText();
  212.         updateSetEmpty();
  213.         if(checkIfWon()) {
  214.             status.setText("You have won! Play again!");
  215.             return;
  216.         }
  217.  
  218.         AI();
  219.         if(checkIfWon()) {
  220.             status.setText("From your move of " + update1 + "-" + update2 +
  221.                     "-" + update3 + ", the computer turned it into " + aiUpdate1 +
  222.                     "-" + aiUpdate2 + "-" + aiUpdate3 +". The computer has won! Play again!");
  223.             return;
  224.         }
  225. }                                          
  226.  
  227.         public void AI() {            
  228.             AIUpdate();
  229.             AISelect();
  230.             AITake();
  231.             updateSetEmpty();
  232.             AIResetSelected();
  233.             updateBoxText();
  234.  
  235.             status.setText("From your move of " + update1 + "-" + update2 +
  236.                     "-" + update3 + ", the computer turned it into " + set1 +
  237.                     "-" + set2 + "-" + set3);
  238.         }
  239.  
  240.         public void AIUpdate() {
  241.             update1 = set1;
  242.             update2 = set2;
  243.             update3 = set3;
  244.         }
  245.  
  246.         public void AISelect() {
  247.             if(!set1Empty && !set2Empty && !set3Empty) {
  248.                 whichSetRand = rand.nextInt(3) + 1;
  249.                 switch(whichSetRand) {
  250.                     case 1: set1Selected = true; break;
  251.                     case 2: set2Selected = true; break;
  252.                     case 3: set3Selected = true; break;
  253.                 }
  254.             } else if(!set1Empty && !set2Empty && set3Empty) {
  255.                 whichSetRand = rand.nextInt(2) + 1;
  256.                 if(whichSetRand == 1) {
  257.                     set1Selected = true;
  258.                 } else {
  259.                     set2Selected = true;
  260.                 }
  261.             } else if(!set1Empty && set2Empty && !set3Empty) {
  262.                 whichSetRand = rand.nextInt(2) + 1;
  263.                 if(whichSetRand == 1) {
  264.                     set1Selected = true;
  265.                 } else {
  266.                     set3Selected = true;
  267.                 }
  268.             } else if(set1Empty && !set2Empty && !set3Empty) {
  269.                 whichSetRand = rand.nextInt(2) + 1;
  270.                 if(whichSetRand == 1) {
  271.                     set2Selected = true;
  272.                 } else {
  273.                     set3Selected = true;
  274.                 }
  275.             } else if(!set1Empty && set2Empty && set3Empty) {
  276.                 set1Selected = true;
  277.             } else if(set1Empty && !set2Empty && set3Empty) {
  278.                 set2Selected = true;
  279.             } else if(set1Empty && set2Empty && !set3Empty) {
  280.                 set3Selected = true;
  281.             }
  282.         }
  283.  
  284.         public void AITake() {
  285.             if(set1Selected) {
  286.                 if(set2Empty && set3Empty) {
  287.                     howManyRand = set1 - 1;
  288.                 } else {
  289.                     howManyRand = rand.nextInt(set1) + 1;
  290.                 }
  291.                 set1 -= howManyRand;
  292.             } else if(set2Selected) {
  293.                 if(set1Empty && set3Empty) {
  294.                     howManyRand = set2 - 1;
  295.                 } else {
  296.                     howManyRand = rand.nextInt(set2) + 1;
  297.                 }
  298.                 set2 -= howManyRand;
  299.             } else if(set3Selected) {
  300.                 if(set1Empty && set2Empty) {
  301.                     howManyRand = set3 - 1;
  302.                 } else {
  303.                     howManyRand = rand.nextInt(set3) + 1;
  304.                 }
  305.                 set3 -= howManyRand;
  306.             }
  307.         }
  308.  
  309.         public void AIResetSelected() {
  310.             set1Selected = false;
  311.             set2Selected = false;
  312.             set3Selected = false;
  313.         }
  314.  
  315.         public void resetTake() {
  316.             take[0] = 0;
  317.             take[1] = 0;
  318.             take[2] = 0;
  319.         }
  320.  
  321.         public int checkWhichSelected() {
  322.             if(radio1.isSelected()) {
  323.                 return 1;
  324.             } else if(radio2.isSelected()) {
  325.                 return 2;
  326.             } else if(radio3.isSelected()) {
  327.                 return 3;
  328.             }
  329.             return 1;
  330.         }
  331.  
  332.         public boolean checkIfZero() {
  333.             if(radio1.isSelected()) {
  334.                 if(set1Empty) {
  335.                     return true;
  336.                 }
  337.             } else if(radio2.isSelected()) {
  338.                 if(set2Empty) {
  339.                     return true;
  340.                 }
  341.             } else if(radio3.isSelected()) {
  342.                 if(set3Empty) {
  343.                     return true;
  344.                 }
  345.             }
  346.  
  347.             return false;
  348.         }
  349.  
  350.         public boolean checkIfEmpty() {
  351.             if(field1.getText().equals("")) {
  352.                     status.setText("Please subtract atleast 1 from a number");
  353.                     return true;
  354.             }
  355.             return false;
  356.         }
  357.  
  358.         public boolean checkIfOver() {
  359.             if((set2 == 0 && set3 == 0) && (set1-take[0] < 1) ||
  360.                 (set1 == 0 && set3 == 0) && (set2-take[1] < 1) ||
  361.                 (set1 == 0 && set2 == 0) && (set3-take[2] < 1)) {
  362.                 status.setText("You must leave atleast 1 in order to win");
  363.                 return true;
  364.             }
  365.             return false;
  366.         }
  367.  
  368.         public void loadTakeVars(int i, int[] take) {
  369.             switch(i) {
  370.                 case 1:
  371.                     take[0] = Integer.parseInt(field1.getText());
  372.                     break;
  373.                 case 2:
  374.                     take[1] = Integer.parseInt(field1.getText());
  375.                     break;
  376.                 case 3:
  377.                     take[2] = Integer.parseInt(field1.getText());
  378.                     break;
  379.             }
  380.         }
  381.  
  382.         public void updateSets(int[] take) {
  383.             set1 -= take[0];
  384.             set2 -= take[1];
  385.             set3 -= take[2];
  386.  
  387.             if(set1 < 0)
  388.                 set1 = 0;
  389.             if(set2 < 0)
  390.                 set2 = 0;
  391.             if(set3 < 0)
  392.                 set3 = 0;
  393.         }
  394.  
  395.         public void updateBoxText() {
  396.             box1.setText(Integer.toString(set1));
  397.             box2.setText(Integer.toString(set2));
  398.             box3.setText(Integer.toString(set3));
  399.         }
  400.  
  401.         public void resetFieldText() {
  402.             field1.setText("");
  403.         }
  404.  
  405.         public void updateSetEmpty() {
  406.             if(set1 == 0)
  407.                 set1Empty = true;
  408.             if(set2 == 0)
  409.                 set2Empty = true;
  410.             if(set3 == 0)
  411.                 set3Empty = true;
  412.         }
  413.  
  414.         public boolean checkIfWon() {
  415.             if((set1 == 1 && set2 == 0 && set3 == 0) ||
  416.                 (set1 == 0 && set2 == 1 && set3 == 0) ||
  417.                 (set1 == 0 && set2 == 0 && set3 == 1)) {
  418.                 aiUpdate1 = set1;
  419.                 aiUpdate2 = set2;
  420.                 aiUpdate3 = set3;
  421.                 set1 = 2;
  422.                 set2 = 4;
  423.                 set3 = 7;
  424.                 set1Empty = false;
  425.                 set2Empty = false;
  426.                 set3Empty = false;
  427.                 box1.setText(Integer.toString(set1));
  428.                 box2.setText(Integer.toString(set2));
  429.                 box3.setText(Integer.toString(set3));
  430.  
  431.                 return true;
  432.             }
  433.  
  434.             return false;
  435.         }
  436.    
  437.  
  438.  
  439.     // Variables declaration - do not modify                    
  440.     private javax.swing.JTextField box1;
  441.     private javax.swing.JTextField box2;
  442.     private javax.swing.JTextField box3;
  443.     private javax.swing.ButtonGroup buttonGroup1;
  444.     private javax.swing.JTextField field1;
  445.     private javax.swing.JOptionPane jOptionPane1;
  446.     private javax.swing.JPanel jPanel1;
  447.     private javax.swing.JScrollPane jScrollPane2;
  448.     private javax.swing.JRadioButton radio1;
  449.     private javax.swing.JRadioButton radio2;
  450.     private javax.swing.JRadioButton radio3;
  451.     private javax.swing.JTextArea status;
  452.     private javax.swing.JButton takeButton;
  453.     // End of variables declaration                  
  454.  
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement