Monday, 13 April 2015

Example of Tic Tac Toe Game in Swing

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

218.               }//eof class  

No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...