Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tictac2;
- import java.awt.Button;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.Timer;
- /**
- *
- * @author beststudent
- */
- public class NewJFrame extends javax.swing.JFrame {
- /**
- * Creates new form NewJFrame
- */
- public NewJFrame() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jButton1 = new javax.swing.JButton();
- jButton2 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jButton5 = new javax.swing.JButton();
- jButton6 = new javax.swing.JButton();
- jButton7 = new javax.swing.JButton();
- jButton8 = new javax.swing.JButton();
- jButton9 = new javax.swing.JButton();
- jLabel1 = new javax.swing.JLabel();
- jButton10 = new javax.swing.JButton();
- jLabel2 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- jButton1.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton1.setToolTipText("");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jButton2.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton2.setToolTipText("");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jButton3.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jButton4.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jButton5.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton5ActionPerformed(evt);
- }
- });
- jButton6.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton6.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton6ActionPerformed(evt);
- }
- });
- jButton7.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton7.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton7ActionPerformed(evt);
- }
- });
- jButton8.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton8.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton8ActionPerformed(evt);
- }
- });
- jButton9.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jButton9.setToolTipText("");
- jButton9.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton9ActionPerformed(evt);
- }
- });
- jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 18)); // NOI18N
- jLabel1.setText("TIC TAC TOE");
- jButton10.setText("RESTART");
- jButton10.setToolTipText("");
- jButton10.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton10ActionPerformed(evt);
- }
- });
- jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
- jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
- jLabel2.setText("Castigator");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel1))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGroup(layout.createSequentialGroup()
- .addGap(145, 145, 145)
- .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(145, 145, 145)
- .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(12, 12, 12)
- .addComponent(jButton10)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(21, 21, 21)
- .addComponent(jLabel1)
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(24, 24, 24)
- .addComponent(jButton9, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
- .addGap(31, 31, 31)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jButton10)
- .addGap(0, 30, Short.MAX_VALUE))
- .addGroup(layout.createSequentialGroup()
- .addGap(6, 6, 6)
- .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- int player_turn = 1;
- boolean job_final = false;
- boolean first_button_click = false;
- int click = 0;
- public int GetCurrentTurn()
- {
- return player_turn;
- }
- public String GetCurrentSymbol(int turn)
- {
- String text = null;
- if(turn == 1) text = "X";
- else text = "0";
- return text;
- }
- public void ChangePlayerTurn()
- {
- if(player_turn == 1)
- player_turn = 2;
- else
- player_turn = 1;
- }
- public void ChangeButtonText(JButton button)
- {
- if(job_final == false) {
- if(!(button.getText().matches("X")) && !(button.getText().matches("0")))
- {
- button.setText(GetCurrentSymbol(GetCurrentTurn()));
- ChangePlayerTurn();
- CheckForWin();
- click++;
- CheckEndGame();
- }
- }
- }
- public void DeclareWin(JButton button)
- {
- job_final = true;
- String winner = button.getText();
- String winner_text = "Castigatorul este " + winner;
- jLabel2.setText(winner_text);
- }
- public void RestartGame()
- {
- job_final = false;
- first_button_click = false;
- click = 0;
- player_turn = 1;
- jButton1.setText(" ");
- jButton2.setText(" ");
- jButton3.setText(" ");
- jButton4.setText(" ");
- jButton5.setText(" ");
- jButton6.setText(" ");
- jButton7.setText(" ");
- jButton8.setText(" ");
- jButton9.setText(" ");
- jLabel2.setText("Castigator");
- }
- public void CheckForWin()
- {
- String a1, a2, a3, a4, a5, a6, a7, a8, a9;
- a1 = jButton1.getText();
- a2 = jButton2.getText();
- a3 = jButton3.getText();
- a4 = jButton4.getText();
- a5 = jButton5.getText();
- a6 = jButton6.getText();
- a7 = jButton7.getText();
- a8 = jButton8.getText();
- a9 = jButton9.getText();
- if((a1.matches("X") && (a2.matches("X")) && (a3.matches("X"))))
- DeclareWin(jButton1);
- if((a1.matches("0") && (a2.matches("0")) && (a3.matches("0"))))
- DeclareWin(jButton1);
- if((a4.matches("X") && (a5.matches("X")) && (a6.matches("X"))))
- DeclareWin(jButton4);
- if((a4.matches("0") && (a5.matches("0")) && (a6.matches("0"))))
- DeclareWin(jButton4);
- if((a7.matches("X") && (a8.matches("X")) && (a9.matches("X"))))
- DeclareWin(jButton7);
- if((a7.matches("0") && (a8.matches("0")) && (a9.matches("0"))))
- DeclareWin(jButton7);
- if((a1.matches("X") && (a4.matches("X")) && (a7.matches("X"))))
- DeclareWin(jButton1);
- if((a1.matches("0") && (a4.matches("0")) && (a7.matches("0"))))
- DeclareWin(jButton1);
- if((a2.matches("X") && (a5.matches("X")) && (a8.matches("X"))))
- DeclareWin(jButton2);
- if((a2.matches("0") && (a5.matches("0")) && (a8.matches("0"))))
- DeclareWin(jButton2);
- if((a3.matches("X") && (a6.matches("X")) && (a9.matches("X"))))
- DeclareWin(jButton3);
- if((a3.matches("0") && (a6.matches("0")) && (a9.matches("0"))))
- DeclareWin(jButton3);
- if((a1.matches("X") && (a5.matches("X")) && (a9.matches("X"))))
- DeclareWin(jButton1);
- if((a1.matches("0") && (a5.matches("0")) && (a9.matches("0"))))
- DeclareWin(jButton1);
- if((a3.matches("X") && (a5.matches("X")) && (a7.matches("X"))))
- DeclareWin(jButton3);
- if((a3.matches("0") && (a5.matches("0")) && (a7.matches("0"))))
- DeclareWin(jButton3);
- }
- public void CheckEndGame()
- {
- if(click >= 9)
- EndGame();
- }
- public void EndGame()
- {
- job_final = false;
- first_button_click = false;
- click = 0;
- player_turn = 1;
- jButton1.setText(" ");
- jButton2.setText(" ");
- jButton3.setText(" ");
- jButton4.setText(" ");
- jButton5.setText(" ");
- jButton6.setText(" ");
- jButton7.setText(" ");
- jButton8.setText(" ");
- jButton9.setText(" ");
- jLabel2.setText("Remiza");
- }
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton1);
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton4);
- }
- private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton7);
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton2);
- }
- private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton6);
- }
- private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton8);
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton3);
- }
- private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton5);
- }
- private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
- ChangeButtonText(jButton9);
- }
- private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
- RestartGame();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new NewJFrame().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton10;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JButton jButton5;
- private javax.swing.JButton jButton6;
- private javax.swing.JButton jButton7;
- private javax.swing.JButton jButton8;
- private javax.swing.JButton jButton9;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement