Write a java applet that will allow two players to play a Ti
Solution
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class TicTacToe extends Applet implements MouseListener
{
Frame f;
int flag=2,n,m,i=0;
char ch[]=new char[9];
public TicTacToe()
{
f=new Frame(\"Tic Tac Toe\");
f.setLayout(null);
f.setVisible(true);
f.setSize(500,500);
f.addMouseListener(this);
for(i=0;i<9;i++)
ch[i]=\'B\';
}
public void mouseClicked(MouseEvent e)
{
Graphics g=f.getGraphics();
g.drawLine(166,0,166,500);
g.drawLine(333,0,333,500);
g.drawLine(0,166,500,166);
g.drawLine(0,333,500,333);
flag--;
int x=e.getX();
int y=e.getY();
if(flag==1)
{
if(x<166&&y<166){m=0;n=0;ch[0]=\'R\';}
if((x>166&&x<333)&&(y<166)){m=166;n=0;ch[1]=\'R\';}
if((x>333&&x<500)&&(y<166)){m=333;n=0;ch[2]=\'R\';}
if(x<166&&(y>166&&y<333)){m=0;n=166;ch[3]=\'R\';}
if((x>166&&x<333)&&(y>166&&y<333)){m=166;n=166;ch[4]=\'R\';}
if((x>333&&x<500)&&(y>166&&y<333)){m=333;n=166;ch[5]=\'R\';}
if(x<166&&(y>333&&y<500)){m=0;n=333;ch[6]=\'R\';}
if((x>166&&x<333)&&(y>333&&y<500)){m=166;n=333;ch[7]=\'R\';}
if((x>333&&x<500)&&(y>333&&y<500)){m=333;n=333;ch[8]=\'R\';}
g.setColor(Color.red);
g.drawLine(m,n,m+199,n+199);
g.drawLine(m+199,n,m,n+199);
}
if(flag==0)
{
if(x<166&&y<166){m=0;n=20;ch[0]=\'P\';}
if((x>166&&x<333)&&(y<166)){m=166;n=20;ch[1]=\'P\';}
if((x>333&&x<500)&&(y<166)){m=333;n=20;ch[2]=\'P\';}
if(x<166&&(y>166&&y<333)){m=0;n=166;ch[3]=\'P\';}
if((x>166&&x<333)&&(y>166&&y<333)){m=166;n=166;ch[4]=\'P\';}
if((x>333&&x<500)&&(y>166&&y<333)){m=333;n=166;ch[5]=\'P\';}
if(x<166&&(y>333&&y<500)){m=0;n=333;ch[6]=\'P\';}
if((x>166&&x<333)&&(y>333&&y<500)){m=166;n=333;ch[7]=\'P\';}
if((x>333&&x<500)&&(y>333&&y<500)){m=333;n=333;ch[8]=\'P\';}
g.setColor(Color.green);
g.drawOval(m+10,n+10,169,169);
flag=flag+2;
}
for(i=0;i<9;i++) // for draw
{
if(ch[i]!=\'B\')
{
if(i==8)
draw();
}
else
break;
}
for(i=0;i<3;i++) //for vertical
{
// System.out.print(ch[i]);
if(ch[i]!=\'B\')
{
if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))
win();
}
}
for(i=0;i<7;i++) //for horizontal
{
if(ch[i]!=\'B\')
{
if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
win();
i=i+2;
}
else
i=i+2;
}
if(ch[4]!=\'B\') //for diagonals
{
if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))
win();
}
}
public Frame win()
{
Frame m=new Frame(\"Result\");
Label l=new Label(\"you win\");
m.setLayout(null);
m.add(l);
l.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;
}
public Frame draw()
{
Frame m=new Frame(\"Result\");
Label l1=new Label(\"Stalemate\");
m.setLayout(null);
m.add(l1);
l1.setBounds(20,20,60,60);
m.setVisible(true);
m.setSize(100,100);
return m;
}
public void mouseReleased(MouseEvent e)
{
System.out.print(\"\");
}
public void mouseEntered(MouseEvent e)
{
System.out.print(\"\");
}
public void mouseExited(MouseEvent e)
{
System.out.print(\"\");
}
public void mousePressed(MouseEvent e)
{
System.out.print(\"\");
}
public static void main(String args [])
{
new TicTacToe();
}
}


