Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a TicTacToeGUI In this project, you will implement a Tic Tac Toe Simulato

ID: 3566318 • Letter: C

Question

Create a TicTacToeGUI

In this project, you will implement a Tic Tac Toe Simulator. Display a frame that contains ten labels. Nine large labels display Xs and Os. What to display on these nine large labels are randomly decided. Use the Math.random() method to generate an integer 0 or 1, which corresponds to displaying an X or O. One label shows the result of the game. If three Xs in a horizontal, vertical, or diagonal row, X wins. If three Os in a horizontal, vertical, or diagonal row, Y wins. If no player wins, it is a Tie. If both players win, it is also a tie.

Explanation / Answer

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Random;
class Tic implements ActionListener
{
JFrame f;
static JButton b[] = new JButton[9];
JPanel jp;
JLabel l1;
GridLayout g;
static int a[][] = new int[3][3];
static boolean flag[] = new boolean[9];
Random dice = new Random();
int count=0;
     Tic(String s)
   {
       jp=new JPanel();
       g=new GridLayout(3,3,5,5);
       jp.setLayout(g);
       f = new JFrame(s);
       for(int i=0;i<9;i++)
       b[i] = new JButton();
       for(int j=0;j<9;j++)
       b[j].addActionListener(this);
       for(int k=0;k<9;k++)
          jp.add(b[k]);
       f.add(jp);
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       f.setSize(300,300);
       f.setVisible(true);
   }
   public static void main(String s[])
       {          
           new Tic("RAJNI KANT(Single player Mode)s");
       }
   public void actionPerformed(ActionEvent e)
   {
       if(!check())
       {
           for(int i=0;i<9;i++)
           {
              if(e.getSource()==b[i] && !flag[i])
              {
                   b[i].setText("X");
                   set1(i);
                   flag[i] = true;
                   count++;
              }
            }
       }  
      check();
       if(!check())
       {
           int n;
           while(true)
           {
              n = dice.nextInt(9);
              if(!flag[n])
              {
               b[n].setText("O");
               set2(n);
               flag[n] = true;
               count++;
               break;
              }
           else
               {
               if(count==9)
               break;
           }
           }
       }  
       check();
   }
   static void set1(int x)
   {
       switch(x)
       {
           case 0:
           {
               a[0][0] = 1;
               break;
           }
           case 1:
           {
               a[0][1] = 1;
               break;
           }
           case 2:
           {
               a[0][2] = 1;
               break;
           }
           case 3:
           {
               a[1][0] = 1;
               break;
           }
           case 4:
           {
               a[1][1] = 1;
               break;
           }
           case 5:
           {
               a[1][2] = 1;
               break;
           }
           case 6:
           {
               a[2][0] = 1;
               break;
           }
           case 7:
           {
               a[2][1] = 1;
               break;
           }
           case 8:
           {
               a[2][2] = 1;
               break;
           }
           default:
           break;
       }
   }
  
   static void set2(int x)
   {
       switch(x)
       {
           case 0:
           {
               a[0][0] = 2;
               break;
           }
           case 1:
           {
               a[0][1] = 2;
               break;
           }
           case 2:
           {
               a[0][2] = 2;
               break;
           }
           case 3:
           {
               a[1][0] = 2;
               break;
           }
           case 4:
           {
               a[1][1] = 2;
               break;
           }
           case 5:
           {
               a[1][2] = 2;
               break;
           }
           case 6:
           {
               a[2][0] = 2;
               break;
           }
           case 7:
           {
               a[2][1] = 2;
               break;
           }
           case 8:
           {
               a[2][2] = 2;
               break;
           }
           default:
           break;
       }
   }
   static boolean check()
   {  
           if( (a[0][0]==1 && a[0][1]==1 && a[0][2]==1) || (a[0][0]==2 && a[0][1]==2 && a[0][2]==2) )
           {   setcol(0,1,2);
               return true;
           }  
           else if( (a[0][0]== 1 && a[1][0]==1 && a[2][0]==1) || (a[0][0]==2 && a[1][0]==2 && a[2][0]==2) )
           {
           setcol(0,3,6);
              return true;
           }
           else if ((a[0][0]==1 && a[1][1]==1 && a[2][2]==1) || (a[0][0]==2 && a[1][1]==2 && a[2][2]==2) )
              {
              setcol(0,4,8);
              return true;
              }
           else if( (a[0][1]==1 && a[1][1]==1 && a[2][1]==1) || (a[0][1]==2 && a[1][1]==2 && a[2][1]==2) )
           {
              setcol(1,4,7);
              return true;
           }
           else if( (a[0][2]==1 && a[1][2]==1 && a[2][2]==1) || (a[0][2]==2 && a[1][2]==2 && a[2][2]==2) )
           {
           setcol(2,5,8);
               return true;
           }
           else if( (a[1][0]==1 && a[1][1]==1 && a[1][2]==1) || (a[1][0]==2 && a[1][1]==2 && a[1][2]==2) )
              {
               setcol(3,4,5);
               return true;
              }
           else if( (a[2][0]==1 && a[2][1]==1 && a[2][2]==1) || (a[2][0]==2 && a[2][1]==2 && a[2][2]==2) )
               {
                  setcol(6,7,8);
                  return true;
               }
           else if( (a[2][0]==1 && a[1][1]==1 && a[0][2]==1) || (a[2][0]==2 && a[1][1]==2 && a[0][2]==2) )
               {
                 setcol(6,4,2);
               return true;
               }  
           else
              return false;
       }      
  
  
   static void setcol(int i,int j,int k)
   {
       b[i].setBackground(Color.RED);
       b[j].setBackground(Color.RED);
       b[k].setBackground(Color.RED);
   }
}