//Warr address change  search to change the address
/*
     Computer science 2  Major Group Project
     
     Names
     
     Use Marbles program and turtle class to create an interactive, networked
     game.  This will be a group project, with grades determined by the 
     following requirements.
     
     Requirements:
     
     1.  Clean up graphics (they leave color when colliding) 30 points
     2.  Adjust buttons (things) --  Appear to face forward  30 points
     3.  Opening screen                                      30 points
     4.  Third dimension (z coordinate) buttons jump         30 points
     4.  Network code.  Critical Thinking in Java -- 
         Chapter 15, page 833-                               50 points
     5.  Interactive game (players on multiple computers)    50 points
     6.  Scoring system (print score on buttons)             90 points
         Basic  + 1                                              20
         Receive x% of button contacted score if hit front.      20 
         negative score                                          20  
         cooperative  (working together is a benefit)            30
     7.  Closing screen.                                     30 points
     
     Other (I must approve.  Network code must work)         30 points
     
     400 points total to be divided among you based on my evaluation 
     (with your input) of your contribution to the project.  110 maximum
     individual grade.
     
     Counts as two tests grades.
      
*/	
import javax.swing.JOptionPane;					
import java.util.StringTokenizer;								
import java.util.Random;

import java.awt.*;								
import java.applet.*;								
import java.net.*;								
import java.awt.event.*;								
import java.io.*;   //for files						
public class marbles
{
    final static int SCREENHEIGHT = 768;
    final static int SCREENWIDTH = 1024;
	public static void main(String args[]) throws IOException
	{
        Windows W = new Windows();  //Make a window
        W.setSize(SCREENWIDTH,SCREENHEIGHT);
        W.addWindowListener(new WindowAdapter()
          {
              public void windowClosing(WindowEvent e)
                {
                  System.exit(0);
                }
           });
        W.mainInput(); //Call the input function
  }
  
}

class Windows extends Frame   implements KeyListener, MouseListener, MouseMotionListener
{
        //declare variables global

// direction constants
// control variables
final static int N = 0,NE = 1,E = 2,SE = 3, S = 4, SW = 5, W = 6, NW =7,STILL = 8;
AudioClip shoot;
final static int SCREENHEIGHT = 768;
final static int SCREENWIDTH = 1024;

static int moveDown =KeyEvent.VK_DOWN ; // control thing 1
static int moveUp =KeyEvent.VK_UP ;
static int moveRight =KeyEvent.VK_RIGHT ;
static int moveLeft =KeyEvent.VK_LEFT ;
static int moveBrake =KeyEvent.VK_SPACE ; //brake
static int teleport =KeyEvent.VK_T ; //teleport
static int faceWest =KeyEvent.VK_A ; //Face west
static int faceNorth =KeyEvent.VK_W ; //Face north
static int faceEast =KeyEvent.VK_D ; //Face east
static int faceSouth =KeyEvent.VK_S ; //Face south
static int faceNorth1 =KeyEvent.VK_2 ; //Face north
static int faceNorthWest =KeyEvent.VK_Q ; //Face northwest
static int faceNorthEast =KeyEvent.VK_E ; //Face northeast
static int faceSouthWest =KeyEvent.VK_Z ; //Face southwest
static int faceSouthEast =KeyEvent.VK_C ; //Face southeast
static int menu =KeyEvent.VK_BACK_QUOTE ; //Face southeast
static int oldx = 0,oldy = 0;
static int Escape =KeyEvent.VK_ESCAPE ;
static String []taunt = {"You Will lose!",
						"I heard RED always wins",
						"You have ZERO chance!",
						"What a loser you are",
						"This game is GREAT!",
						"Why are you reading this? Play the Game",
						"You are way behind.",
						"You have no chance against me",
						"You will never get any points",
						"You will always be TINY",
						};
static int numTaunts = 10;
static int[]blockX = {600,500,400,300};
static int[]blockY = {200,300,400,500};
public static int chargerX = 0,chargerY = 0;
static String[] teamcolors = {"black","magenta","blue","cyan (light blue)","pink","green",
                              "dark green","yellow","red","gray"};
int numTeams = 0;
       	  //static int  =KeyEvent.VK_ESCAPE E;//quit
   //instance variables
static int NUMTHINGS = 0;
static int MYGUY = 0;
final static int STRT = 0,DIAG = 1,MAXUSERS = 40,SCOREBOX=0;
final static int X = 0,Y = 1,Z = 2,SCORE = 3;
static int[] teamScores = new int[MAXUSERS];
static int[] numInTeam = new int[MAXUSERS];
Button button = new Button(" ");
Panel info = new Panel();
final String PATH = "c:\\clieserv\\";//c:\\cs2\\marbles\\";
//final String PATH = "c:\\clieserv\\";//c:\\cs2\\marbles\\";
static int thisTeam=100;
static turtle [][] thing=new turtle[MAXUSERS][2];                    //define turtle objects
static turtle sp=new turtle();                    //define turtle object for tree
static int [][] locations = new int[MAXUSERS][6];
int delay = 80;
int turbo = 2, up = 5, down = 5, left = 5, right = 5, hit=0;
int count = 0;
int []dir =new int[10];
int cycle = 0;
boolean lostPoints = false;
public static String hitBy = "Loser";
int hitByNumber= 3;
boolean first = true;
boolean gameNotOver = true; 
//network variables
public static boolean inOpen = true,outOpen = true;
public static String serverInput = "";
public static String changeString = "";
public static String lastTaunt = "";
public static String clientOutput = "";
public static BufferedReader in;
public static PrintWriter out;
public static Socket socket;
public static InetAddress addr;
public static int PORT = 9000;
public static String ServerAddress = "127.0.0.1";//1.55";
public static InetAddress localAddr;
public static String outString="X";
public static int TIMER = 5000;
public static int therm = 0;
public static int ranTaunt = 0;
   int []saveX=new int[MAXUSERS];
   int []saveY=new int[MAXUSERS];
   int []saveZ=new int[MAXUSERS];
Image offImage;   
boolean success = true;   
int[] tempScore = new int [MAXUSERS];
Image pic;


public void readControls()
{
   FileReader inf;

   BufferedReader infile;
    try
       {
        inf = new FileReader("marbles.data");
        infile = new BufferedReader(inf);
        
        moveDown =Integer.parseInt(infile.readLine()); // control thing 1
        moveUp =Integer.parseInt(infile.readLine());
        moveRight  =Integer.parseInt(infile.readLine()) ;
        moveLeft =Integer.parseInt(infile.readLine());
        moveBrake =Integer.parseInt(infile.readLine()); //brake
        teleport  =Integer.parseInt(infile.readLine()); //teleport
        faceWest =Integer.parseInt(infile.readLine()) ; //Face west
        faceNorth =Integer.parseInt(infile.readLine()); //Face north
        faceEast =Integer.parseInt(infile.readLine()); //Face east
        faceSouth =Integer.parseInt(infile.readLine()) ; //Face south
        faceNorth1 =Integer.parseInt(infile.readLine()); //Face north
        faceNorthWest =Integer.parseInt(infile.readLine()) ; //Face northwest
        faceNorthEast =Integer.parseInt(infile.readLine()) ; //Face northeast
        faceSouthWest =Integer.parseInt(infile.readLine()) ; //Face southwest
        faceSouthEast =Integer.parseInt(infile.readLine()) ; //Face southeast
        Escape =Integer.parseInt(infile.readLine()) ;
        infile.close();
        }
   catch (IOException exc)
       {
       	   //JOptionPane.showMessageDialog(null,"Input File Not found");        
       	   //return;
       }	   

}	
public void saveControls()
{
   FileWriter outf;
   BufferedWriter outfile;
    try
       {
        outf = new FileWriter("marbles.data");
        outfile = new BufferedWriter(outf);
        
        outfile.write(moveDown+"\n"); // control thing 1
        outfile.write(moveUp       +"\n");
        outfile.write(moveRight               +"\n");
        outfile.write(moveLeft       +"\n");
        outfile.write(moveBrake       +"\n"); //brake
        outfile.write(teleport        +"\n"); //teleport
        outfile.write(faceWest        +"\n"); //Face west
        outfile.write(faceNorth       +"\n"); //Face north
        outfile.write(faceEast       +"\n"); //Face east
        outfile.write(faceSouth        +"\n"); //Face south
        outfile.write(faceNorth1       +"\n"); //Face north
        outfile.write(faceNorthWest        +"\n"); //Face northwest
        outfile.write(faceNorthEast        +"\n"); //Face northeast
        outfile.write(faceSouthWest        +"\n"); //Face southwest
        outfile.write(faceSouthEast        +"\n"); //Face southeast
        outfile.write(Escape        +"\n");
        outfile.close();
        }
   catch (IOException exc)
       {
       	   //JOptionPane.showMessageDialog(null,"Input File Not found");        
       	   //return;
       }	   

}
public void mainInput() throws IOException
{       
//*******************************************************network
         localAddr =  InetAddress.getLocalHost();
         BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
//  pic = Toolkit.getDefaultToolkit().getImage(PATH+"shark.gif");
        //System.out.println("enter server ip");
         //String ServerAddress = input.readLine();//
//         String starter="5";
         String starter="55";
//         do
//           {
//              starter = JOptionPane.showInputDialog(null,
//                   "The server must be started before you can play.\n"+
//                    "Enter the last number of the server address.");
//            }
//          while(starter.charAt(0)<'0'||starter.charAt(0)>'9');   	  	 
 	  	 //System.out.println("enter server port");
        String inName="",newKey="";
	    readControls();
	    do
	      {
	         inName = JOptionPane.showInputDialog(null,"Welcome to Marbles\n"+
	              "Current controls -- enter Letter to change \n" +
	              "                 "+(char)faceNorth+"                    Move direction\n" +
	              "         "+(char)faceNorthWest+"     ^     "+(char)faceNorthEast+
	                    "                  Arrow keys\n"+ 
	              "      "+(char)faceWest+"  < face >  "+(char)faceEast+
	                    "                       "+(char)moveUp+"^\n"+ 
	              "         "+(char)faceSouthWest+"      v     "+(char)faceSouthEast+
	                    "                 "+(char)moveLeft+"<-          "+
	                                      (char)moveRight+"->\n"+ 
	              "                 "+(char)faceSouth+"                                  "+(char)moveDown+" v\n"+
	              "Enter your name to begin.","Marbles",2);
	         for (int a = 0;a<inName.length();a++)
	            if(inName.charAt(a)==':')
	               {
	               	  inName = "I am a LOSER!";
	               	  break;
	               	} 
	         if (inName.length() > 10 )      	
	              inName = inName.substring(0,10);      	      
	         if (inName.length()==1)
	            {
	            	if (inName.toUpperCase().charAt(0) ==(char)faceNorth) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing north",
		                     "Change Key",1);
	            	   	  faceNorth = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceWest) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing West",
		                     "Change Key",1);
	            	   	  faceWest = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceSouth) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing South",
		                     "Change Key",1);
	            	   	  faceSouth = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceEast) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing East",
		                     "Change Key",1);
	            	   	  faceEast = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceNorthWest) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing NorthWest",
		                     "Change Key",1);
	            	   	  faceNorthWest = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceNorthEast) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing NorthEast",
		                     "Change Key",1);
	            	   	  faceNorthEast = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)faceSouthEast) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key for facing SouthEast",
		                     "Change Key",1);
	            	   	  faceSouthEast = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)moveUp) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key to move up.",
		                     "Change Key",1);
	            	   	  moveUp = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)moveDown) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key to move Down.",
		                     "Change Key",1);
	            	   	  moveDown = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)moveLeft) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key to move Left.",
		                     "Change Key",1);
	            	   	  moveLeft = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            	if (inName.toUpperCase().charAt(0) ==(char)moveRight) 
	            	   {
		                  newKey = JOptionPane.showInputDialog(null,"Enter the new Key to move Right.",
		                     "Change Key",1);
	            	   	  moveRight = (int)newKey.toUpperCase().charAt(0);
	            	   }	
	            }	    
	             
	       }
	     while(inName.length()<2);  
	     saveControls();

// 	  	 if(starter.length()>1)
// 	  	   if(starter.length()<3)
// 	  	      starter=starter.substring(starter.length()-2,starter.length());
//Warr address change 
 	  	  ServerAddress = "10.21.48.109"; 
 	  	  //ServerAddress = "127.0.0.1";//+starter; 
         addr =
              InetAddress.getByName(ServerAddress); 
            
 	  	  String str = "";
 //end network init
   for (int x = 0;x<MAXUSERS;x++)
     {
         thing[x][STRT] = new turtle((int)(Math.random()*700)+50,(int)(Math.random()*500)+75,0,
                              (int)(Math.random()*5)-2,//speedX
                              (int)(Math.random()*5)-2,//speedY
                               0 ,   //speedZ
                               x ,  //turtleColor
                              ""+x    //name
                              ,1);   //size  
       
         
         thing[x][DIAG] = new turtle();
         thing[x][DIAG].turtleColor =x;
         if (x == 11)
           {
              thing[x][DIAG].turtleColor =16;
              thing[x][STRT].turtleColor =16;
           }
         thing[x][STRT].readSprite(PATH +"ballc.data",1);
         thing[x][DIAG].readSprite(PATH+"ballcD.data",1);//remove when pictureD
     } 
    first = true;
   
//   thing[MYGUY][STRT].setSize (4);
//   thing[MYGUY][DIAG].setSize (4);
   this.addKeyListener(this);
   this.addMouseListener(this);
   this.addMouseMotionListener(this);
   int setTeam = (int)GetUpChar("Choose your own team 0 \n Let the computer choose 1.","0,1")-48;           
   this.setVisible(true);
   if (setTeam == 0)
   do
      {
            thisTeam = (int)GetUpChar("Enter team number.","Team")-48;
            System.out.println(""+thisTeam);
 	        if(thisTeam >=0 && thisTeam <=9)
 	           {
 	           	  first = false;
 	           	}  
 	   }
   while(first);	           	
   else
    thisTeam = (int)(Math.random()*8 + 1);
 //  initialize network output  **********************	   	   	  	
	socket = new Socket(addr,PORT);//server.PORT);
  	outOpen = true;
	out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
	   	        socket.getOutputStream())),true);
    in = new BufferedReader ( new InputStreamReader(
	   	       socket.getInputStream()));
	
//******remove (int)(Math.random()*90)+10 below -- for single computer test    
    outString =inName+"["+thisTeam+":"+localAddr;//'~' equals new user
	    //+(int)((Math.random()*90)+10);//+(int)(Math.random()*90)+10remove  	 
//******remove above line    
    try
       {
	  	      out.println(""+outString);
	   }
    catch(Exception e)
       {
   	  	 outOpen = false;
	  	 System.out.println("Exception "+e + " output not open");
	  	 input.readLine();
       }
 // end network initialize **********************	   	   	  	
   outString =inName+":"+localAddr;
   getPacket();
   socket.close();
   MYGUY = NUMTHINGS-1;
   if (MYGUY<0) MYGUY = 0;
   thing[MYGUY][STRT].name = inName;
   thing[MYGUY][STRT].team = thisTeam;
   System.out.println(MYGUY);
   
   thing[MYGUY][STRT].readSprite(PATH +"ARROW.DAT",1);
   thing[MYGUY][DIAG].readSprite(PATH+"ARROWD.DAT",1);
    try {
    	   //copy the .au files from H:\javaBeg to your S: drive and 
    	   //enter the declarations below
    	
           shoot = Applet.newAudioClip(new URL("file:c:\\clieserv\\crump.au"));
        }    
    catch (MalformedURLException e) {}
    therm = TIMER /SCREENWIDTH;

   this.repaint	();
   do
     {
     	 if (lostPoints)
     	   {
		     ranTaunt = (int)(Math.random()*numTaunts);         
		    // JOptionPane.showMessageDialog(null,taunt[ranTaunt]);
		    //  pause(2000);
          }
 	  	 blockX[1] = TIMER/20 + 250;
	  	 blockX[2] = TIMER/20 + 150;
	  	 blockX[3] = TIMER/20 + 5;
	  	 blockY[1] = 200 - TIMER/80;
	  	 blockY[2] = 200 - TIMER/80;
	  	 blockY[3] = 200 - TIMER/80;
         if (TIMER <= 1)
            gameNotOver = false;
         else
            
            this.repaint();
     }
   while (TIMER>1&&success);
   // End Screen
   
   pause(1000);
   String message = "Game Over\n\n";
     success = getNet();
   sortScore(thing,NUMTHINGS);
   for (int c = 0;c<10;c++)
     {
                    teamScores[c] = 0;
     }
   for (int c = 0;c<NUMTHINGS;c++)
     {
       teamScores[thing[c][STRT].team] += thing[c][STRT].score;
       numInTeam[thing[c][STRT].team]++;
     }
   for (int c = 0;c<10;c++)
     {
             if (numInTeam[c]>0)
                    teamScores[c] /= numInTeam[c];
     }
   socket.close();
//      FileReader scoref;
// //  try
//       {
//           scoref = new FileReader(PATH + "Marblescore.data");
//           BufferedReader scorefile = new BufferedReader(scoref);
//       }
// //  catch (IOException e)
//   {}
 
   for (int d =0;d<NUMTHINGS;d++)
           {
             message += ""+ (d+1) +". "+thing[d][STRT].score+"  ";
             message += thing[d][STRT].name+" "+ teamcolors[thing[d][STRT].team]+"  \n";
            } 
   message += "\n Team Scores\n";
   sortcolors();
   for (int c =0;c<10;c++)
           {
             if (numInTeam[c]>0)
                 message += " "+ teamcolors[c]+"    " +teamScores[c]+"\n";
//             message += ""+ (d+1) +". "+thing[d][STRT].score+"  ";
//             message += thing[d][STRT].name+"  \n";
           }    
   JOptionPane.showMessageDialog(null,
 	  	          message +
 	  	          "\n\nTo play again, restart the server and run marbles.",
 	  	          "Marbles  Game over",2);
  
 	         
   	
  
}// end of mainInput
public void paint(Graphics Screen)  // Draws the window
 {                        
	
      if (first)
         {
         	
         	Screen.drawString("Choose your team color",30,100);
         	for (int x = 0;x< 10;x++)
         	  {
                thing[x][STRT].sprite(x * 60 + 30,200,4,Screen);
         	    Screen.drawString(""+x,x*60+30,150);
              } 
         	 first = false;  
         }	
      else
      {   	
          setTitle("Press arrow keys OR drag mouse. Release faces opposite. Score by contacting "+
               "the BACK of other marbles. Time left: "+ (TIMER/30));  
		     if (lostPoints)
		         {
                   shoot.play();
			       Screen.setColor(Color.red);
//			       Screen.fillRect(30,
//			                       thing[MYGUY][STRT].turtleY-20,
//			                       270,50);
//			
			       Screen.setColor(Color.red);
			       Screen.fillRect(0,0,SCREENWIDTH,30);
			       Screen.fillRect(0,0,30,SCREENWIDTH);
			       Screen.fillRect(SCREENWIDTH-30,0,30,SCREENHEIGHT);
			       Screen.fillRect(0,SCREENHEIGHT-30,SCREENWIDTH,30);
//			       Screen.setColor(Color.white);
//			       Screen.fillRect(40,
//			                       thing[MYGUY][STRT].turtleY-10,
//			                       450,30);
			       Screen.setColor(Color.black);
			       for (int x = 50; x> 50;x--)
		              {
		              	  Screen.drawOval(oldx-x*3,oldy-x*3,oldx+x*3,oldy+x*3);
		              	//  pause(70); 
		              }	   	    
 


		           Screen.drawString(taunt[ranTaunt]+" "+
		                 thing[thing[MYGUY][STRT].hitBy][STRT].name, 
		                             250,thing[MYGUY][STRT].turtleY);
		            lastTaunt = taunt[ranTaunt]+" - "+
		                thing[thing[MYGUY][STRT].hitBy][STRT].name;                
//		           thing[thing[MYGUY][STRT].hitBy][STRT].sprite(285,thing[MYGUY][STRT].turtleY+5,
//		                     4,Screen); 	    
//		          // thing[MYGUY][STRT].sprite(25,thing[MYGUY][STRT].turtleY+5,
//		            //    4,Screen); 
//		            Screen.setColor(Color.red);
		            //pause(100);
			   
		      if (lostPoints)
		        {
		   	   lostPoints = false;
				}  
		            Screen.setColor(Color.black);
		   }     
	      offImage = createImage(getSize().width,getSize().height);   
	      Graphics offg =offImage.getGraphics();
	      paintOffScreen(offg);
	      Screen.drawImage(offImage,0,0,null);
	      if (tempScore[MYGUY]>thing[MYGUY][STRT].score) 
	           lostPoints = true;
	      oldx = thing[MYGUY][STRT].turtleX;
	      oldy = thing[MYGUY][STRT].turtleY;
	   }
  		   
  }
/*public void paint(Graphics Screen)  // Draws the window
 {                        
	
      setTitle("Press arrow keys to move - Space slow -- T Teleport. Score by contacting "+
               "the BACK of other marbles. Time left: "+ (TIMER/30));  
      for (int x = 0;x<NUMTHINGS;x++)
        {
		     if (tempScore[x]<thing[x][STRT].score)
		        {
		           
		           Screen.setColor(Color.white);
		           Screen.fillOval(saveX[x]-25,
		                           saveY[x]-25,
		                           50,50);
		                           
		       }
		     if (tempScore[x]>thing[x][STRT].score)
		        {
		           
		           Screen.setColor(Color.white);
		           Screen.fillRect(saveX[x]-25,
		                           saveY[x]-25,
		                           40,40);
		       }           
           thing[x][STRT].drawSprite(thing[x][STRT],thing[x][DIAG],Screen); 	    
		   saveX[x]=thing[x][STRT].turtleX;
		   saveY[x]=thing[x][STRT].turtleY;
		   saveZ[x]=thing[x][STRT].turtleZ;
           tempScore[x] = thing[x][STRT].score;         
        }     
     pause(200/NUMTHINGS); // not needed?
     success = getNet();
     if (success) 
       for (int x = 0;x<NUMTHINGS;x++)
         {	
             thing[x][STRT].eraseSprite(thing[x][STRT],thing[x][DIAG],
                    saveX[x],saveY[x],saveZ[x],Screen); 	    

		     if (tempScore[x]<thing[x][STRT].score)
		        {
		           Screen.setColor(Color.blue);
		           Screen.fillOval(saveX[x]-25,
		                           saveY[x]-25,
		                           50,50);
		           pause(80/NUMTHINGS);	                
		       }
		     if (tempScore[x]>thing[x][STRT].score)
		        {
		           Screen.setColor(Color.red);
		           Screen.fillRect(saveX[x]-25,
		                           saveY[x]-25,
		                           40,40);
		           pause(80/NUMTHINGS);	                
		       }
         }    


  }
  */
 public void paintOffScreen(Graphics Screen)  // Draws the window
 {                        
      String scorelist="";
	  Screen.setColor(Color.white);
	  Screen.fillRect(0,0,getSize().width,getSize().height); 
      Screen.setColor(Color.blue);
      Screen.fillRect(0,395,TIMER/therm,20);
      Screen.setColor(Color.cyan);
      Screen.fillRect(5,400,TIMER/therm-10,10);
      //Screen.drawString("charger "+chargerX,600,300);
      int rrr = (int)(Math.random()*2);
      int high = 0,hiOne = 0;
      int high2 = 0,hi2 = 0;
      int high3 = 0,hi3 = 0;
      int left = 400,top = 200,xx=0,yy=0;
      if (chargerX > 0)
        for (int x = 0;x<60;x+=2)
          {
	         Screen.setColor(new Color(256-180+x*3,220,90));
	         Screen.drawOval(chargerX+x,chargerY+x,60-x,60-x);
	      }   
           
	  Screen.setColor(Color.black);
//      for (int b = 1;b<4;b++)
      int b = 3;
    	  //Screen.fillOval(blockX[b],50-blockY[b],30,30); 
	     
	  if (TIMER % 3 == 0)
    	  Screen.setColor(Color.black);
	  else    
	  if (TIMER % 3 == 1)
    	  Screen.setColor(Color.magenta);
      else	  
    	  Screen.setColor(Color.red);
      Screen.fillOval(SCREENWIDTH-30,-30,100,100); 
      //Screen.fillOval(750,550,100,100); 
      Screen.fillOval(-50,SCREENHEIGHT-50,100,100); 
	  if (TIMER % 3 == 0)
	      Screen.setColor(Color.yellow);
	  else    
	  if (TIMER % 3 == 1)
	      Screen.setColor(Color.green);
	  else    
	      Screen.setColor(Color.blue);
      Screen.fillOval(SCREENWIDTH-50,SCREENHEIGHT-50,80,80); 
      Screen.fillOval(-30,-10,80,80); 
//     Screen.drawString(""+thing[MYGUY][STRT].pictureD[0] +" " +thing[MYGUY][STRT].picHeight,100,500);
//     System.out.println(""+thing[MYGUY][STRT].pictureD[0] +" " +thing[MYGUY][STRT].picHeight);
      
     if(rrr==0)
      {
       for (int x = 0;x<NUMTHINGS;x++)
        {
           thing[x][DIAG].turtleColor =thing[x][STRT].team;
           thing[x][STRT].turtleColor =thing[x][STRT].team;
           thing[x][STRT].drawSprite(thing[x][STRT],thing[x][DIAG],
           Screen,thing[x][STRT].score/12+2); 	
                  
//           Screen.drawImage(pic/*.getScaledInstance(thing[x][STRT].score/12+2
//                     , thing[x][STRT].score/12+2,Image.SCALE_FAST)*/	,
//                  thing[MYGUY][STRT].turtleX-10,
//                  thing[MYGUY][STRT].turtleY-10,
//                  this);
		   saveX[x]=thing[x][STRT].turtleX;
		   saveY[x]=thing[x][STRT].turtleY;
		   saveZ[x]=thing[x][STRT].turtleZ;
           tempScore[x] = thing[x][STRT].score;
                    
	       Screen.setColor(Color.black);
		   Screen.drawString(""+thing[x][STRT].score + "  "+
		                      thing[x][STRT].name,10,x*20+65);
		   if (thing[x][STRT].score > high)
		      {
		      	high3=high2;
		      	hi3=hi2;
		      	high2 = high;
		      	hi2=hiOne;
		      	high = thing[x][STRT].score;
		      	hiOne = x;
		      }	                  
		      
        }     
      }
     else
      {
       for (int x = NUMTHINGS-1;x>=0;x--)
        {
           thing[x][DIAG].turtleColor =thing[x][STRT].team;
           thing[x][STRT].turtleColor =thing[x][STRT].team;
           thing[x][STRT].drawSprite(thing[x][STRT],thing[x][DIAG],
                Screen,thing[x][STRT].score/12+2); 	    
           
//           Screen.drawImage(thing[MYGUY][STRT].image[x].getScaledInstance(thing[x][STRT].score/12+2
//                     , thing[x][STRT].score/12+2,SCALE_FAST)	,
//                  thing[MYGUY][STRT].turtleX-10,
//                  thing[MYGUY][STRT].turtleY-10,
//                  this);
//           Screen.drawImage(pic.getScaledInstance(thing[x][STRT].score/12+2
//                     , thing[x][STRT].score/12+2,Image.SCALE_FAST)	,
//                  thing[MYGUY][STRT].turtleX-10,
//                  thing[MYGUY][STRT].turtleY-10,
//                  this);
		   saveX[x]=thing[x][STRT].turtleX;
		   saveY[x]=thing[x][STRT].turtleY;
		   saveZ[x]=thing[x][STRT].turtleZ;
           tempScore[x] = thing[x][STRT].score;
                    
	       Screen.setColor(Color.black);
		   Screen.drawString(""+thing[x][STRT].score + "  "+
		                      thing[x][STRT].name,10,x*20+65);
		   if (thing[x][STRT].score > high)
		      {
		      	high3=high2;
		      	hi3=hi2;
		      	high2 = high;
		      	hi2=hiOne;
		      	high = thing[x][STRT].score;
		      	hiOne = x;
		      }	                  
		      
        }
       }      
 
	  Screen.setColor(Color.blue);
//      Screen.fillRect(SCOREBOX,0,10,600);
	  Screen.setColor(Color.red);
//      Screen.fillRect(550,0,3,600);
//      Screen.drawString(""+(char)faceNorth,407,225);
//      Screen.drawString(""+(char)faceNorthWest+"       "+(char)faceNorthEast,394,230);
//      Screen.drawString("^",408,235);
//      Screen.drawString(""+(char)faceWest+"<face>"+(char)faceEast,385,245);
//      Screen.drawString("v",408,255);
//      Screen.drawString(""+(char)faceSouthWest+"       "+(char)faceSouthEast,394,260);
//      Screen.drawString(""+(char)faceSouth,407,265);
//      Screen.drawString("Press arrow keys OR drag mouse. Release faces opposite. Score by contacting "+
//               "the BACK of other marbles. Time left: "+ (TIMER/30),120,560);  
     Screen.drawString("High Score "+thing[hiOne][STRT].score + "  "+
		                      thing[hiOne][STRT].name,350,300);
     Screen.drawString("Second High "+thing[hi2][STRT].score + "  "+
		                      thing[hi2][STRT].name,350,325);
     Screen.drawString(lastTaunt,350,350);
//     pause(200/NUMTHINGS); // not needed?
     success = getNet();
   // if (success) 
//       for (int x = 0;x<NUMTHINGS;x++)
//         {	
//
///*		     if (tempScore[x]<thing[x][STRT].score)
//		        {
//		           Screen.setColor(Color.blue);
//		           Screen.fillOval(saveX[x]-25,
//		                           saveY[x]-25,
//		                           50,50);
//		       }
//*/		     if (tempScore[x]>thing[x][STRT].score)
//		        {
//		           Screen.setColor(Color.red);
//		           Screen.fillOval(saveX[x]-20,
//		                           saveY[x]-20,
//		                           40,40);
//          if (x == MYGUY)                
//		               {
//		                   for (int v = 0;v< NUMTHINGS;v++)	   
//		                      scorelist+= "\n"+thing[v][STRT].score + " "+
//		                          thing[v][STRT].name;
//       	                   JOptionPane.showMessageDialog(null,"You lost points " +
//       	                        scorelist);
//       	               }                 
//		       */}
//         }    


  }
public void update(Graphics g)
{
	paint (g);
}	      
public static boolean getNet()
{      	   	    	  	 
     boolean successful = false;
	 try  //net stuff
			{
				if (!inOpen)
				    socket = new Socket(addr,PORT);//server.PORT);
         	  	outOpen = true;
         	  	inOpen = true;
 			}
	 catch(Exception e)
      	     {
      	   	    	  	 System.out.println("Exception "+e + ": new socket" );
          	   	   	  	 inOpen = false;
      	      }
     try
       {
		     if(inOpen)  
   			   out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
				   	        socket.getOutputStream())),true);
		     if(inOpen)  
			    in = new BufferedReader ( new InputStreamReader(
				   	       socket.getInputStream()));
       }
	 catch(Exception e)
      	     {
      	   	    	  	 System.out.println("Exception "+e + " can't create in /out" );
             }
                
                sendPacket();
                if (inOpen)
  	    		   successful = getPacket();
      try
            {
				
 				socket.close();
 				outOpen = false;
 				inOpen = false;
 			}
	 catch(Exception e)
      	     {
      	   	    	  	 System.out.println("Exception "+e + " can't close" );
          	   	   	  	 inOpen = false;
      	      }
     return successful; 	      
}
public static void sendPacket()
{      	   	    	  	 
        try
           {
              if (clientOutput.equals("~"))
                   clientOutput+=outString+changeString;
              clientOutput+=outString;
              //System.out.println("sendPacket"+clientOutput);	   	    	  	 
			  out.println(""+clientOutput);
			  
   	          clientOutput = "X";  //don't repeat commands
   	       }
        catch(Exception e)
           {
   	   	  	 outOpen = false;
    	  	 System.out.println("Exception "+e + " sendPacket ");
           }
           
}
public static boolean getPacket() 
{     	
//   input from stream **********************	   	   	  	
     try
      {
	  	 in.ready();
         //System.out.println("Before in.readLine "+serverInput);	   	    	  	 
	  	 serverInput = in.readLine();
        // System.out.println("Before parseLocations "+serverInput);	   	    	  	 
	  	 if(serverInput.equals("END")) return false;
 		 parseLocations ( serverInput);
      }
     catch(Exception e)
      {
	  	 //System.out.println("Exception "+e + " getpacket "+"here" );
   	  	 inOpen = false;
       }
    return true;	   	           
//  end input from stream **********************	   	   	  	
}      	   	           
public static void parseLocations (String s)
  {
       int begin = 0;
       int let = -1;
       do
          {
          	let++;
          }
       while (s.charAt(let) <= '9'&& s.charAt(let)>='0');   	
          	
  	   NUMTHINGS = Integer.parseInt(s.substring(begin,let));
  	   begin = let+1;
       do
          {
          	let++;
          }
       while ((s.charAt(let) <= '9'&& s.charAt(let)>='0')||s.charAt(let)=='-');   	
          	
  	   TIMER = Integer.parseInt(s.substring(begin,let));
  	   if (TIMER < 0 ) 
  	       TIMER = 0;

  	   begin = let;
  	   for (int u = 0;u<NUMTHINGS;u++)
  	      {
      // System.out.println("parseLocations :"+s.substring(0,10)+" Begin " + begin+" let "+let);
  	      	
	        do
	          { 
	          	let++;
	          }
	        while(s.charAt(let)!=':');
	        thing[u][STRT].name = s.substring(begin,let);
	        let+=1;
	        begin = ++let; 	
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0');   	
  	      	thing[u][STRT].turtleX = Integer.parseInt(s.substring(begin,let));//  x coordinate
  	      	begin = let+1;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0');   	
	  	 //System.out.println("Exception  parselocation   s.substring(begin,let " +s.substring(begin,let));
  	      	thing[u][STRT].turtleY = Integer.parseInt(s.substring(begin,let));//  y coordinate
  	      
  	      	begin = let+1;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
            //if(let<s.length())  	
  	      	   thing[u][STRT].heading = Integer.parseInt(s.substring(begin,let));//  heading
  	      	
  	      	begin = let+1;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
            try
              {  	
              //  if(let<s.length())  	
         	      	thing[u][STRT].score = Integer.parseInt(s.substring(begin,let));//  score
  	          }
            catch(Exception e)
              {
	  	 		System.out.println("Exception "+e + " 1 parselocation  " +u+ " "+s.substring(begin-8,let+4));
   	  	
              }
             begin = let+1;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
            try
              {  	
              //  if(let<s.length())  	
         	      	thing[u][STRT].hitBy = Integer.parseInt(s.substring(begin,let));//  score
  	          }
            catch(Exception e)
              {
	  	 		System.out.println("Exception "+e + " 2 parselocation  " +u+ " "+s.substring(begin-8,let+4));
	  	 		System.out.println("Exception "+e + " 3 parselocation 931 " +u+ " "+s.substring(begin-1,let));
   	  	
              }
            begin = let+1;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
              
            try
              {  	
              //  if(let<s.length())  	
         	      	thing[u][STRT].team = Integer.parseInt(s.substring(begin,let));//  score
  	          }
            catch(Exception e)
              {
	  	 		System.out.println("Exception "+e + " 4 parselocation  " +u+ " "+s.substring(begin-8,let+4));
   	  	
              }

  	     
  	      	begin = let;
  	      	//continue with score and speedX and speedY     
//System.out.println("X "+ thing[u][STRT].turtleX+" Y "+ thing[u][STRT].turtleY); 	   //toReturn = "" + x +"," + y + ","+ speedX + "," + speedY + ","+ score +","+user;   	     	    
  	      }
             begin++;
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
            try
              {  	
              //  if(let<s.length())  	
         	      	chargerX = Integer.parseInt(s.substring(begin,let));//  score
  	          }
            catch(Exception e)
              {
	  	 		//System.out.println("Exception "+e + " 5 parselocation  "+s.substring(begin-8,let+4));
   	  	
              }
  	     
  	      	begin = let+1;
  	      	     
             do
               {
          	     let++;
               }
            while (let<s.length()&&s.charAt(let) <= '9'&& s.charAt(let)>='0'); 
            try
              {  	
              //  if(let<s.length())  	
         	      	chargerY = Integer.parseInt(s.substring(begin,let));//  score
  	          }
            catch(Exception e)
              {
	  	 		System.out.println("Exception "+e + " 6 parselocation  " +s.substring(begin-8,let+4));
   	  	
              }
  	     
  	      	begin = let;
  }
public static void sortScore(turtle[][] n,int num)
{
	turtle temp=new turtle();
	for (int x = 0; x< num-1;x++)
    	for (int y = x+1 ;y< num;y++)
    	   if(n[x][STRT].score < n[y][STRT].score)
    	       {
    	       	   temp = n[x][STRT];
    	       	   n[x][STRT] = n[y][STRT];
    	       	   n [y][STRT] = temp;
	         }
}	 
public static void sortcolors()
{
	String temp="";
	int t1 = 0;
	int t2 = 0;
	for (int x = 0; x< 9;x++)
    	for (int y = x+1 ;y< 10;y++)
    	   if( teamScores[x] < teamScores[y])
    	       {
    	       	   t2 =numInTeam[x];
    	       	   numInTeam[x]=numInTeam[y];
    	       	   numInTeam[y]= t2;
    	       	   temp = teamcolors[x];
    	       	   teamcolors[x]=teamcolors[y];
    	       	   teamcolors[y] = temp;
    	       	   t1 = teamScores[x];
    	       	   teamScores[x]=teamScores[y];
    	       	   teamScores[y] = t1;
	         }
}	 
public void mousePressed(MouseEvent m)
{
	mouseDragged(m);
	/*Point p = m.getPoint();
	int cx = p.x - thing[MYGUY][STRT].turtleX;
	int cy = p.y - thing[MYGUY][STRT].turtleY; 
   char code = 'X';
  if (!lostPoints)
     {
       	  if (cx>0)
			{	
       	    if (cy >0)
       	      {
       	         if( cx >=cy ) code = 'R';
       	         if( cx <cy ) code = 'D';
       	       }  
       	    else
       	      {
       	         if( cx >=cy*-1 ) code = 'R';
       	         if( cx <cy*-1 ) code = 'U';
       	       }  
       	    }   
       	  if (cx<0)
			{	
       	    if (cy >0)
       	      {
       	         if( cx*-1 >=cy ) code = 'L';
       	         if( cx*-1 <cy ) code = 'D';
       	       }  
       	    else
       	      {
       	         if( cx*-1 >=cy*-1 ) code = 'L';
       	         if( cx*-1 <cy*-1 ) code = 'U';
       	       }  
       	    }   
    }  	       
    clientOutput = "" + code;
  
   	  */
}
public void mouseClicked(MouseEvent m)
{
//	Point p = m.getPoint();
//	System.out.println("Clicked mouseX "+p.x+" Mouse Y "+p.y);
//	char code = 'X';
//	thing[MYGUY][STRT].heading += 4;
//	if(thing[MYGUY][STRT].heading>7)
//	   thing[MYGUY][STRT].heading -= 8;
//	if (thing[MYGUY][STRT].heading ==W ) code = 'W'; //Face west
//	if (thing[MYGUY][STRT].heading ==N ) code = 'N'; //Face north
//	if (thing[MYGUY][STRT].heading ==E ) code = 'E'; //Face east
//	if (thing[MYGUY][STRT].heading ==S ) code = 'S'; //Face south
//	if (thing[MYGUY][STRT].heading ==NW ) code = 'Q'; //Face northwest
//	if (thing[MYGUY][STRT].heading ==NE ) code = 'F'; //Face northeast
//	if (thing[MYGUY][STRT].heading ==SW ) code = 'Z'; //Face southwest
//	if (thing[MYGUY][STRT].heading ==SE ) code = 'C'; //Face southeast
//	clientOutput = "" + code;
}
public void mouseEntered(MouseEvent m)
{
//	Point p = m.getPoint();
//	System.out.println("Entered mouseX "+p.x+" Mouse Y "+p.y);
}
public void mouseExited(MouseEvent m)
{
}
public void mouseReleased(MouseEvent m)
{
	Point p = m.getPoint();
//	System.out.println("Clicked mouseX "+p.x+" Mouse Y "+p.y);
  	char code = 'X';
if (!lostPoints)
     {
	thing[MYGUY][STRT].heading += 4;
	if(thing[MYGUY][STRT].heading>7)
	   thing[MYGUY][STRT].heading -= 8;
	if (thing[MYGUY][STRT].heading ==W ) code = 'W'; //Face west
	if (thing[MYGUY][STRT].heading ==N ) code = 'N'; //Face north
	if (thing[MYGUY][STRT].heading ==E ) code = 'E'; //Face east
	if (thing[MYGUY][STRT].heading ==S ) code = 'S'; //Face south
	if (thing[MYGUY][STRT].heading ==NW ) code = 'Q'; //Face northwest
	if (thing[MYGUY][STRT].heading ==NE ) code = 'F'; //Face northeast
	if (thing[MYGUY][STRT].heading ==SW ) code = 'Z'; //Face southwest
	if (thing[MYGUY][STRT].heading ==SE ) code = 'C'; //Face southeast
}
	clientOutput = "" + code;
}
public void mouseMoved(MouseEvent m)
{
}
public void mouseDragged(MouseEvent m)
{
//	Point p = m.getPoint();
//	System.out.println("Dragged mouseX "+p.x+" Mouse Y "+p.y);
	Point p = m.getPoint();
  	int cx = p.x - thing[MYGUY][STRT].turtleX;
	int cy = p.y - thing[MYGUY][STRT].turtleY; 
    char code = 'X';
    double slope = 500;
    if (cx!=0)
         slope = (double)(cy)/(double)(cx);
    //slope *=-1;
if (!lostPoints)
     {
       	  if (cx>0)
			{	
       	    if (cy >0)
       	      {
       	         if (slope >= 2)
       	              code = 'D';
       	         else   
       	         if( slope <.5) 
       	             code = 'R';
       	         else
       	            code = 'H';
       	         //if( cx <cy ) 
 
       	       }  
       	    else
       	      {
        	     if( slope> -0.5 ) 
        	        code = 'R';
       	         else   
       	         if (slope < -2) 
       	            code = 'U';
        	     else    
       	         //if( cx <cy ) 
       	            code = 'Y';
       	       }  
       	    }   
       	  if (cx<0)
			{	
       	    if (cy >0)
       	      {
       	         if (slope > -0.5)  
       	            code = 'L';
       	         else 
       	         if(slope < -2)   
  					code = 'D';
       	         else
                    code = 'G';      	         //if( cx <cy ) 
       	         
       	       }  
       	    else
       	      {
       	         if (slope < 0.5)  
       	            code = 'L';
       	         else 
       	         if(slope > 2)   
  					code = 'U';
       	         else
                    code = 'T';      	         //if( cx <cy ) 
//       	         if (slope < -.5 && slope > -2) 
//       	            code = 'T';
//       	         else   
//        	         if( slope >-.5 ) code = 'L';
//       	         else
//       	         //if( cx*-1 <cy*-1 ) 
//       	           code = 'U';
       	       }  
       	    }   
       	       
 }   	  
    clientOutput = "" + code;
}
public void keyPressed(KeyEvent e)
{
   char code = 'X';
   String list = "";
   String Chteam="",newTeam="";
   int numInTeam,teamToChange,x,high,countTeam;
   //myTeam = 
  if (!lostPoints)
     {
      	  if (e.getKeyCode() ==moveDown ) code = 'D'; // control thing 1
       	  if (e.getKeyCode() ==moveUp ) code = 'U';
       	  if (e.getKeyCode() ==moveRight ) code = 'R';
       	  if (e.getKeyCode() ==moveLeft ) code = 'L';
      	  if (e.getKeyCode() ==moveBrake ) code = 'B'; //brake
      	 // if (e.getKeyCode() ==T ) code = 'T'; //teleport
      	  if (e.getKeyCode() ==faceWest ) code = 'W'; //Face west
      	  if (e.getKeyCode() ==faceNorth ) code = 'N'; //Face north
      	  if (e.getKeyCode() ==faceEast ) code = 'E'; //Face east
      	  if (e.getKeyCode() ==faceSouth ) code = 'S'; //Face south
      	  if (e.getKeyCode() ==faceNorth1 ) code = 'N'; //Face north
      	  if (e.getKeyCode() ==faceNorthWest ) code = 'Q'; //Face northwest
      	  if (e.getKeyCode() ==faceNorthEast ) code = 'F'; //Face northeast
      	  if (e.getKeyCode() ==faceSouthWest ) code = 'Z'; //Face southwest
      	  if (e.getKeyCode() ==faceSouthEast ) code = 'C'; //Face southeast
       	  if (e.getKeyCode() ==Escape ) gameNotOver = false;
       	  if (e.getKeyCode() ==menu ) 
       	     {
        	     	 list = "Enter the number of the player\nyou want to change teams.\n  ";
       	     	 high = 0;
       	     	 countTeam = 0;
       	     	 for (x = 0;x < NUMTHINGS;x++)
       	     	    {
       	     	      if (thing[x][STRT].team == thing[MYGUY][STRT].team) 
       	     	       {
       	     	       	  if (high < thing[x][STRT].score)
       	     	       	       high = thing[x][STRT].score;
       	     	          countTeam++;
       	     	          list += ""+ (x+1) + " "+thing[x][STRT].name + 
       	     	                " Score: "+thing[x][STRT].score + "\n"; 
       	     	        }        
       	     	    } 
       	     	 if(high > thing[MYGUY][STRT].score||countTeam==1)
       	     	     Chteam = ""+(MYGUY+1);
       	     	 else                  
       	     	 if(high <= thing[MYGUY][STRT].score||countTeam==1)
                     Chteam = JOptionPane.showInputDialog(list,"");        
       	     	 if (thing[Integer.parseInt(Chteam)-1][STRT].team == thing[MYGUY][STRT].team)
       	     	 {
       	     	 	list = "Enter the number of the NEW team\n"+
       	     	         "    0 black\n" +
       	     	         "    1 magenta\n" +
       	     	         "    2 blue\n" +
       	     	         "    3 cyan\n" +
       	     	         "    4 pink\n" +
       	     	         "    5 green\n" +
       	     	         "    6 dark green\n" +
       	     	         "    7 yellow\n" +
       	     	         "    8 red\n" +
       	     	         "    9 gray\n" ;
	                 e = null;
	                 newTeam = JOptionPane.showInputDialog(list,"");
	                 code = '~'; 
	                 if (newTeam.charAt(0) == '`' || newTeam.charAt(0) == '~')
	                    newTeam = ""+newTeam.charAt(1);
	                 else   
	                    newTeam = ""+newTeam.charAt(0);
	                 changeString = "~"+Chteam+"N"+newTeam; 
                     clientOutput = "";
                     outString = changeString;
                     sendPacket();
                 }      
       	     }	
//   switch(e.getKeyCode())
//       {
//       //	  case KeyEvent.VK_SHIFT :code = 'S';break;
//       	  case KeyEvent.VK_DOWN :code = 'D';break; // control thing 1
//       	  case KeyEvent.VK_UP :code = 'U';break;
//       	  case KeyEvent.VK_RIGHT :code = 'R';break;
//       	  case KeyEvent.VK_LEFT :code = 'L';break;
//      	  case KeyEvent.VK_SPACE :code = 'B';break; //brake
//      	 // case KeyEvent.VK_T :code = 'T';break; //teleport
//      	  case KeyEvent.VK_A :code = 'W';break; //Face west
//      	  case KeyEvent.VK_S :code = 'N';break; //Face north
//      	  case KeyEvent.VK_D :code = 'E';break; //Face east
//      	  case KeyEvent.VK_X :code = 'S';break; //Face south
//      	  case KeyEvent.VK_W :code = 'N';break; //Face north
//      	  case KeyEvent.VK_Q :code = 'Q';break; //Face northwest
//      	  case KeyEvent.VK_E :code = 'F';break; //Face northeast
//      	  case KeyEvent.VK_Z :code = 'Z';break; //Face southwest
//      	  case KeyEvent.VK_C :code = 'C';break; //Face southeast
//       	  case KeyEvent.VK_ESCAPE :gameNotOver = false;break;
//       	  //case KeyEvent.VK_ESCAPE :code = 'E';break;//quit
//       	  default: code = 'X';
//      }
}
   clientOutput = "" + code;
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
//public void update(Graphics G)
//{
//	  paint(G);
//}	    
public static void checkCollision(turtle t1,turtle t2)//keep for single player game
{
	int offx1 =t1.picWidth/2*t1.size;
	int offx2 =t2.picWidth/2*t2.size;
	int offy1 =t1.picHeight/2*t1.size;
	int offy2 =t2.picHeight/2*t2.size;

    int left1 = t1.turtleX-offx1;
    int right1 = t1.turtleX+offx1;
    int top1 = t1.turtleY-offy1;
    int bottom1 = t1.turtleY+offy1;
    int left2 = t2.turtleX-offx2;
    int right2 = t2.turtleX+offx2;
    int top2 = t2.turtleY-offy2;
    int bottom2 = t2.turtleY+offy2;
    int temp = 0;
	if (right1 >left2)
	    if (bottom1 > top2)
	        if (right2 > left1)
	            if( bottom2 > top1)
	                {
	                   temp =t1.speedX;
	                   t1.speedX = t2.speedX;
	                   t2.speedX = temp;
	                   temp =t1.speedY;
	                   t1.speedY = t2.speedY;
	                   t2.speedY = temp;
                       
                       t2.turtleX += t2.speedX;
                       t2.turtleY += t2.speedY;
	                 }  
}	
public void drawTree(Graphics Screen)
{
	     int rancolor =0;
   
         for (int j = 0;j<10;j++)
            for (int x = 0;x<1000;x++)
              {
            	rancolor = (int)(Math.random()*20)+1;
             	if (rancolor >14)
        	       sp.setcolor(5); 
        	    else    
        	       sp.setcolor(rancolor);
                sp.drawPixel(500 + (int)(Math.random()*x/3)-x/6,
                      x/2 + 50,(int)(Math.random()*8),Screen);	
             }
    



}
char GetUpChar(String title,String input)
{
    String inVal;
    inVal = JOptionPane.showInputDialog(title,input);        
    inVal = inVal.toUpperCase();
    return  inVal.charAt(0);
    	
}	   
public static void pause(int t)
	   {
               for (long x = 0; x< t;x++)
                  for (long y = 0; y< 50000;y++)
                         {
                         }
	   }
}
/*
public void keyPressed(KeyEvent e)//use for single player game
{
    setTitle(""+ KeyEvent.getKeyText(e.getKeyCode()));  
    System.out.println("hit + "+ KeyEvent.getKeyText(e.getKeyCode()));
    // limits speed
    if (thing[MYGUY][STRT].speedY >= 6) 
      {
    	down = 0;   
      }  else { down = 2;}
    	
   	if (thing[MYGUY][STRT].speedY <= -6) 
   	  {
    	up = 0;   
      }  
      else { up = 2;}
    	
    if (thing[MYGUY][STRT].speedX >= 6) {
    	right = 0;   
    	}  else { right = 2;}
    	
    if (thing[MYGUY][STRT].speedX <= -6) {
    	left = 0;   
    	}  else { left = 2;}
    
    switch(e.getKeyCode())
       {
       	  case KeyEvent.VK_SHIFT :turbo =0;break;
       	  case KeyEvent.VK_DOWN :thing[MYGUY][STRT].speedY +=down;break; // control thing 1
       	  case KeyEvent.VK_UP :thing[MYGUY][STRT].speedY -=up;break;
       	  case KeyEvent.VK_RIGHT :thing[MYGUY][STRT].speedX +=right;break;
       	  case KeyEvent.VK_LEFT :thing[MYGUY][STRT].speedX -=left;break;
      	  case KeyEvent.VK_SPACE :thing[MYGUY][STRT].speedX /=2;
      	                   thing[MYGUY][STRT].speedY /=2;turbo =0;break; //stop
       	  case KeyEvent.VK_ESCAPE :gameNotOver = false;break;
      	  
       }
   constructPacket(); 
    
}*/
