Advertisement
Gireada

TICTACTOE

Dec 6th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.12 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tictac2;
  7.  
  8. import java.awt.Button;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import javax.swing.JButton;
  12. import javax.swing.Timer;
  13.  
  14. /**
  15.  *
  16.  * @author beststudent
  17.  */
  18. public class NewJFrame extends javax.swing.JFrame {
  19.  
  20.     /**
  21.      * Creates new form NewJFrame
  22.      */
  23.     public NewJFrame() {
  24.         initComponents();
  25.     }
  26.  
  27.     /**
  28.      * This method is called from within the constructor to initialize the form.
  29.      * WARNING: Do NOT modify this code. The content of this method is always
  30.      * regenerated by the Form Editor.
  31.      */
  32.     @SuppressWarnings("unchecked")
  33.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  34.     private void initComponents() {
  35.  
  36.         jButton1 = new javax.swing.JButton();
  37.         jButton2 = new javax.swing.JButton();
  38.         jButton3 = new javax.swing.JButton();
  39.         jButton4 = new javax.swing.JButton();
  40.         jButton5 = new javax.swing.JButton();
  41.         jButton6 = new javax.swing.JButton();
  42.         jButton7 = new javax.swing.JButton();
  43.         jButton8 = new javax.swing.JButton();
  44.         jButton9 = new javax.swing.JButton();
  45.         jLabel1 = new javax.swing.JLabel();
  46.         jButton10 = new javax.swing.JButton();
  47.         jLabel2 = new javax.swing.JLabel();
  48.  
  49.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  50.  
  51.         jButton1.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  52.         jButton1.setToolTipText("");
  53.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  54.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  55.                 jButton1ActionPerformed(evt);
  56.             }
  57.         });
  58.  
  59.         jButton2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  60.         jButton2.setToolTipText("");
  61.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  62.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  63.                 jButton2ActionPerformed(evt);
  64.             }
  65.         });
  66.  
  67.         jButton3.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  68.         jButton3.addActionListener(new java.awt.event.ActionListener() {
  69.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  70.                 jButton3ActionPerformed(evt);
  71.             }
  72.         });
  73.  
  74.         jButton4.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  75.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  76.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  77.                 jButton4ActionPerformed(evt);
  78.             }
  79.         });
  80.  
  81.         jButton5.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  82.         jButton5.addActionListener(new java.awt.event.ActionListener() {
  83.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  84.                 jButton5ActionPerformed(evt);
  85.             }
  86.         });
  87.  
  88.         jButton6.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  89.         jButton6.addActionListener(new java.awt.event.ActionListener() {
  90.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  91.                 jButton6ActionPerformed(evt);
  92.             }
  93.         });
  94.  
  95.         jButton7.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  96.         jButton7.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 jButton7ActionPerformed(evt);
  99.             }
  100.         });
  101.  
  102.         jButton8.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  103.         jButton8.addActionListener(new java.awt.event.ActionListener() {
  104.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  105.                 jButton8ActionPerformed(evt);
  106.             }
  107.         });
  108.  
  109.         jButton9.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  110.         jButton9.setToolTipText("");
  111.         jButton9.addActionListener(new java.awt.event.ActionListener() {
  112.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  113.                 jButton9ActionPerformed(evt);
  114.             }
  115.         });
  116.  
  117.         jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
  118.         jLabel1.setText("TIC TAC TOE");
  119.  
  120.         jButton10.setText("RESTART");
  121.         jButton10.setToolTipText("");
  122.         jButton10.addActionListener(new java.awt.event.ActionListener() {
  123.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  124.                 jButton10ActionPerformed(evt);
  125.             }
  126.         });
  127.  
  128.         jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
  129.         jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  130.         jLabel2.setText("Castigator");
  131.  
  132.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  133.         getContentPane().setLayout(layout);
  134.         layout.setHorizontalGroup(
  135.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.             .addGroup(layout.createSequentialGroup()
  137.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  138.                     .addGroup(layout.createSequentialGroup()
  139.                         .addContainerGap()
  140.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  141.                             .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  142.                             .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  143.                             .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
  144.                         .addGap(18, 18, 18)
  145.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  146.                             .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  147.                             .addComponent(jLabel1))
  148.                         .addGap(18, 18, 18)
  149.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  150.                             .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  151.                             .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
  152.                             .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
  153.                     .addGroup(layout.createSequentialGroup()
  154.                         .addGap(145, 145, 145)
  155.                         .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
  156.                     .addGroup(layout.createSequentialGroup()
  157.                         .addGap(145, 145, 145)
  158.                         .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
  159.                     .addGroup(layout.createSequentialGroup()
  160.                         .addGap(12, 12, 12)
  161.                         .addComponent(jButton10)
  162.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  163.                         .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
  164.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  165.         );
  166.         layout.setVerticalGroup(
  167.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.             .addGroup(layout.createSequentialGroup()
  169.                 .addGap(21, 21, 21)
  170.                 .addComponent(jLabel1)
  171.                 .addGap(18, 18, 18)
  172.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  173.                     .addGroup(layout.createSequentialGroup()
  174.                         .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  175.                         .addGap(24, 24, 24)
  176.                         .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  177.                         .addGap(24, 24, 24)
  178.                         .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
  179.                     .addGroup(layout.createSequentialGroup()
  180.                         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  181.                         .addGap(24, 24, 24)
  182.                         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                         .addGap(24, 24, 24)
  184.                         .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
  185.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  186.                         .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  187.                         .addGap(24, 24, 24)
  188.                         .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
  189.                         .addGap(24, 24, 24)
  190.                         .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
  191.                 .addGap(31, 31, 31)
  192.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  193.                     .addGroup(layout.createSequentialGroup()
  194.                         .addComponent(jButton10)
  195.                         .addGap(0, 30, Short.MAX_VALUE))
  196.                     .addGroup(layout.createSequentialGroup()
  197.                         .addGap(6, 6, 6)
  198.                         .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  199.                 .addContainerGap())
  200.         );
  201.  
  202.         pack();
  203.     }// </editor-fold>                        
  204.     int player_turn = 1;
  205.     boolean job_final = false;
  206.     boolean first_button_click = false;  
  207.     int click = 0;
  208.     public int GetCurrentTurn()
  209.     {
  210.         return player_turn;
  211.     }
  212.     public String GetCurrentSymbol(int turn)
  213.     {
  214.         String text = null;
  215.         if(turn == 1) text = "X";
  216.         else text = "0";
  217.  
  218.         return text;
  219.     }
  220.     public void ChangePlayerTurn()
  221.     {
  222.         if(player_turn == 1)
  223.             player_turn = 2;
  224.         else
  225.             player_turn = 1;
  226.     }
  227.     public void ChangeButtonText(JButton button)
  228.     {  
  229.         if(job_final == false) {
  230.             if(!(button.getText().matches("X")) && !(button.getText().matches("0")))
  231.             {
  232.                 button.setText(GetCurrentSymbol(GetCurrentTurn()));
  233.                 ChangePlayerTurn();
  234.                 CheckForWin();
  235.                 click++;
  236.                 CheckEndGame();
  237.             }
  238.         }
  239.     }
  240.     public void DeclareWin(JButton button)
  241.     {
  242.         job_final = true;
  243.         String winner = button.getText();
  244.         String winner_text = "Castigatorul este " + winner;
  245.         jLabel2.setText(winner_text);
  246.     }
  247.     public void RestartGame()
  248.     {
  249.         job_final = false;
  250.         first_button_click = false;
  251.         click = 0;
  252.         player_turn = 1;
  253.         jButton1.setText(" ");
  254.         jButton2.setText(" ");
  255.         jButton3.setText(" ");
  256.         jButton4.setText(" ");
  257.         jButton5.setText(" ");
  258.         jButton6.setText(" ");
  259.         jButton7.setText(" ");
  260.         jButton8.setText(" ");
  261.         jButton9.setText(" ");
  262.         jLabel2.setText("Castigator");
  263.     }
  264.     public void CheckForWin()
  265.     {
  266.         String a1, a2, a3, a4, a5, a6, a7, a8, a9;
  267.         a1 = jButton1.getText();
  268.         a2 = jButton2.getText();
  269.         a3 = jButton3.getText();
  270.         a4 = jButton4.getText();
  271.         a5 = jButton5.getText();
  272.         a6 = jButton6.getText();
  273.         a7 = jButton7.getText();
  274.         a8 = jButton8.getText();
  275.         a9 = jButton9.getText();
  276.        
  277.         if((a1.matches("X") && (a2.matches("X")) && (a3.matches("X"))))
  278.             DeclareWin(jButton1);
  279.         if((a1.matches("0") && (a2.matches("0")) && (a3.matches("0"))))
  280.             DeclareWin(jButton1);
  281.        
  282.         if((a4.matches("X") && (a5.matches("X")) && (a6.matches("X"))))
  283.             DeclareWin(jButton4);
  284.         if((a4.matches("0") && (a5.matches("0")) && (a6.matches("0"))))
  285.             DeclareWin(jButton4);
  286.        
  287.         if((a7.matches("X") && (a8.matches("X")) && (a9.matches("X"))))
  288.             DeclareWin(jButton7);
  289.         if((a7.matches("0") && (a8.matches("0")) && (a9.matches("0"))))
  290.             DeclareWin(jButton7);
  291.        
  292.         if((a1.matches("X") && (a4.matches("X")) && (a7.matches("X"))))
  293.             DeclareWin(jButton1);
  294.         if((a1.matches("0") && (a4.matches("0")) && (a7.matches("0"))))
  295.             DeclareWin(jButton1);
  296.        
  297.         if((a2.matches("X") && (a5.matches("X")) && (a8.matches("X"))))
  298.             DeclareWin(jButton2);
  299.         if((a2.matches("0") && (a5.matches("0")) && (a8.matches("0"))))
  300.             DeclareWin(jButton2);
  301.        
  302.         if((a3.matches("X") && (a6.matches("X")) && (a9.matches("X"))))
  303.             DeclareWin(jButton3);
  304.         if((a3.matches("0") && (a6.matches("0")) && (a9.matches("0"))))
  305.             DeclareWin(jButton3);
  306.        
  307.         if((a1.matches("X") && (a5.matches("X")) && (a9.matches("X"))))
  308.             DeclareWin(jButton1);
  309.         if((a1.matches("0") && (a5.matches("0")) && (a9.matches("0"))))
  310.             DeclareWin(jButton1);
  311.        
  312.         if((a3.matches("X") && (a5.matches("X")) && (a7.matches("X"))))
  313.             DeclareWin(jButton3);
  314.         if((a3.matches("0") && (a5.matches("0")) && (a7.matches("0"))))
  315.             DeclareWin(jButton3);
  316.        
  317.     }
  318.     public void CheckEndGame()
  319.     {
  320.         if(click >= 9)
  321.             EndGame();
  322.     }
  323.     public void EndGame()
  324.     {
  325.         job_final = false;
  326.         first_button_click = false;
  327.         click = 0;
  328.         player_turn = 1;
  329.         jButton1.setText(" ");
  330.         jButton2.setText(" ");
  331.         jButton3.setText(" ");
  332.         jButton4.setText(" ");
  333.         jButton5.setText(" ");
  334.         jButton6.setText(" ");
  335.         jButton7.setText(" ");
  336.         jButton8.setText(" ");
  337.         jButton9.setText(" ");
  338.         jLabel2.setText("Remiza");
  339.     }
  340.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  341.        
  342.         ChangeButtonText(jButton1);
  343.     }                                        
  344.  
  345.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  346.         ChangeButtonText(jButton4);
  347.     }                                        
  348.  
  349.     private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  350.         ChangeButtonText(jButton7);
  351.     }                                        
  352.  
  353.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  354.         ChangeButtonText(jButton2);
  355.     }                                        
  356.  
  357.     private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  358.         ChangeButtonText(jButton6);
  359.     }                                        
  360.  
  361.     private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  362.         ChangeButtonText(jButton8);
  363.     }                                        
  364.  
  365.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  366.         ChangeButtonText(jButton3);
  367.     }                                        
  368.  
  369.     private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  370.         ChangeButtonText(jButton5);
  371.     }                                        
  372.  
  373.     private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  374.         ChangeButtonText(jButton9);
  375.     }                                        
  376.  
  377.     private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  378.         RestartGame();
  379.     }                                        
  380.  
  381.     /**
  382.      * @param args the command line arguments
  383.      */
  384.     public static void main(String args[]) {
  385.         /* Set the Nimbus look and feel */
  386.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  387.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  388.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  389.          */
  390.         try {
  391.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  392.                 if ("Nimbus".equals(info.getName())) {
  393.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  394.                     break;
  395.                 }
  396.             }
  397.         } catch (ClassNotFoundException ex) {
  398.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  399.         } catch (InstantiationException ex) {
  400.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  401.         } catch (IllegalAccessException ex) {
  402.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  403.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  404.             java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  405.         }
  406.         //</editor-fold>
  407.  
  408.         /* Create and display the form */
  409.         java.awt.EventQueue.invokeLater(new Runnable() {
  410.             public void run() {
  411.                 new NewJFrame().setVisible(true);
  412.             }
  413.         });
  414.     }
  415.  
  416.     // Variables declaration - do not modify                    
  417.     private javax.swing.JButton jButton1;
  418.     private javax.swing.JButton jButton10;
  419.     private javax.swing.JButton jButton2;
  420.     private javax.swing.JButton jButton3;
  421.     private javax.swing.JButton jButton4;
  422.     private javax.swing.JButton jButton5;
  423.     private javax.swing.JButton jButton6;
  424.     private javax.swing.JButton jButton7;
  425.     private javax.swing.JButton jButton8;
  426.     private javax.swing.JButton jButton9;
  427.     private javax.swing.JLabel jLabel1;
  428.     private javax.swing.JLabel jLabel2;
  429.     // End of variables declaration                  
  430.  
  431.  
  432.  
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement