import java.io.*;
import java.net.*;
import java.util.Random;
import javax.swing.JOptionPane;					

public class server 
{
final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
	public static final int PORT = 9000;
  	final static int MAXUSERS = 50;
    final static int SCREENHEIGHT = 768;
    final static int SCREENWIDTH = 1024;
    static int [] xx = new int[MAXUSERS];
    static int [] hitBy = new int[MAXUSERS];
    static int [] yy = new int[MAXUSERS];
    static int [] spx = new int[MAXUSERS];
    static int [] spy = new int[MAXUSERS];
    static int [] score = new int[MAXUSERS];
    static int [] heading = new int[MAXUSERS];
    static int [] team = new int[MAXUSERS];
    static int[]blockX = {600,500,400,300};
    static int[]blockY = {200,300,400,500};

    static boolean [] alive = new boolean[MAXUSERS];
    static String [] name = new String[MAXUSERS];
    public static int numUsers = 0;
    public static int timer = 20000;
    public static int chargerX = -50,chargerY = -50;
    public static int size = 2;
    final static int SCOREBOX = 80;
final static String PATH = "c:\\clieserv\\";//c:\\cs2\\marbles\\";
    
public static void main(String[] args) throws IOException
      {
     	 ServerSocket s= new ServerSocket(PORT);
    	 InetAddress addr =  InetAddress.getLocalHost();
    	 int playerNum = 0;
	  	 BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
	  	 String str = "";
	  	 String playAgain="";
	  	 String[] users = new String[MAXUSERS];
   	  boolean inOpen = true,outOpen = true;
   	  Socket socket = new Socket();
  	  do
	   {	 
	  	 for (int d =0;d<MAXUSERS;d++)
	  	     score[d]=0;
	  	 numUsers = 0;    
	  	 String thisUser="";
	  	 //timer = 30000;
      	 try
      	   {
      	   	  System.out.println(addr.toString());
      	   	  System.out.println("\n\n\n           Server address "+addr.toString());
      	//   	       addr.toString().substring(addr.toString().length()-2,addr.toString().length()));
      	   	  System.out.println("\n       Click Start\n         Run and\n        type H:MARBLES.BAT");
      	   	  BufferedReader in;
      	   	  PrintWriter out;
      	   	  String whoIsOn="";
        	  whoIsOn = "Server " + addr + "\n";
        	  int whichUser = 0;
  	   	      while(timer > 0)
      	   	   	{
        	   	    timer--;
                    socket = s.accept();
         	   	    in	 = new BufferedReader ( new InputStreamReader(
  	   	    	   	        socket.getInputStream()));
 	   	            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
  	   	    	   	        socket.getOutputStream())),true);
  //   input from stream **********************	   	   	  	
      	   	        try
      	   	          {
         	    	  	 in.ready();
         	    	  	 str = in.readLine();
         	    	  	 System.out.println(str);
        	   	   	  	 inOpen = true;
         	    	  //	 turtle.enter("after str = in.readLine() " + str);
      	   	          //   whoIsOn += str; 
         	    	  	 thisUser = str.substring(str.length()-2,str.length());
         	    	  	 int x = -1;
         	    	  	 do
         	    	  	    {
         	    	  	    	x++;
         	    	  	    	if (thisUser.equals(users[x]))
         	    	  	    	    whichUser = x;
         	    	  	    }	
         	    	  	 while (x<numUsers&&!thisUser.equals(users[x]));//works but slow
         	    	  	 if (x==numUsers && x< MAXUSERS) //new player
  //                       if (str.charAt(0) == '~')//new player
         	    	  	    {
         	    	  	        int v = -1;
         	    	  	        do
         	    	  	          {
         	    	  	          	v++;
         	    	  	          }
         	    	  	        while(str.charAt(v)!='[');
         	    	  	     
         	    	  	        name[numUsers] = str.substring(0,v); 	
         	    	  	        v ++;
         	    	  	     
         	    	  	        team[numUsers] = (int)str.charAt(v)-48; 	
      	   	                    System.out.print(name[numUsers]+
      	   	                       " entered the game on computer " + thisUser);
   	    		             	System.out.println("  "+str.substring(str.indexOf('_')+1,str.indexOf('_')+3));
         	    	  	        System.out.println("..................." + 
         	    	  	           (numUsers + 1)+ " in game.");
        	    	  	    	users[numUsers] = thisUser;
         	    	  	    	xx[numUsers] = 50;//(int)(Math.random()*650)+125;
         	    	  	    	yy[numUsers] = numUsers*25+50;//(int)(Math.random()*525)+50;
         	    	  	    	spx[numUsers] = 0;//(int)(Math.random()*15)-7;
         	    	  	    	spy[numUsers] = 0;//(int)(Math.random()*15)-7;
         	    	  	    	heading[numUsers] = E;//(int)(Math.random()*8)-7;
         	    	  	    	score[numUsers] = 0;
         	    	  	    	numUsers++;
        	    	  	    }
         	    	  	 else
         	    	  	    {  
         	    	  	         if (str.charAt(0)== '~')  
         	    	  	            {   
      	   	    		             	System.out.println("which user  "+str);
      	   	    		             	int loc = str.indexOf('~',1);
      	   	    		         		int wTeam = str.charAt(loc+1)-1-48;    
      	   	    		         		team[wTeam] = str.charAt(loc+3)-48; 
     	   	    		             	System.out.println("team to change  "+str.charAt(loc+1) +
     	   	    		             	        " array index "+ wTeam + " Color " + 
     	   	    		             	        str .charAt(loc+3)+ "team color " +team[wTeam]);
   
      	   	    		           }
         	    	  	         keyInput(whichUser,str.charAt(0));
         	    	  	    }	   	   
         	    	  	    	
      	   	    	  	// if(str.equals("END")) break;
      	   	            // System.out.println(" Users "+thisUser);
                      }
      	   	        catch(Exception e)
      	   	          {
      	   	    	  	 //System.out.println("Exception "+e + " input not open");
          	   	   	  	 inOpen = false;
      	   	    	  	 //input.readLine();
      	   	           }
      //   output to stream **********************	   	   	  	
      	   	        if (timer % (10*30) == 0)
      	   	           {
         	    	  	    	chargerX = (int)(Math.random()*650)+75;
         	    	  	    	chargerY = (int)(Math.random()*450)-75;
      	   	           }
//      	   	        else   
//      	   	        if (timer % (10*30) == 0)
//      	   	           {
//         	    	  	    	chargerX = 0;
//         	    	  	    	chargerY = 0;
//      	   	           }
      	   	        String outString = ""+ numUsers+"T"+timer;
      	   	        try
      	   	          {
      	   	    	  	 for (int x = 0;x<numUsers;x++)
      	   	    	  	   {
      	   	    	  	   	  outString +=name[x]+":";
      	   	    	  	   	  outString += "X" + xx[x];
      	   	    	  	   	  outString += "Y" + yy[x];
      	   	    	  	      outString += "H" + heading[x];
      	   	    	  	      outString += "S" + score[x];
      	   	    	  	      outString += "B" + hitBy[x];
      	   	    	  	      outString += "T" + team[x];
      	   	    	  	   }   
            	  	     outString += "C" + chargerX + "C" + chargerY;
      	   	    		 //System.out.println("outstring "+outString);
      	 	   	         if (timer <= 1) 
        	   	             outString = "END";
      	   	    	  	 out.println(""+ outString);
          	   	   	  	 outOpen = true;
      	   	            // System.out.println(outString);
      	   	    	  }
      	   	        catch(Exception e)
      	   	          {
          	   	   	  	 outOpen = false;
      	   	    	  	 System.out.println("Exception "+e + " output not open");
      	   	    	  	 //input.readLine();
      	   	           }
      //   close stream **********************	   	   	  	
      	   	         if (inOpen || outOpen)
      	   	    	   {  	 
      	   	    	  	 inOpen = false;
      	   	    	  	 outOpen = false;
      	   	    	  	 socket.close();
      	   	    	  	} 
      	   	         updateLoc();  	 
      	   	    	 
      	   	    } // END network loop	 
      	   	  

      	   	  }          	  	 
   	   	 
   	   	 finally
      	   {

                 if (inOpen)      	   	   	     	  	    	   
      	   	    	 socket.close();
      	     	 s.close();
      	     	 String message = "Game Over\n\n";
      	     	 sortScore(name,score,numUsers);
  //    	     	 BufferedWriter scorefile;
  // FileWriter scoref;
 //  try
       {
    //       scoref = new FileWriter(PATH + "Marblescore.data");
      //     scorefile = new BufferedWriter(scoref);
       }
 //  catch (IOException e)
   {}
      	     	 for (int d =0;d<numUsers;d++)
	  	                {
	  	                     message += "" +(d+1)+".  "+score[d]+"  ";
	  	                     message += name[d]+"  \n";
	  	//                     scorefile.write(message);
	  	                     if (name[d].equals("COL"))
	  	                         score[d] = 300;
	  	                 }  
	  	  //        scorefile.close();  
                 JOptionPane.showMessageDialog(null,
 	  	            message +
 	  	            "\n\nTo play again, restart the server and run marbles.",
 	  	            "Marbles  Game over",2);

//      	     	  	 playAgain = JOptionPane.showInputDialog(null,
//      	     	  	          message +"\n\nPlay again?","Marbles",2);

      	   }
      }
     while (playAgain.toUpperCase().charAt(0) == 'Y'); 	   
  }
public static void sortScore(String[] n,int [] score,int num)
{
	String temp = "";
	int tmp = 0;
	for (int x = 0; x< num-1;x++)
    	for (int y = x+1 ;y< num;y++)
    	   if(score[x] < score[y])
    	       {
    	       	   temp = name[x];
    	       	   name[x] = name[y];
    	       	   name [y] = temp;
    	       	   tmp = score[x];
    	       	   score[x] = score[y];
    	       	   score [y] = tmp;
	         }
}	 
public static void keyInput(int user,char KeyCode )
{
   char code = 0;
   int accel = 1;
   int decel = 1;
   int maxStep = 2;
   int currentSpeed =0;
     //  accel = 0;
     switch(KeyCode)
       {
       //	  case KeyEvent.VK_SHIFT :code = 'S';break;
       	  case '~' :
       	  			break;
       	  case 'D' :spy[user] += accel;
       	            yy[user] += spy[user]*2;
       	            spx[user]/=2;
       	            break; // control thing 1
       	  case 'U':spy[user] -= accel;
       	            yy[user] += spy[user]*2;
       	            spx[user]/=2;
       	            break;
       	  case 'R':spx[user] += accel;
       	            xx[user] += spx[user]*2;
       	            spy[user]/=2;
       	            break;
       	  case 'L' :spx[user] -= accel;
       	            xx[user] += spx[user]*2;
       	            spy[user]/=2;
       	            break;
       	  case 'Y' :spx[user] += accel*2;        //mouse up right
       	           // xx[user] += spx[user]*2;
       	            spy[user]-=accel*2;
       	            break;
       	  case 'H' :spx[user] += accel*2;        //mouse down right
       	           // xx[user] += spx[user]*2;
       	            spy[user]+=accel*2;
       	            break;
       	  case 'G' :spx[user] -= accel*2;        //mouse up left
       	           // xx[user] += spx[user]*2;
       	            spy[user]+=accel*2;
       	            break;
       	  case 'T' :spx[user] -= accel*2;        //mouse down left
       	           // xx[user] += spx[user]*2;
       	            spy[user]-=accel;
       	            break;
       	  case 'N' :heading[user] = N;break;
       	  case 'W' :heading[user] = W;break;
       	  case 'S' :heading[user] = S;break;
       	  case 'E' :heading[user] = E;break;
       	  case 'Q' :heading[user] = NW;break;
       	  case 'F' :heading[user] = NE;break;
       	  case 'Z' :heading[user] = SW;break;
       	  case 'C' :heading[user] = SE;break;
/*       	  case 'T' :xx[user] = (int)(Math.random()*700)+50;
       	            yy[user] = (int)(Math.random()*500)+50;
       	            //spy[user]-=2;
       	            break;
*/      	  case 'B' :
      	           if (spy[user] >0)  //stop
      	              {
      	                 spy[user] -=decel;
      	                 if (spy[user] <0)
      	                     spy [user] = 0;
      	              }       
      	                 
      	           else      
      	              {
      	                 spy[user] +=decel;
      	                 if (spy[user] >0)
      	                     spy [user] = 0;
      	              }       
      	           if (spx[user] >0)
      	              { 
      	                 spx[user] -=decel;
      	                 if (spx[user] <0)
      	                     spx [user] = 0;
      	              }       
      	           else      
      	              {
      	                 spx[user] +=decel;
      	                 if (spx[user] >0)
      	                     spx [user] = 0;
      	              }       
      	               
      	           break;
      } 	  //case KeyEvent.VK_ESCAPE :code = 'E';break;
      	
      	           if (spy[user] >maxStep)  //stop
      	                 spy[user] =maxStep;
      	           if (spy[user] < -maxStep)  //stop
      	                 spy[user] = -maxStep;
      	           if (spx[user] >maxStep)  //stop
      	                 spx[user] =maxStep;
      	           if (spx[user] < -maxStep)  //stop
      	                 spx[user] = -maxStep;
      	               
      
 /*  do
     {
       // currentSpeed = (int)(Math.sqrt((spy[user]*spy[user])+(spy[user]*spy[user])));
        currentSpeed = (int)((spx[user]*spx[user])+(spy[user]*spy[user]));
        if(spy[user] >0) 
            spy[user]--;
        else    
            spy[user]++;
        if(spx[user] >0) 
           spx[user]--;
        else   
           spx[user]++;
     }
   while (currentSpeed>200);   //speed governor
   System.out.println("sx sy current "+spx[user]+" "+spy[user] +" "+currentSpeed);   
  */ 
   
}  

public static void updateLoc ()
  {
     for (int x = 0; x<numUsers;x++)
        {
        	int offx =3*(score[x]/12+1)/2;
	        int offy =3*(score[x]/12+1)/2;
            xx[x]+=spx[x];
        	yy[x]+=spy[x];
        	if (xx[x] < 70 && yy[x] <70&&score[x]> 10)
        	    score[x] -= 2;
         	if (xx[x] < 70 && yy[x] >SCREENHEIGHT - 70&&score[x]> 10)
        	    score[x] -= 2;
        	if (xx[x] > SCREENWIDTH - 70 && yy[x] <50&&score[x]> 10)
        	    score[x] -= 2;
        	if (xx[x] > SCREENWIDTH - 70 && yy[x] > SCREENHEIGHT -70&&score[x]> 10)
        	    score[x] -= 2;
          /*     use for obstacles
            for(int b = 1;b<4;b++)
         	   if (xx[x] + offx > blockX[b])
         	      if (xx[x] -offx < blockX[b]+50)
         	         if (yy[x] - offy < blockY[b]+50)
         	            if (yy[x] + offy > blockY[b])
        	   {
        	   	   spx[x] *= -1;
        	   	   spy[x] *= -1;
        	   	   xx[x]+=offx*spx[x];
        	   	   yy[x]+=offy*spy[x];
        	   	   if (score[x]>100)
        	   	      score[x] -= 2;

       	   	   } 
       	 */  	     
        	if (xx[x] > chargerX&& xx[x] < chargerX +60)
        	  if (yy[x] > chargerY&& yy[x] < chargerY +60)
        	   {
        	   	   spx[x] *= 2;
        	   	   spy[x] *= 2;
        	   	   xx[x]+=offx*spx[x];
        	   	   yy[x]+=offy*spy[x];
        	   	   score[x] += 3;
				   if (score[x] > 300)
					   score[x] = 300;   
        	   	}   
         	if (xx[x] <25)
        	   {
        	   	   spx[x] *= -1;
        	   	   if (spx[x] <0)
        	   	      spx[x] *= -1;
        	   	   xx[x]=30;
        	   	}   
            if( xx[x] > SCREENWIDTH - 25)
        	   {
        	   	   spx[x] *= -1;
        	   	   if (spx[x] >0)
        	   	      spx[x] *= -1;
        	   	   xx[x]=SCREENWIDTH - 30;
        	   	}           	   	
        	if (yy[x] <50)
        	   {
        	   	   spy[x] *= -1;
        	   	   if (spy[x] <0)
        	   	      spy[x] *= -1;
        	   	   yy[x]=55;
           // heading[x] = setHeading(spx[x],spy[x],heading[x]);
        	   	}
        	if( yy[x] > SCREENHEIGHT - 25)
        	   {
        	   	   spy[x] *= -1;
        	   	   if (spx[x] >0)
        	   	      spx[x] *= -1;
        	   	   yy[x]=SCREENHEIGHT-30;
           // heading[x] = setHeading(spx[x],spy[x],heading[x]);
        	   	} 
        }
     int order = (int)(Math.random()*2);
     int start = (int)(Math.random()*numUsers);
     int t = start + 1;
     if (t==numUsers)
        t = 0; 
     if (order == 0)
        t = start-1 ;
     if (t<0)
        t = numUsers-1; 
     int cycle = (int)(Math.random()*numUsers);
     int v = cycle +1;
     if (v >= numUsers)
	           v =0;   
   
     if (numUsers>1)
       do
         {	   	          	
		    order = (int)(Math.random()*2);
		    start = (int)(Math.random()*numUsers);
		    t = start + 1;
		    if (t>=numUsers)
		       t = 0; 
		    if (order == 0)
		       t = start-1 ;
		    if (t<0)
		        t = numUsers-1; 
      	     do
    	   	  {
    	   		checkCollision(start,t);
    		    if (order == 1)
    	           {
       	              t++;
            	      if (t == numUsers)
            	           t =0;   
       	           }
   	            else
   	               {
       	              t--;
            	      if (t == -1)
            	           t =numUsers-1;   
       	           }
        	  }         
       	   while (t!=start);    
           v++;
	       if (v == numUsers)
	           v =0;   
          }         
      while (v!=cycle);    
	   	  
}
public static int setHeading(int x,int y,int current)
{
	    int spdx = x /2;
	    int spdy = y /2;
	    if (spdx == 0)
          {
           if (spdy > 0)
         	    return S;
           if (spdy < 0)
         	    return N;
          }	    
        if (spdx > 0)
          {
           if (spdy > 0)
         	    return SE;
           if (spdy < 0)
         	    return NE;
           if (spdy == 0)
         	    return E;
          }	    
        if (spdx < 0)
          {
           if (spdy > 0)
         	    return SW;
           if (spdy < 0)
         	    return NW;
           if (spdy == 0)
         	    return W;
          }	
       return current;      
}
public static int getDirTo(int x1,int y1,int x2,int y2)
{
	double slope = 0;
	if(x1-x2==0)
	   {
	   	  if (y1<y2)
	   	    return S;
	   	  else
	   	    return N;  
	   	}    
     slope = (double)(y1-y2)/(double)(x1-x2);
     
     
     if(x1<x2)
         {
         	if (slope > 2)
         	   return N;
         	if (slope > 1/2.0)
         	   return NE;
         	if (slope > -1/2.0)
         	   return E;
         	if (slope > -2)
         	   return SE;
         	else
         	   return S;
         }         	   
 	if (slope > 2)
 	   return S;
 	if (slope > 1/2.0)
 	   return SW;
 	if (slope > -1/2.0)
 	   return W;
 	if (slope > -2)
 	   return NW;
 	else
 	   return N;
    //return 0;    
//         if((y1-y2)<3)
//            if((y1-y2)>-3)
//                return E;
//     
//     if(x1>x2 &&((y1-y2)<3&&(y1-y2)>-3))
//         return W;
//     if(y1<y2 &&((x1-x2)<3&&(x1-x2)>-3))
//         return S;
//     if(y1>y2 &&((x1-x2)<3&&(x1-x2)>-3))
//         return N;
//
//     if(x1<x2 &&y1<y2)
//         return SE;
//     if(x1<x2 &&y1>y2)
//         return NE;
//     if(x1>x2 &&y1<y2)
//         return SW;
//     if(x1>x2 &&y1>y2)
//         return NW;
}
public static void checkCollision(int x, int y)
{
	
//if (xx[x] > SCOREBOX && xx[y] > SCOREBOX)//  no collisions in scorebox
  {
	
	if (team[x]!=team[y])
	{
	int offx1 =3*(score[x]/12+2);
	int offy1 =3*(score[x]/12+2);
	int offx2 =3*(score[y]/12+2);
	int offy2 =3*(score[y]/12+2);

    int left1 = xx[x]-offx1;
    int right1 = xx[x]+offx1;
    int top1 = yy[x]-offy1;
    int bottom1 = yy[x]+offy1;
    
    int left2 = xx[y]-offx2;
    int right2 = xx[y]+offx2;
    int top2 = yy[y]-offy2;
    int bottom2 = yy[y]+offy2;
    int temp = 0;
	if (right1 >left2)
	    if (bottom1 > top2)
	        if (right2 > left1)
	            if( bottom2 > top1)
	                {
	                   temp =spx[x];
	                   spx[x] = spx[y];
	                   spx[y] = temp;
	                   
	                   temp =spy[x];
	                   spy[x] = spy[y];
	                   spy[y] = temp;
 
                       xx[x] += spx[x]*offx1;
                       yy[x] += spy[x]*offy1;
                       xx[y] += spx[y]*offx2;
                       yy[y] += spy[y]*offy2;
                  //   xx[y] = (int)(Math.random()*700)+50;
                      // yy[y] = (int)(Math.random()*500)+50;

                       spy[x]/=2;
                       spx[x]/=2;
                       spy[y]/=2;
                       spx[y]/=2;
                       
	                   doScore(x,y);
//                       heading[x] = setHeading(spx[x],spy[x],heading[x]);
//                       heading[y] = setHeading(spx[y],spy[y],heading[y]);
                       
	                 }  
    } //scorebox
    else
    {
//    	if (timer%30==0)
//           {
//             if (xx[x] <= SCOREBOX)
//    	        score[x]--;	                 
//             if (xx[y] <= SCOREBOX)
//    	   
//    	        score[y]--;	                 
//    	     if (score[x]<0)   
//    	         score[x] = 0;
//    	     if (score[y]<0)   
//    	         score[y] = 0;
//    	   } 
    }
    }	       
}	
public static void doScore(int x, int y)
{
    int whoScores = (int)(Math.random()*2); 
    int XHeading = heading[x];//setHeading(spx[x],spy[x],heading[x]);
    int YHeading = heading[y];//setHeading(spx[y],spy[y],heading[y]);
    int dirToY = getDirTo(xx[x],yy[x],xx[y],yy[y]);
    int dirToX = getDirTo(xx[y],yy[y],xx[x],yy[x]);
    int scoreAddX = 1;
    
    int scoreAddY = 1;
   /* if(xx[x]>600)
       scoreAddX = xx[x]/70;
    if(xx[y]>600)
       scoreAddY = xx[y]/70;
 */
       
    if (whoScores == 0)
       {
		    if (XHeading == dirToY||
		        XHeading+1==dirToY||
		        XHeading-1==dirToY||
		        XHeading+7==dirToY||
		        XHeading-7==dirToY)
		         if (YHeading != dirToX)/*&&
		             YHeading+1!=dirToX&&
		             YHeading-1!=dirToX&&
		             YHeading+7!=dirToX&&
		             YHeading-7!=dirToX)*/
				      {
            		    if (XHeading == dirToY)
            		       {
				    		    score[x] += score[y]/5+scoreAddX;
				   		        hitBy[y]=x;
						        score[y]-=score[y]/5;
						        teleport(y);  
						    }
						else
				    		 { 
				    		    score[x] += score[y]/8+scoreAddX;
				   		        hitBy[y]=x;
						        score[y]-=score[y]/8;
						        teleport(y);  
						    }
						    
				       } 
		    if (YHeading == dirToX||
		        YHeading+1==dirToX||
		        YHeading-1==dirToX||
		        YHeading+7==dirToX||
		        YHeading-7==dirToX)
		         if (XHeading != dirToY)/*&&
		             XHeading+1!=dirToY&&
		             XHeading-1!=dirToY&&
		             XHeading+7!=dirToY&&
		             XHeading-7!=dirToY)*/
				      {
            		    if (YHeading == dirToX)
            		       {
			    		    score[y] += score[x]/5+scoreAddY;
			   		        hitBy[x]=y;
					        score[x]-=score[x]/5;
					        teleport(x);  
					       } 
					    else   
            		       {
			    		    score[y] += score[x]/8+scoreAddY;
			   		        hitBy[x]=y;
					        score[x]-=score[x]/8;
					        teleport(x);  
					       } 
					  }     
		}
	else			       
       {
		    if (YHeading == dirToX||
		        YHeading+1==dirToX||
		        YHeading-1==dirToX||
		        YHeading+7==dirToX||
		        YHeading-7==dirToX)
		         if (XHeading != dirToY)/*&&
		             XHeading+1!=dirToY&&
		             XHeading-1!=dirToY&&
		             XHeading+7!=dirToY&&
		             XHeading-7!=dirToY)*/
				      {
            		    if (YHeading == dirToX)
            		       {
			    		    score[y] += score[x]/5+scoreAddY;
			   		        hitBy[x]=y;
					        score[x]-=score[x]/5;
					        teleport(x);  
					       } 
					    else   
            		       {
			    		    score[y] += score[x]/8+scoreAddY;
			   		        hitBy[x]=y;
					        score[x]-=score[x]/8;
					        teleport(x);  
					       } 
					  }     
		    if (XHeading == dirToY||
		        XHeading+1==dirToY||
		        XHeading-1==dirToY||
		        XHeading+7==dirToY||
		        XHeading-7==dirToY)
		         if (YHeading != dirToX)/*&&
		             YHeading+1!=dirToX&&
		             YHeading-1!=dirToX&&
		             YHeading+7!=dirToX&&
		             YHeading-7!=dirToX)*/
				      {
            		    if (XHeading == dirToY)
            		       {
				    		    score[x] += score[y]/5+scoreAddX;
				   		        hitBy[y]=x;
						        score[y]-=score[y]/5;
						        teleport(y);  
						    }
						else
				    		 { 
				    		    score[x] += score[y]/8+scoreAddX;
				   		        hitBy[y]=x;
						        score[y]-=score[y]/8;
						        teleport(y);  
						    }
						    
				       } 
		}

//		    if (XHeading == dirToY&&YHeading!=dirToX)
//		      {
//		           score[x] += score[y]/5+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/5;
//		        teleport(y);  
//		       }
//		    else    
//		    if ((XHeading == dirToY-1||XHeading == dirToY+7)&&
//		         YHeading!=dirToX&&
//		         YHeading+1!=dirToX
//		         &&YHeading+7!=dirToX)
//		      {
//    		        score[x] += score[y]/8+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/8;
//		        teleport(y);  
//		       } 
//		    else    
//		    
//		    if ((XHeading == dirToY+1||XHeading == dirToY-7)&&
//		        YHeading!=dirToX&&
//		         YHeading+1!=dirToX
//		         &&YHeading+7!=dirToX)
//		      {
//  		        score[x] += score[y]/8+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/8;
//		        teleport(y);  
//		       }
//		    else    
//		     
//		    if (YHeading == dirToX&&XHeading!=dirToY)
//		      {
//	            score[y] += score[x]/5+1;
//   		        hitBy[x]=y;
//		        score[x]-=score[x]/5;
//		        teleport(x);  
//		       } 
//		    else    
//		    
//		    if ((YHeading == dirToX-1||YHeading == dirToX+7)&&
//		        XHeading!=dirToY&&
//		         XHeading+1!=dirToY
//		         &&XHeading+7!=dirToY)
//		      {
//   		        score[y] += score[x]/8+1;
//   		        hitBy[x]=y;
//
//		        score[x]-=score[x]/8;
//		        teleport(x);  
//		       }
//		    else    
//		     
//		    if ((YHeading == dirToX+1||YHeading == dirToX-7)&&
//		         XHeading!=dirToY&&
//		         XHeading+1!=dirToY
//		         &&XHeading+7!=dirToY)
//		      {
//   		        score[y] += score[x]/8+1;
//   		        hitBy[x]=y;
//		        score[x]-=score[x]/8;
//		        teleport(x);  
//		       }
//        }
//    else
//       {    
//		    if (YHeading == dirToX&&XHeading!=dirToY)
//		      {
//	            score[y] += score[x]/5+1;
//   		        hitBy[x]=y;
//		        score[x]-=score[x]/5;
//		        teleport(x);  
//		       }
//		    else    
//		     
//		    if ((YHeading == dirToX-1||YHeading == dirToX+7)&&
//		        XHeading!=dirToY&&
//		         XHeading+1!=dirToY
//		         &&XHeading+7!=dirToY)
//		      {
//   		        score[y] += score[x]/8+1;
//   		        hitBy[x]=y;
//
//		        score[x]-=score[x]/8;
//		        teleport(x);  
//		       }
//		    else    
//		     
//		    if ((YHeading == dirToX+1||YHeading == dirToX-7)&&
//		         XHeading!=dirToY&&
//		         XHeading+1!=dirToY
//		         &&XHeading+7!=dirToY)
//		      {
//   		        score[y] += score[x]/8+1;
//   		        hitBy[x]=y;
//		        score[x]-=score[x]/8;
//		        teleport(x);  
//		       }
// 		    else    
//		    
//		    if (XHeading == dirToY&&YHeading!=dirToX)
//		      {
//		           score[x] += score[y]/5+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/5;
//		        teleport(y);  
//		       }
//		    else    
//		     
//		    if ((XHeading == dirToY-1||XHeading == dirToY+7)&&
//		         YHeading!=dirToX&&
//		         YHeading+1!=dirToX
//		         &&YHeading+7!=dirToX)
//		      {
//    		        score[x] += score[y]/8+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/8;
//		        teleport(y);  
//		       } 
//		    else    
//		    
//		    if ((XHeading == dirToY+1||XHeading == dirToY-7)&&
//		        YHeading!=dirToX&&
//		         YHeading+1!=dirToX
//		         &&YHeading+7!=dirToX)
//		      {
//  		        score[x] += score[y]/8+1;
//   		        hitBy[y]=x;
//		        score[y]-=score[y]/8;
//		        teleport(y);  
//		       }
//      } 
       
    if (score[x] < 0)
       score[x] = 0;   
    if (score[y] < 0)
       score[y] = 0;   
       
    if (score[x] > 300)
       score[x] = 300;   
    if (score[y] > 300)
       score[y] = 300;   
}
public static void teleport(int x)
{
	  if(spx[x]==0)
	    spx[x]= 1;
	  if(spy[x]==0)
	    spy[x]= 1;
//	    {
          //spx[x]*=3;// 30;//(int)(Math.random()*700)+50;
          xx[x]  +=100*spx[x];//(int)(Math.random()*300)+350;
          //spy[x]*=3;
          yy[x]  +=100*spy[x];//(int)(Math.random()*300)+350;
          //heading[x] = E;//        }  
//      else    
//          xx[x] =50;//(int)(Math.random()*700)+50;
//	  if(spy[x]!=0)
//          yy[x] =50;//(int)(Math.random()*500)+50;
//      else    
//          yy[x] =50;//(int)(Math.random()*700)+50;
          
}	
public static void pause (int t)
  {
     for (int x = 0; x<1150;x++)
        for(int y = 0;y < 50*t;y++); 	
  }	
}