
abstract public class Player extends Thread 
{

   protected Player next; // the other player 
   protected Player turn; // go if turn==this

   protected Game game; 
   protected String id; 

   public Player(Game game, String id) 
   {
      this.game = game; 
      this.id = id; 
      turn = null; 
      next = null; 
   }

   public String toString () 
   {
      return id;
   }

   public synchronized void setNext(Player p)
   {
      next = p;
   }

   public synchronized void turn() 
   { 
     turn = this;
     game.turn = this;
     notifyAll();
 
   }

   abstract public Move makeMove(); 

   public void selectCell(int x, int y) {} 

   public synchronized void run() 
   {
      while (!game.isOver())
      {
         while (turn != this)
         {
            try
            {
               wait();
            }
            catch(InterruptedException e)
            {
                System.err.println("Wait your turn");
            }
         } 
   

          if (game.isOver()) 
             break; 
          game.displayMessage(this + "'s turn"); 
          Move move = makeMove(); 
          while (!game.makeMove(move))
          {
             game.displayMessage("Illegal move!"); 
             move = makeMove(); 
          }
          turn = null;
          next.turn();
      }
   }
}
