//tic tac toe..using applet ...
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
//
//
@author - techieRJ
//
**/
//<applet code=tic_tac_toe width=500 height=500></applet>
public class tic_tac_toe extends Applet implements ActionListener
{
Button FF,FS,FT,SF,SS,ST,TF,TS,TT;
String F_F="z",F_S="z",F_T="z",S_F="z",S_S="z",S_T="z",T_F="z",T_S="z",T_T="z";
Button Select_X,Select_O,Replay;
//where f=first s=second t=third...first letter is for row and second is for column;
Panel panel1,panel2,panel3,cardpanel;
Label lbl_win;
CardLayout card;
String value_selected;
String last_value="O";
public void init()
{
Font font=new Font("Verdana", Font.BOLD, 20);
setFont(font);
setForeground(Color.blue);
setBackground(Color.yellow);
card = new CardLayout();
setLayout(card);
cardpanel = new Panel();
add(cardpanel);
cardpanel.setLayout(card);
//first panel
panel1 = new Panel();
cardpanel.add(panel1,"First");
Select_X = new Button("Select X");
Select_O = new Button("Select O");
panel1.add(Select_X);
panel1.add(Select_O);
//add action event
Select_X.addActionListener(this);
Select_O.addActionListener(this);
//end of panel 1
//panel 2
panel2 =new Panel();
cardpanel.add(panel2,"Second");
panel2.setLayout(new GridLayout(3,3));
FF = new Button("");
FS = new Button("");
FT = new Button("");
SF = new Button("");
SS = new Button("");
ST = new Button("");
TF = new Button("");
TS = new Button("");
TT = new Button("");
panel2.add(FF);
panel2.add(FS);
panel2.add(FT);
panel2.add(SF);
panel2.add(SS);
panel2.add(ST);
panel2.add(TF);
panel2.add(TS);
panel2.add(TT);
FF.addActionListener(this);
FS.addActionListener(this);
FT.addActionListener(this);
SF.addActionListener(this);
SS.addActionListener(this);
ST.addActionListener(this);
TF.addActionListener(this);
TS.addActionListener(this);
TT.addActionListener(this);
//end of panel2
//start of panel 3
panel3 = new Panel();
panel3.setLayout(new GridLayout(2,1));
cardpanel.add(panel3,"Third");
Replay = new Button("Repaly");
lbl_win = new Label("");
panel3.add(lbl_win);
panel3.add(Replay);
lbl_win.setBounds(10,10,100,200);
Replay.setBounds(50,50,300,200);
Replay.addActionListener(this);
//end of panel 3
}
public void actionPerformed(ActionEvent e)
{
//action first event
if(e.getSource()==Select_X)
{
value_selected= "X";
last_value = value_selected;
card.show(cardpanel,"Second");
}
if(e.getSource()==Select_O)
{
value_selected= "O";
last_value = value_selected;
card.show(cardpanel,"Second");
}
//end of panel 1
//second panel
if(e.getSource()==FF)
{
FF.setLabel(last_value);
FF.setEnabled(false);
F_F=last_value;
last();
won();
check();
}
if(e.getSource()==FS)
{
FS.setLabel(last_value);
FS.setEnabled(false);
F_S=last_value;
last();
won();
check();
}
if(e.getSource()==FT)
{
FT.setLabel(last_value);
FT.setEnabled(false);
F_T=last_value;
last();
won();
check();
}
if(e.getSource()==SF)
{
SF.setLabel(last_value);
S_F=last_value;
SF.setEnabled(false);
last();
won();
check();
}
if(e.getSource()==SS)
{
SS.setLabel(last_value);
SS.setEnabled(false);
S_S=last_value;
last();
won();
check();
}
if(e.getSource()==ST)
{
ST.setLabel(last_value);
ST.setEnabled(false);
S_T=last_value;
last();
won();
check();
}
if(e.getSource()==TF)
{
TF.setLabel(last_value);
TF.setEnabled(false);
T_F=last_value;
last();
won();
check();
}
if(e.getSource()==TS)
{
TS.setLabel(last_value);
TS.setEnabled(false);
T_S=last_value;
last();
won();
check();
}
if(e.getSource()==TT)
{
TT.setLabel(last_value);
TT.setEnabled(false);
T_T=last_value;
last();
won();
check();
}
//panel 3
if(e.getSource()==Replay)
{
again();
}
}
public void last()
{
if(last_value.equals("X"))
{
last_value="O";
}
else
{
last_value="X";
}
}
public void won()
{
//case 1 :--- horizontal line
if((F_F.equals("X") && F_S.equals("X") && F_T.equals("X")) ||(F_F.equals("O") && F_S.equals("O") && F_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((S_F.equals("X") && S_S.equals("X") && S_T.equals("X") ) || (S_F.equals("O") && S_S.equals("O") && S_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((T_F.equals("X") && T_S.equals("X") && T_T.equals("X") ) || (T_F.equals("O") && T_S.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
//case 2 :---- vertical line
if((F_F.equals("X") && S_F.equals("X") && T_F.equals("X")) ||(F_F.equals("O") && S_F.equals("O") && T_F.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_S.equals("X") && S_S.equals("X") && T_S.equals("X")) || (F_S.equals("O") && S_S.equals("O") && T_S.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_T.equals("X") && S_T.equals("X") && T_T.equals("X")) || (F_T.equals("O") && S_T.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
//case 3 : diagonal
if((F_F.equals("X") && S_S.equals("X") && T_T.equals("X")) || (F_F.equals("O") && S_S.equals("O") && T_T.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
if((F_T.equals("X") && S_S.equals("X") && T_F.equals("X")) || (F_T.equals("O") && S_S.equals("O") && T_F.equals("O")))
{
card.show(cardpanel,"Third");
last();
lbl_win.setText(last_value+" WON ");
}
}
public void again()
{
F_F="z";F_S="z";F_T="z";S_F="z";S_S="z";S_T="z";T_F="z";T_S="z";T_T="z";
FF.setEnabled(true);
FS.setEnabled(true);
FT.setEnabled(true);
SF.setEnabled(true);
SS.setEnabled(true);
ST.setEnabled(true);
TF.setEnabled(true);
TS.setEnabled(true);
TT.setEnabled(true);
FF.setLabel("");
FS.setLabel("");
FT.setLabel("");
SF.setLabel("");
SS.setLabel("");
ST.setLabel("");
TF.setLabel("");
TS.setLabel("");
TT.setLabel("");
lbl_win.setText("");
card.show(cardpanel,"First");
}
public void check()
{
if(!F_F.equals("z") && !F_S.equals("z") && !F_T.equals("z") && !S_F.equals("z") && !S_S.equals("z") && !S_T.equals("z") && !T_F.equals("z") && !T_S.equals("z") && !T_T.equals("z"))
{
lbl_win.setText("Draw.....");
card.show(cardpanel,"Third");
}
}
}
0 comments :
Post a Comment