import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame implements ActionListener
{
static Socket socket;
static BufferedReader br;
static PrintWriter out;
JFrame f;
JButton b[][];
JPanel p1,p2;
JLabel lb;
int stepIndecator=0;
int firstStep;
Icon wPone = new ImageIcon("01-Pone.png");
Icon wCastel = new ImageIcon("02-Castle.png");
Icon wKnight = new ImageIcon("03-Knight.png");
Icon wBishop = new ImageIcon("04-Bishop.png");
Icon wQueen = new ImageIcon("05-Queen.png");
Icon wKing = new ImageIcon("06-King.png");
Icon bPone = new ImageIcon("07-Pone.png");
Icon bCastel = new ImageIcon("08-Castle.png");
Icon bKnight = new ImageIcon("09-Knight.png");
Icon bBishop = new ImageIcon("10-Bishop.png");
Icon bQueen = new ImageIcon("11-Queen.png");
Icon bKing = new ImageIcon("12-King.png");
public Client()
{
f=new JFrame("Chess.............Client");
b=new JButton[9][9];
p1=new JPanel();
p2=new JPanel();
p2.setLayout(new FlowLayout());
lb=new JLabel("Please Wait........");
p2.add(lb);
boolean setBackColor=true;
for(int i=1;i<=8;i++)
{
for(int j=1;j<=8;j++)
{
b[i][j]=new JButton();
b[i][j].addActionListener(this);
p1.add(b[i][j]);
if(setBackColor)
{
b[i][j].setBackground(Color.white);
setBackColor=false;
}
else
{
b[i][j].setBackground(new Color(206,206,206));
setBackColor=true;
}
}
if(setBackColor)
setBackColor=false;
else
setBackColor=true;
}
for(int j=1;j<=8;j++)
{
b[2][j].setIcon(wPone);
b[7][j].setIcon(bPone);
}
b[1][1].setIcon(wCastel);
b[1][8].setIcon(wCastel);
b[8][1].setIcon(bCastel);
b[8][8].setIcon(bCastel);
b[1][2].setIcon(wKnight);
b[1][7].setIcon(wKnight);
b[8][2].setIcon(bKnight);
b[8][7].setIcon(bKnight);
b[1][3].setIcon(wBishop);
b[1][6].setIcon(wBishop);
b[8][3].setIcon(bBishop);
b[8][6].setIcon(bBishop);
b[1][4].setIcon(wQueen);
b[8][4].setIcon(bQueen);
b[1][5].setIcon(wKing);
b[8][5].setIcon(bKing);
p1.setLayout(new GridLayout(8,8,0,0));
f.add(p1,BorderLayout.CENTER);
f.add(p2,BorderLayout.SOUTH);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
System.out.println("stemIndecator: "+stepIndecator);
if(stepIndecator==0)
stepIndecator=1;
boolean flag=false;
int i=0,j=0;
for(i=1;i<=8;i++)
{
for(j=1;j<=8;j++)
{
if(ae.getSource()==b[i][j])
{
flag=true;
//System.out.println("i=hgh "+i+" j= "+j);
break;
}
}
if(flag)
break;
}
System.out.println("stemIndecator: "+stepIndecator);
System.out.println("i= "+i+" j= "+j);
//System.out.println((b[i][j].getIcon()).equals(null));
try{
if(stepIndecator==2)
{
System.out.println("2");
try{
if((b[i][j].getIcon()).equals(null))
{
}
else
{
if((b[i][j].getIcon()).equals(wPone)||(b[i][j].getIcon()).equals(wCastel)||(b[i][j].getIcon()).equals(wKnight)||(b[i][j].getIcon()).equals(wBishop)||(b[i][j].getIcon()).equals(wQueen)||(b[i][j].getIcon()).equals(wKing))
{
moveOk(i,j);
}
else
stepIndecator=1;
}
}catch(Exception e)
{
moveOk(i,j);
}
}
if (stepIndecator==1 && !((b[i][j].getIcon()).equals(null)))
{
if(!((b[i][j].getIcon()).equals(wPone)||(b[i][j].getIcon()).equals(wCastel)||(b[i][j].getIcon()).equals(wKnight)||(b[i][j].getIcon()).equals(wBishop)||(b[i][j].getIcon()).equals(wQueen)||(b[i][j].getIcon()).equals(wKing)))
{
firstStep=(i*10)+j;
stepIndecator++;
System.out.println("1");
}
}
System.out.println("xxxxxxxxxxxxx");
}catch(Exception e){}
}
public void moveOk(int i,int j)
{
try
{
boolean move=false;
if((b[firstStep/10][firstStep%10].getIcon()).equals(bPone))
move=bPoneMove(i,j);
if((b[firstStep/10][firstStep%10].getIcon()).equals(bCastel))
move=bCastelMove(i,j);
if((b[firstStep/10][firstStep%10].getIcon()).equals(bKnight))
move=bKnightMove(i,j);
if((b[firstStep/10][firstStep%10].getIcon()).equals(bBishop))
move=bBishopMove(i,j);
if((b[firstStep/10][firstStep%10].getIcon()).equals(bKing))
move=bKingMove(i,j);
if((b[firstStep/10][firstStep%10].getIcon()).equals(bQueen))
move=bQueenMove(i,j);
if(move)
{
b[i][j].setIcon(b[firstStep/10][firstStep%10].getIcon());
b[firstStep/10][firstStep%10].setIcon(null);
boolean flag=false,brk=false;
for(int m=0;m<=8;m++)
{
for(int n=0;n<=8;n++)
{
try{
if((b[m][n].getIcon()).equals(wQueen))
{
flag=true;
brk=true;
break;
}
}catch(Exception e){}
}
if(brk)
break;
}
if(!flag)
{
JOptionPane.showMessageDialog(null,"You Won","Client",2);
out.flush();
out.println("lost");
out.close();
System.exit(0);
}
stepIndecator=0;
out.flush();
out.println(firstStep+""+i+""+j);
out.close();
for(int ii=1;ii<=8;ii++)
{
for(int jj=1;jj<=8;jj++)
{
lb.setText("Please Wait........");
b[ii][jj].removeActionListener(this);
}
}
}
stepIndecator=0;
}
catch (Exception e)
{
System.out.println("exception in moveOk"+e);
}
}
public boolean bQueenMove(int d1,int d2)
{
int s1=firstStep/10;
int s2=firstStep%10;
stepIndecator=0;
if((s1==d1)&&((s2==(d2-1))||(s2==(d2+1))))
return true;
else if((s2==d2)&&((s1==(d1-1))||(s1==(d1+1))))
return true;
else if(((s1==(d1+1))&&(s2==(d2+1)))|| ((s1==(d1-1))&&(s2==(d2-1))))
return true;
else if(((s1==(d1-1))&&(s2==(d2+1))) ||((s1==(d1+1))&&(s2==(d2-1))))
return true;
return false;
}
public boolean bKingMove(int d1,int d2)
{
return(bBishopMove(d1,d2)|| bCastelMove(d1,d2));
}
public boolean bBishopMove(int d1,int d2)
{
System.out.println("wBishopMove");
int s1=firstStep/10;
int s2=firstStep%10;
stepIndecator=0;
boolean flag=false;
int x=s1,y=s2;
if((s1<d1)&&(s2>d2))
{
System.out.println("Yes-1");
while((x<=8)&&(y>0))
{
x++;y--;
if((x==d1)&&(y==d2))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println("Yes-2");
x=s1+1;y=s2-1;
while((x<d1)&&(y>d2))
{
if(!(checkImage(x,y)))
return false;
x++;y--;
}
System.out.println("Yes-3");
return true;
}
else
return false;
}
if((s1<d1)&&(s2<d2))
{
System.out.println("Yes-1");
while((x<=8)&&(y<=8))
{
x++;y++;
if((x==d1)&&(y==d2))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println("Yes-2");
x=s1+1;y=s2+1;
while((x<d1)&&(y<d2))
{
if(!(checkImage(x,y)))
return false;
x++;y++;
}
System.out.println("Yes-3");
return true;
}
else
return false;
}
if((s1>d1)&&(s2>d2))
{
System.out.println("Yes-1");
while((x>0)&&(y>0))
{
x--;y--;
if((x==d1)&&(y==d2))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println("Yes-2");
x=s1-1;y=s2-1;
while((x>d1)&&(y>d2))
{
if(!(checkImage(x,y)))
return false;
x--;y--;
}
System.out.println("Yes-3");
return true;
}
else
return false;
}
if((s1>d1)&&(s2<d2))
{
System.out.println("Yes-1");
while((x>0)&&(y<=8))
{
x--;y++;
if((x==d1)&&(y==d2))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println("Yes-2");
x=s1-1;y=s2+1;
while((x>d1)&&(y>d2))
{
if(!(checkImage(x,y)))