Following Code extends Applet and Allows the users to play the TicTacToe Game.
Code::
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TicTacToe extends Applet implements ActionListener {
Button b[][]=new Button[3][3];
int c=1,w=0;
String p1="",p2="";
public void init()
{ setLayout(null);
Frame f=new Frame("D");
Dialog d=new Dialog(f,"Dialog Example",true);
d.setLayout(null);
Button s=new Button("Submit");
s.setBounds(50,150,50,50);
Label l1=new Label("Player1:");
Label l2=new Label("Player2:");
TextField t1=new TextField("");
TextField t2=new TextField("");
l1.setBounds(50,50,50,50);
l2.setBounds(50,100,50,50);
t1.setBounds(120,70,100,30);
t2.setBounds(120,120,100,30);
s.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
p1=t1.getText();
p2=t2.getText();
d.setVisible(false);
}
});
d.add(s);
d.add(l1);
d.add(l2);
d.add(t1);
d.add(t2);
d.setSize(300,300);
d.setVisible(true);
int x=100;
int y=100;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{ b[i][j]=new Button();
b[i][j].setBounds(x+100,y+100,50,50);
add(b[i][j]);
x+=100;
}
y+=100;
x=100;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j].addActionListener(this);
}
}
Button R=new Button("Reset");
R.setBounds(290,500,70,50);
add(R);
R.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j].setLabel("");
}
}
c=1;w=0;
}
});
}
public void actionPerformed(ActionEvent e)
{ for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(e.getSource()==b[i][j])
{
if(c%2!=0)
{
b[i][j].setLabel("X");
}
else
{
b[i][j].setLabel("0");
}
b[i][j].setEnabled(false);
}
}
}
c++;
for(int i=0;i<3;i++)
{ int j=0;
if(b[i][j].getLabel().equals("X")&&b[i][j+1].getLabel().equals("X")&&b[i][j+2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
}
for(int i=0;i<3;i++)
{ int j=0;
if(b[i][j].getLabel().equals("0")&&b[i][j+1].getLabel().equals("0")&&b[i][j+2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
}
for(int j=0;j<3;j++)
{ int i=0;
if(b[i][j].getLabel().equals("X")&&b[i+1][j].getLabel().equals("X")&&b[i+2][j].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
}
for(int j=0;j<3;j++)
{ int i=0;
if(b[i][j].getLabel().equals("0")&&b[i+1][j].getLabel().equals("0")&&b[i+2][j].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
}
int i=0,j=0;
if(b[i][j].getLabel().equals("0")&&b[i+1][j+1].getLabel().equals("0")&&b[i+2][j+2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
i=0;
j=0;
if(b[i][j].getLabel().equals("X")&&b[i+1][j+1].getLabel().equals("X")&&b[i+2][j+2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
i=0;
j=2;
if(b[i][j].getLabel().equals("X")&&b[i+1][j-1].getLabel().equals("X")&&b[i+2][j-2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
i=0;
j=2;
if(b[i][j].getLabel().equals("0")&&b[i+1][j-1].getLabel().equals("0")&&b[i+2][j-2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
if(c>9 && w==0)
{
JOptionPane.showMessageDialog(null,"Draw");
}
}
public void paint(Graphics g)
{ Image i=getImage(getDocumentBase(),"23.jpg");
g.setColor(Color.BLACK);
g.setFont(new Font("",Font.BOLD,50));
g.drawImage(i,0, 0, this);
g.drawString("TicTacToe",200,100);
g.drawLine(270, 180,270,480);
g.drawLine(375, 180,375,480);
g.drawLine(180,270,480,270);
g.drawLine(180,375,480,375);
g.setColor(Color.BLUE);
g.drawString("By Dishant",200,650);
}
}
Code::
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TicTacToe extends Applet implements ActionListener {
Button b[][]=new Button[3][3];
int c=1,w=0;
String p1="",p2="";
public void init()
{ setLayout(null);
Frame f=new Frame("D");
Dialog d=new Dialog(f,"Dialog Example",true);
d.setLayout(null);
Button s=new Button("Submit");
s.setBounds(50,150,50,50);
Label l1=new Label("Player1:");
Label l2=new Label("Player2:");
TextField t1=new TextField("");
TextField t2=new TextField("");
l1.setBounds(50,50,50,50);
l2.setBounds(50,100,50,50);
t1.setBounds(120,70,100,30);
t2.setBounds(120,120,100,30);
s.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
p1=t1.getText();
p2=t2.getText();
d.setVisible(false);
}
});
d.add(s);
d.add(l1);
d.add(l2);
d.add(t1);
d.add(t2);
d.setSize(300,300);
d.setVisible(true);
int x=100;
int y=100;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{ b[i][j]=new Button();
b[i][j].setBounds(x+100,y+100,50,50);
add(b[i][j]);
x+=100;
}
y+=100;
x=100;
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j].addActionListener(this);
}
}
Button R=new Button("Reset");
R.setBounds(290,500,70,50);
add(R);
R.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j].setLabel("");
}
}
c=1;w=0;
}
});
}
public void actionPerformed(ActionEvent e)
{ for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(e.getSource()==b[i][j])
{
if(c%2!=0)
{
b[i][j].setLabel("X");
}
else
{
b[i][j].setLabel("0");
}
b[i][j].setEnabled(false);
}
}
}
c++;
for(int i=0;i<3;i++)
{ int j=0;
if(b[i][j].getLabel().equals("X")&&b[i][j+1].getLabel().equals("X")&&b[i][j+2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
}
for(int i=0;i<3;i++)
{ int j=0;
if(b[i][j].getLabel().equals("0")&&b[i][j+1].getLabel().equals("0")&&b[i][j+2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
}
for(int j=0;j<3;j++)
{ int i=0;
if(b[i][j].getLabel().equals("X")&&b[i+1][j].getLabel().equals("X")&&b[i+2][j].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
}
for(int j=0;j<3;j++)
{ int i=0;
if(b[i][j].getLabel().equals("0")&&b[i+1][j].getLabel().equals("0")&&b[i+2][j].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
}
int i=0,j=0;
if(b[i][j].getLabel().equals("0")&&b[i+1][j+1].getLabel().equals("0")&&b[i+2][j+2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
i=0;
j=0;
if(b[i][j].getLabel().equals("X")&&b[i+1][j+1].getLabel().equals("X")&&b[i+2][j+2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
i=0;
j=2;
if(b[i][j].getLabel().equals("X")&&b[i+1][j-1].getLabel().equals("X")&&b[i+2][j-2].getLabel().equals("X"))
{
JOptionPane.showMessageDialog(null,p1+" Wins");
w=1;
}
i=0;
j=2;
if(b[i][j].getLabel().equals("0")&&b[i+1][j-1].getLabel().equals("0")&&b[i+2][j-2].getLabel().equals("0"))
{
JOptionPane.showMessageDialog(null,p2+" Wins");
w=1;
}
if(c>9 && w==0)
{
JOptionPane.showMessageDialog(null,"Draw");
}
}
public void paint(Graphics g)
{ Image i=getImage(getDocumentBase(),"23.jpg");
g.setColor(Color.BLACK);
g.setFont(new Font("",Font.BOLD,50));
g.drawImage(i,0, 0, this);
g.drawString("TicTacToe",200,100);
g.drawLine(270, 180,270,480);
g.drawLine(375, 180,375,480);
g.drawLine(180,270,480,270);
g.drawLine(180,375,480,375);
g.setColor(Color.BLUE);
g.drawString("By Dishant",200,650);
}
}
Output::