All for Joomla All for Webmasters

Tic Tac Toe Game

In this example, we are going to see the example of tic tac toe game (also known as 0 and x (cross)).

It can be developed only through AWT api, but we are using here swing framework.

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. class TTT1 extends JFrame implements ItemListener, ActionListener{
  5. int i,j,ii,jj,x,y,yesnull;
  6. int a[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
  7.                 {10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},
  8.         {10,7,8,9,11} };
  9. int a1[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},
  10.                 {10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},{10,7,8,9,11} };
  11. boolean state,type,set;
  12. Icon ic1,ic2,icon,ic11,ic22;
  13. Checkbox c1,c2;
  14. JLabel l1,l2;
  15. JButton b[]=new JButton[9];
  16. JButton reset;
  17. public void showButton(){
  18. x=10; y=10;j=0;
  19. for(i=0;i<=8;i++,x+=100,j++){
  20.  b[i]=new JButton();
  21. if(j==3)
  22. {j=0; y+=100; x=10;}
  23.  b[i].setBounds(x,y,100,100);
  24. add(b[i]);
  25. b[i].addActionListener(this);
  26. }//eof for
  27. reset=new JButton(“RESET”);
  28. reset.setBounds(100,350,100,50);
  29. add(reset);
  30. reset.addActionListener(this);
  31. }//eof showButton
  32. /*********************************************************/
  33. public  void check(int num1){
  34. for(ii=0;ii<=7;ii++){
  35.    for(jj=1;jj<=3;jj++){
  36.         if(a[ii][jj]==num1){ a[ii][4]=11;  }
  37.    }//eof for jj
  38. }//eof for ii
  39. }//eof check
  40. /**********************************************************/
  41. /*********************************************************/
  42. public void complogic(int num){
  43.  for(i=0;i<=7;i++){
  44.    for(j=1;j<=3;j++){
  45.       if(a[i][j]==num){  a[i][0]=11; a[i][4]=10;    }
  46.       }
  47.   }
  48.    for(i=0;i<=7;i++){              // for 1
  49.      set=true;
  50.    if(a[i][4]==10){               //if 1 
  51.        int count=0;
  52.        for(j=1;j<=3;j++){         //for 2 
  53.            if(b[(a[i][j]-1)].getIcon()!=null){ //if 2
  54.              count++;
  55.                }                        //eof if 2
  56.             else{ yesnull=a[i][j]; }
  57.         }                               //eof for 2
  58.       if(count==2){                     //if 2
  59.          b[yesnull-1].setIcon(ic2);
  60.          this.check(yesnull); set=false;break;
  61.          }                                  //eof if 2
  62.       }                                     //eof if 1
  63.       else
  64.       if(a[i][0]==10){
  65.                 for(j=1;j<=3;j++){         //for2
  66.                     if(b[(a[i][j]-1)].getIcon()==null){ //if 1
  67.                       b[(a[i][j]-1)].setIcon(ic2);
  68.                         this.check(a[i][j]);
  69.                          set=false;
  70.                          break;
  71.                     }                         //eof if1
  72.                 }                             //eof for 2
  73.                 if(set==false)
  74.                       break;
  75.             }//eof elseif
  76.     if(set==false)
  77.          break;
  78.  }//eof for 1
  79. }//eof complogic
  80. /*********************************************************/
  81. TTT1(){
  82. super(“tic tac toe by ashwani”);
  83. CheckboxGroup cbg=new CheckboxGroup();
  84. c1=new Checkbox(“vs computer”,cbg,false);
  85. c2=new Checkbox(“vs friend”,cbg,false);
  86. c1.setBounds(120,80,100,40);
  87. c2.setBounds(120,150,100,40);
  88. add(c1); add(c2);
  89. c1.addItemListener(this);
  90. c2.addItemListener(this);
  91. state=true;type=true;set=true;
  92. ic1=new ImageIcon(“ic1.jpg”);
  93. ic2=new ImageIcon(“ic2.jpg”);
  94. ic11=new ImageIcon(“ic11.jpg”);
  95. ic22=new ImageIcon(“ic22.jpg”);
  96. setLayout(null);
  97. setSize(330,450);
  98. setVisible(true);
  99. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  100. }//eof constructor
  101. /*************************************************************/
  102. public void itemStateChanged(ItemEvent e){
  103.  if(c1.getState())
  104.   {
  105.  type=false;
  106.  }
  107.  else if(c2.getState())
  108.   { type=true;
  109.   }
  110. remove(c1);remove(c2);
  111.  repaint(0,0,330,450);
  112.  showButton();
  113. }//eof itemstate
  114. /************************************************************/
  115. public void actionPerformed(ActionEvent e){
  116. /********************************/
  117. if(type==true)//logicfriend
  118. {
  119. if(e.getSource()==reset){
  120.  for(i=0;i<=8;i++){
  121.    b[i].setIcon(null);
  122.   }//eof for  
  123. }
  124. else{
  125.   for(i=0;i<=8;i++){
  126.       if(e.getSource()==b[i]){
  127.            if(b[i].getIcon()==null){
  128.               if(state==true){ icon=ic2;
  129.                state=false;} else{ icon=ic1; state=true; }
  130.             b[i].setIcon(icon);
  131.             }
  132.        }
  133.   }//eof for
  134. }//eof else
  135. }//eof logicfriend
  136. else if(type==false){                           //  complogic
  137.       if(e.getSource()==reset){
  138.           for(i=0;i<=8;i++){
  139.             b[i].setIcon(null);
  140.           }//eof for 
  141.        for(i=0;i<=7;i++)
  142.         for(j=0;j<=4;j++)
  143.         a[i][j]=a1[i][j];   //again initialsing array
  144.         }
  145.         else{  //complogic
  146.             for(i=0;i<=8;i++){
  147.                if(e.getSource()==b[i]){
  148.                   if(b[i].getIcon()==null){
  149.                            b[i].setIcon(ic1);
  150.                             if(b[4].getIcon()==null){
  151.                               b[4].setIcon(ic2);
  152.                               this.check(5);
  153.                               } else{
  154.                                  this.complogic(i);
  155.                                  }
  156.                     }
  157.                  }
  158.              }//eof for
  159.         }
  160.     }//eof complogic
  161. for(i=0;i<=7;i++){
  162.   Icon icon1=b[(a[i][1]-1)].getIcon();
  163.   Icon icon2=b[(a[i][2]-1)].getIcon();
  164.   Icon icon3=b[(a[i][3]-1)].getIcon();
  165.      if((icon1==icon2)&&(icon2==icon3)&&(icon1!=null)){
  166.                if(icon1==ic1){
  167.                  b[(a[i][1]-1)].setIcon(ic11);
  168.                  b[(a[i][2]-1)].setIcon(ic11);
  169.                  b[(a[i][3]-1)].setIcon(ic11);
  170.     JOptionPane.showMessageDialog(TTT1.this,“!!!YOU won!!! click reset”);
  171.          break;
  172.                    }
  173.              else if(icon1==ic2){
  174.              b[(a[i][1]-1)].setIcon(ic22);
  175.              b[(a[i][2]-1)].setIcon(ic22);
  176.              b[(a[i][3]-1)].setIcon(ic22);
  177.                JOptionPane.showMessageDialog(TTT1.this,“won! click reset”);
  178.                 break;
  179.                }
  180.          }
  181.     }
  182. }//eof actionperformed
  183. /************************************************************/
  184. public static void main(String []args){
  185. new TTT1();
  186. }//eof main
  187. }//eof class
PinIt
submit to reddit

Leave a Reply

Top