import java.net.*;
import java.io.*;
import java.util.*;

class NetPlayer extends Player 
{
   private Scanner in;
   private PrintWriter out;
   private static int port = 4444;
   private ServerSocket serverSocket;
   private Socket clientSocket;

   // Server Socket
   public NetPlayer (int pNum, int dir, String n, Board boardRef) throws IOException
   {
      super(pNum, dir, n, boardRef);
      serverSocket = new ServerSocket (port);
      clientSocket = serverSocket.accept();

      // use the InputStream object to create a Scanner.
      in = new Scanner (clientSocket.getInputStream());

      // use the OutputStream object to create a PrintWriter
      out = new PrintWriter (clientSocket.getOutputStream(), true);
   }

   // Client Socket
   public NetPlayer (int pNum, int dir, String n, String server, Board boardRef) throws 
                     UnknownHostException, IOException
   {
      super(pNum, dir, n, boardRef);
      clientSocket = new Socket(server, port);

      // use the InputStream object to create a Scanner.
      in = new Scanner (clientSocket.getInputStream());

      // use the OutputStream object to create a PrintWriter
      out = new PrintWriter (clientSocket.getOutputStream(), true);
   }
   
   public int playerType()
   {
      return 1;
   }

   public int endTurn()
   {
      this.isMyTurn = false;
      return this.opponent.setTurn();
   }
   
   public int setTurn()
   {
      this.isMyTurn = true;
      out.println("end");
      return this.getPlayerNumber();
   }
   
   public void sendMove(int col1, int row1, int col2, int row2)
   {
      out.println((7 - col1) + " " + (7 - row1) + " " + (7 - col2) + " " + (7 - row2));
   }
   
   public synchronized void waitForTurn(Board b)
   {
      String commandGetter = "";
      StringTokenizer st;
      int commandsGotten = 0, col1, row1, col2, row2;
      
      while (commandGetter != "end")
      {
         b.repaint();
         if (in.hasNextLine())
         {
            commandGetter = (in.nextLine()).intern();
            commandsGotten++;
            
            if (commandGetter == "end" && commandsGotten == 1)
            {
               hasWon = true;
               b.gameOver = true;
            }
            
/**            "col1 row1 col2 row2" */
            if (commandGetter != "end")
            {
               st = new StringTokenizer(commandGetter);
               
               col1 = Integer.parseInt(st.nextToken());
               row1 = Integer.parseInt(st.nextToken());
               col2 = Integer.parseInt(st.nextToken());
               row2 = Integer.parseInt(st.nextToken());
               
               b.draggedPiece = b.sq[col1][row1].contents;
               b.draggedPiece.executeMove(col2, row2, b);
               b.crownCheck(col2, row2);
               
               b.repaint();
            }
            
            if (commandGetter == "end" && commandsGotten > 1)
            {
               b.currentPlayer = b.players[b.thisPlayer].setTurn();
               b.setLabel();
               b.repaint();
            }
            
            if (!b.draggedPiece.isNull())
            {
               b.draggedPiece = new Pawn();
            }
         }
      }
   }
   
   public synchronized void run()
   {
      while (!b.gameOver)
      {
         while (isMyTurn)
         {
            waitForTurn(b);
         }
      }
   }
}