Games Using Java
Tic Tac Toe Using Java.
Source Code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*
<applet code=TicTakToe.class width=400 height=200>
</applet>
*/
public class TicTakToe extends java.applet.Applet implements ActionListener
{
Label l,Player,s,gover,sPlayer,w;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnSwap;
Panel p,p1;
public void init()
{
p=new Panel();
p1=new Panel();
p.setLayout(new GridLayout(4,3));
l=new Label("Current Playing");
Player=new Label("Player 1");
s=new Label("0");
gover=new Label("");
sPlayer=new Label("");
w=new Label("");
btn1=new Button("Click Me !!");
btn2=new Button("Click Me !!");
btn3=new Button("Click Me !!");
btn4=new Button("Click Me !!");
btn5=new Button("Click Me !!");
btn6=new Button("Click Me !!");
btn7=new Button("Click Me !!");
btn8=new Button("Click Me !!");
btn9=new Button("Click Me !!");
btnSwap=new Button("Swap !");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnSwap.addActionListener(this);
setBackground(Color.pink);
p1.add(l);
p1.add(Player);
p1.add(btnSwap);
p1.add(s);
add(p1);
p.add(btn1);
p.add(btn2);
p.add(btn3);
p.add(btn4);
p.add(btn5);
p.add(btn6);
p.add(btn7);
p.add(btn8);
p.add(btn9);
p.add(gover);
p.add(sPlayer);
p.add(w);
add(p);
}
void Check()
{
if(!btn1.isEnabled() &!btn2.isEnabled() &!btn3.isEnabled() )
{
if(btn1.getLabel().equals(btn2.getLabel()) & btn1.getLabel().equals(btn3.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn4.isEnabled() &!btn5.isEnabled() &!btn6.isEnabled() )
{
if(btn4.getLabel().equals(btn5.getLabel()) & btn4.getLabel().equals(btn6.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn7.isEnabled() &!btn8.isEnabled() &!btn9.isEnabled() )
{
if(btn7.getLabel().equals(btn8.getLabel()) & btn7.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn1.isEnabled() &!btn4.isEnabled() &!btn7.isEnabled() )
{
if(btn1.getLabel().equals(btn4.getLabel()) & btn1.getLabel().equals(btn7.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn2.isEnabled() &!btn5.isEnabled() &!btn8.isEnabled() )
{
if(btn2.getLabel().equals(btn5.getLabel()) & btn2.getLabel().equals(btn8.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn3.isEnabled() &!btn6.isEnabled() &!btn9.isEnabled() )
{
if(btn3.getLabel().equals(btn6.getLabel()) & btn3.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn1.isEnabled() &!btn5.isEnabled() &!btn9.isEnabled() )
{
if(btn1.getLabel().equals(btn5.getLabel()) & btn1.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn3.isEnabled() &!btn5.isEnabled() &!btn7.isEnabled() )
{
if(btn3.getLabel().equals(btn5.getLabel()) & btn3.getLabel().equals(btn7.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
}
void SwapPlayer()
{
if(Player.getText().equals("Player 1"))
{
Player.setText("Player 2");
}
else
{
Player.setText("Player 1");
}
}
public void actionPerformed(ActionEvent ae)
{
if((ae.getSource()).equals(btnSwap))
{
if(s.getText().equals("0"))
{
s.setText("X");
}
else
{
s.setText("0");
}
}
if((ae.getSource()).equals(btn1))
{
btn1.setLabel(s.getText());
btn1.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn2))
{
btn2.setLabel(s.getText());
btn2.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn3))
{
btn3.setLabel(s.getText());
btn3.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn4))
{
btn4.setLabel(s.getText());
btn4.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn5))
{
btn5.setLabel(s.getText());
btn5.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn6))
{
btn6.setLabel(s.getText());
btn6.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn7))
{
btn7.setLabel(s.getText());
btn7.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn8))
{
btn8.setLabel(s.getText());
btn8.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn9))
{
btn9.setLabel(s.getText());
btn9.setEnabled(false);
Check();
SwapPlayer();
}
}
}
import java.applet.*;
import java.awt.event.*;
/*
<applet code=TicTakToe.class width=400 height=200>
</applet>
*/
public class TicTakToe extends java.applet.Applet implements ActionListener
{
Label l,Player,s,gover,sPlayer,w;
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnSwap;
Panel p,p1;
public void init()
{
p=new Panel();
p1=new Panel();
p.setLayout(new GridLayout(4,3));
l=new Label("Current Playing");
Player=new Label("Player 1");
s=new Label("0");
gover=new Label("");
sPlayer=new Label("");
w=new Label("");
btn1=new Button("Click Me !!");
btn2=new Button("Click Me !!");
btn3=new Button("Click Me !!");
btn4=new Button("Click Me !!");
btn5=new Button("Click Me !!");
btn6=new Button("Click Me !!");
btn7=new Button("Click Me !!");
btn8=new Button("Click Me !!");
btn9=new Button("Click Me !!");
btnSwap=new Button("Swap !");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
btn5.addActionListener(this);
btn6.addActionListener(this);
btn7.addActionListener(this);
btn8.addActionListener(this);
btn9.addActionListener(this);
btnSwap.addActionListener(this);
setBackground(Color.pink);
p1.add(l);
p1.add(Player);
p1.add(btnSwap);
p1.add(s);
add(p1);
p.add(btn1);
p.add(btn2);
p.add(btn3);
p.add(btn4);
p.add(btn5);
p.add(btn6);
p.add(btn7);
p.add(btn8);
p.add(btn9);
p.add(gover);
p.add(sPlayer);
p.add(w);
add(p);
}
void Check()
{
if(!btn1.isEnabled() &!btn2.isEnabled() &!btn3.isEnabled() )
{
if(btn1.getLabel().equals(btn2.getLabel()) & btn1.getLabel().equals(btn3.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn4.isEnabled() &!btn5.isEnabled() &!btn6.isEnabled() )
{
if(btn4.getLabel().equals(btn5.getLabel()) & btn4.getLabel().equals(btn6.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn7.isEnabled() &!btn8.isEnabled() &!btn9.isEnabled() )
{
if(btn7.getLabel().equals(btn8.getLabel()) & btn7.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn1.isEnabled() &!btn4.isEnabled() &!btn7.isEnabled() )
{
if(btn1.getLabel().equals(btn4.getLabel()) & btn1.getLabel().equals(btn7.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn2.isEnabled() &!btn5.isEnabled() &!btn8.isEnabled() )
{
if(btn2.getLabel().equals(btn5.getLabel()) & btn2.getLabel().equals(btn8.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn3.isEnabled() &!btn6.isEnabled() &!btn9.isEnabled() )
{
if(btn3.getLabel().equals(btn6.getLabel()) & btn3.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn1.isEnabled() &!btn5.isEnabled() &!btn9.isEnabled() )
{
if(btn1.getLabel().equals(btn5.getLabel()) & btn1.getLabel().equals(btn9.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
if(!btn3.isEnabled() &!btn5.isEnabled() &!btn7.isEnabled() )
{
if(btn3.getLabel().equals(btn5.getLabel()) & btn3.getLabel().equals(btn7.getLabel()))
{
gover.setText("Game Over");
sPlayer.setAlignment(Label.CENTER);
sPlayer.setText(Player.getText());
w.setText("Is Winner..!!");
}
}
}
void SwapPlayer()
{
if(Player.getText().equals("Player 1"))
{
Player.setText("Player 2");
}
else
{
Player.setText("Player 1");
}
}
public void actionPerformed(ActionEvent ae)
{
if((ae.getSource()).equals(btnSwap))
{
if(s.getText().equals("0"))
{
s.setText("X");
}
else
{
s.setText("0");
}
}
if((ae.getSource()).equals(btn1))
{
btn1.setLabel(s.getText());
btn1.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn2))
{
btn2.setLabel(s.getText());
btn2.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn3))
{
btn3.setLabel(s.getText());
btn3.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn4))
{
btn4.setLabel(s.getText());
btn4.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn5))
{
btn5.setLabel(s.getText());
btn5.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn6))
{
btn6.setLabel(s.getText());
btn6.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn7))
{
btn7.setLabel(s.getText());
btn7.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn8))
{
btn8.setLabel(s.getText());
btn8.setEnabled(false);
Check();
SwapPlayer();
}
if((ae.getSource()).equals(btn9))
{
btn9.setLabel(s.getText());
btn9.setEnabled(false);
Check();
SwapPlayer();
}
}
}
- Save the file with name TicTakToe.java
- Compile with: javac TicTakToe.java
- Run with:appletviewer TicTakToe.java
No comments:
Post a Comment