Java Project – Tic Tac Toe Game

Free Java courses with 37 real-time projects - Learn Java

Program 1

package dataflair;

import java.awt.Color;
import javax.swing.JOptionPane;

  public class TestGame extends javax.swing.JFrame 
  {
    private int player1count=0;
    private int player2count=0;
    private String startgame="X";
    public TestGame() {
        initComponents();
         disablegame();
    }
   public void select_player()
   {
     if(startgame.equalsIgnoreCase("X"))
         startgame="O";   
     else
        startgame="X";  
      }
   public void scoresheet()
   {
       player1.setText(String.valueOf(player1count));
       player2.setText(String.valueOf(player2count));
   }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btn1 = new javax.swing.JButton();
        btn2 = new javax.swing.JButton();
        btn3 = new javax.swing.JButton();
        btn4 = new javax.swing.JButton();
        btn5 = new javax.swing.JButton();
        btn6 = new javax.swing.JButton();
        btn7 = new javax.swing.JButton();
        btn8 = new javax.swing.JButton();
        btn9 = new javax.swing.JButton();
        jDesktopPane1 = new javax.swing.JDesktopPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        player2 = new javax.swing.JLabel();
        player1 = new javax.swing.JLabel();
        btnStart = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 51, 51));
        jLabel1.setText("Data Flair Tic Tac Toe Game");

        btn1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1ActionPerformed(evt);
            }
        });

        btn2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn2ActionPerformed(evt);
            }
        });

        btn3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn3ActionPerformed(evt);
            }
        });

        btn4.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn4ActionPerformed(evt);
            }
        });

        btn5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn5ActionPerformed(evt);
            }
        });

        btn6.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn6ActionPerformed(evt);
            }
        });

        btn7.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn7ActionPerformed(evt);
            }
        });

        btn8.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn8ActionPerformed(evt);
            }
        });

        btn9.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btn9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn9ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(240, 240, 240));
        jLabel2.setText("Play 1 Score");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(240, 240, 240));
        jLabel3.setText("Play 2 Score");

        player2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        player2.setForeground(new java.awt.Color(51, 255, 51));
        player2.setText("########");

        player1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        player1.setForeground(new java.awt.Color(102, 255, 51));
        player1.setText("#######");

        javax.swing.GroupLayout jDesktopPane1Layout = new javax.swing.GroupLayout(jDesktopPane1);
        jDesktopPane1.setLayout(jDesktopPane1Layout);
        jDesktopPane1Layout.setHorizontalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDesktopPane1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(player2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(83, Short.MAX_VALUE))
            .addGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDesktopPane1Layout.createSequentialGroup()
                    .addContainerGap(208, Short.MAX_VALUE)
                    .addComponent(player1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(85, 85, 85)))
        );
        jDesktopPane1Layout.setVerticalGroup(
            jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDesktopPane1Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jLabel2)
                .addGap(42, 42, 42)
                .addGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(player2))
                .addContainerGap(94, Short.MAX_VALUE))
            .addGroup(jDesktopPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jDesktopPane1Layout.createSequentialGroup()
                    .addGap(52, 52, 52)
                    .addComponent(player1)
                    .addContainerGap(155, Short.MAX_VALUE)))
        );
        jDesktopPane1.setLayer(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jDesktopPane1.setLayer(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jDesktopPane1.setLayer(player2, javax.swing.JLayeredPane.DEFAULT_LAYER);
        jDesktopPane1.setLayer(player1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btnStart.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        btnStart.setText("Start Game");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        btnExit.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        btnReset.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        btnReset.setText("Reset Game");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(336, 336, 336)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(82, 82, 82)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
                .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(81, 81, 81))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(77, 77, 77)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(48, 48, 48)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn5, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn6, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jDesktopPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn7, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn8, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn9, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        int n;
         n=JOptionPane.showConfirmDialog(this, "Are you sure want to exit","Exit window",JOptionPane.YES_NO_OPTION);
         if(n==0)
           System.exit(0);
         
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        // TODO add your handling code here:
          enablegame();
          player1count=0;
          player2count=0;
          scoresheet();
          resetdata();
    }//GEN-LAST:event_btnStartActionPerformed

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn1ActionPerformed
        // TODO add your handling code here:
           btn1.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn1.setForeground(Color.BLUE);
           else
               btn1.setForeground(Color.RED);
          select_player();   
          finalwinner();
         
    }//GEN-LAST:event_btn1ActionPerformed

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn2ActionPerformed
        // TODO add your handling code here:
        btn2.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn2.setForeground(Color.BLUE);
           else
               btn2.setForeground(Color.RED);
          select_player();  
          finalwinner();
         
    }//GEN-LAST:event_btn2ActionPerformed

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn3ActionPerformed
        // TODO add your handling code here:
        btn3.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn3.setForeground(Color.BLUE);
           else
               btn3.setForeground(Color.RED);
          select_player();
          finalwinner();
         
    }//GEN-LAST:event_btn3ActionPerformed

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn4ActionPerformed
        // TODO add your handling code here:
        btn4.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn4.setForeground(Color.BLUE);
           else
               btn4.setForeground(Color.RED);
          select_player();  
          finalwinner();
          
    }//GEN-LAST:event_btn4ActionPerformed

    private void btn5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn5ActionPerformed
        // TODO add your handling code here:
        btn5.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn5.setForeground(Color.BLUE);
           else
               btn5.setForeground(Color.RED);
          select_player(); 
          finalwinner();
          
    }//GEN-LAST:event_btn5ActionPerformed

    private void btn6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn6ActionPerformed
        // TODO add your handling code here:
        btn6.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn6.setForeground(Color.BLUE);
           else
               btn6.setForeground(Color.RED);
          select_player();
          finalwinner();
         
    }//GEN-LAST:event_btn6ActionPerformed

    private void btn7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn7ActionPerformed
        // TODO add your handling code here:
        btn7.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn7.setForeground(Color.BLUE);
           else
               btn7.setForeground(Color.RED);
          select_player();
          finalwinner();
          
    }//GEN-LAST:event_btn7ActionPerformed

    private void btn8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn8ActionPerformed
        // TODO add your handling code here:
        btn8.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn8.setForeground(Color.BLUE);
           else
               btn8.setForeground(Color.RED);
          select_player(); 
          finalwinner();
          
    }//GEN-LAST:event_btn8ActionPerformed

    private void btn9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn9ActionPerformed
        // TODO add your handling code here:
        btn9.setText(startgame);
           if(startgame.equalsIgnoreCase("X"))
               btn9.setForeground(Color.BLUE);
           else
               btn9.setForeground(Color.RED);
          select_player();  
          finalwinner();
          
    }//GEN-LAST:event_btn9ActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        resetdata();
    }//GEN-LAST:event_btnResetActionPerformed
    public void enablegame()
    {
        btn1.setEnabled(true);
        btn2.setEnabled(true);
        btn3.setEnabled(true);
        btn4.setEnabled(true);
        btn5.setEnabled(true);
        btn6.setEnabled(true);
        btn7.setEnabled(true);
        btn8.setEnabled(true);
        btn9.setEnabled(true);
    }
    public void disablegame()
    {
        btn1.setEnabled(false);
        btn2.setEnabled(false);
        btn3.setEnabled(false);
        btn4.setEnabled(false);
        btn5.setEnabled(false);
        btn6.setEnabled(false);
        btn7.setEnabled(false);
        btn8.setEnabled(false);
        btn9.setEnabled(false);
    }
    public void resetdata()
    {
       btn1.setText("");
       btn2.setText("");
       btn3.setText("");
       btn4.setText("");
       btn5.setText("");
       btn6.setText("");
       btn7.setText("");
       btn8.setText("");
       btn9.setText("");
       btn1.setBackground(Color.GRAY);
       btn2.setBackground(Color.GRAY);
       btn3.setBackground(Color.GRAY);
       btn4.setBackground(Color.GRAY);
       btn5.setBackground(Color.GRAY);
       btn6.setBackground(Color.GRAY);
       btn7.setBackground(Color.GRAY);
       btn8.setBackground(Color.GRAY);
       btn9.setBackground(Color.GRAY);
       
    }
    public void finalwinner()
    {
        String b1,b2,b3,b4,b5,b6,b7,b8,b9;
        b1=btn1.getText();
        b2=btn2.getText();
        b3=btn3.getText();
        b4=btn4.getText();
        b5=btn5.getText();
        b6=btn6.getText();
        b7=btn7.getText();
        b8=btn8.getText();
        b9=btn9.getText();
        
       
        // Player 1 winner code
        if(b1.equals("X") && b2.equals("X") && b3.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn1.setBackground(Color.YELLOW);
              btn2.setBackground(Color.YELLOW);
              btn3.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b4.equals("X") && b5.equals("X") && b6.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn4.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b7.equals("X") && b8.equals("X") && b8.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn4.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b1.equals("X") && b4.equals("X") && b7.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn1.setBackground(Color.YELLOW);
              btn4.setBackground(Color.YELLOW);
              btn7.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b2.equals("X") && b5.equals("X") && b8.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn2.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn8.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b3.equals("X") && b6.equals("X") && b9.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn3.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              btn9.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b3.equals("X") && b5.equals("X") && b7.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn3.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn7.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b1.equals("X") && b5.equals("X") && b9.equals("X"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 1 is Wnner");
               player1count++;
              btn1.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn9.setBackground(Color.YELLOW);
              scoresheet();
        }
        
         // Player 2 winner code
        if(b1.equals("O") && b2.equals("O") && b3.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn1.setBackground(Color.YELLOW);
              btn2.setBackground(Color.YELLOW);
              btn3.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b4.equals("O") && b5.equals("O") && b6.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn4.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b7.equals("O") && b8.equals("O") && b8.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn4.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b1.equals("O") && b4.equals("O") && b7.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn1.setBackground(Color.YELLOW);
              btn4.setBackground(Color.YELLOW);
              btn7.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b2.equals("O") && b5.equals("O") && b8.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn2.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn8.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b3.equals("O") && b6.equals("O") && b9.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn3.setBackground(Color.YELLOW);
              btn6.setBackground(Color.YELLOW);
              btn9.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        if(b3.equals("O") && b5.equals("O") && b7.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn3.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn7.setBackground(Color.YELLOW);
              scoresheet();
        }
        if(b1.equals("O") && b5.equals("O") && b9.equals("O"))
        {
              JOptionPane.showMessageDialog(this, "Congratulation Player 2 is Wnner");
               player2count++;
              btn1.setBackground(Color.YELLOW);
              btn5.setBackground(Color.YELLOW);
              btn9.setBackground(Color.YELLOW);
              scoresheet();
        }
        
        
    }
    
    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(TestGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestGame.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 TestGame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn1;
    private javax.swing.JButton btn2;
    private javax.swing.JButton btn3;
    private javax.swing.JButton btn4;
    private javax.swing.JButton btn5;
    private javax.swing.JButton btn6;
    private javax.swing.JButton btn7;
    private javax.swing.JButton btn8;
    private javax.swing.JButton btn9;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnStart;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel player1;
    private javax.swing.JLabel player2;
    // End of variables declaration//GEN-END:variables
}

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *