import javax.swing.*;
import java.awt.*;
import java.awt.GradientPaint;
import java.awt.event.*;
import java.util.Vector;
import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
 *  This class creates a Frame by extending JPanel.
 *  A Checker Board is created in the panel.
 *  public
 *  @author Casey Daughtery, Joshua Canter, Yvon Feaster 
 *  @version 4.0
*/
                                                                             
public class Board extends JPanel implements ComponentListener, ActionListener 
{
   protected Square sq[][];//space for a reference to 2D array of Square
   protected Piece pc[][];
   protected Vector<String> moveLog;
   protected Board boardRef;
   protected Piece draggedPiece;
   protected Player players[];
   protected int thisPlayer;
   protected int currentPlayer;
   protected boolean playerIsMidJump;
   protected static boolean notReady = true;
   protected static boolean gameOver = false;
   protected JLabel status1, status2, status3;
   protected JFrame frame;
   protected int topplayer = 1;
   protected int btmplayer = 0;
   protected Image image;
   protected Graphics offscreen;
   protected String set;

   /** Any constant values multiplied by width divided by 90
    *  represents a proportionality constant.  Since there are
    *  many constants that represent vastly different proportions,
    *  it is impractical to make them into constant variables.
   */
   protected int width;   
   /**
    *  This is the constructor.  
   */
   public Board()
   {
      JFrame frame = new JFrame("Phase 4-CPSC 215");
      frame.setSize (504,569);
      frame.add(this);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      this.frame = frame;
      sq = new Square[8][8]; //space for 64 references to squares
      pc = new Piece[2][16];
      moveLog = new Vector<String>();
      players = new Player[2];
      draggedPiece = new Pawn ();
      addMouseListener (new DemoAdapter());
      addMouseMotionListener (new DemoAdapter());
      addComponentListener (this);
      setupMenu(frame);
      setupLabel(frame);
      currentPlayer = 0;

      playerIsMidJump = false;
      width = (this.getSize().height < this.getSize().width) ?
                         this.getSize().height / 8 : this.getSize().width / 8; 
                               repaint();
   }//end constructor
   
   /**
    *  This method sets up the board (8 X 8) with all the squares.
    *  It sets the color of each square, determines if the square is 
    *  able to receive a Piece, sets up an array of 12 pawns per Player
    *  and informs the square of its Pawn
    *  public
    *  @param args    Command Arguments
    *
   */
   public void setUpBoard(String [] args)
   {
     int topcount = 0, btmcount = 0;
     initPlayer(args);
     //sets up the squares


     for(int row = 0; row < 8; row++)
     {
         for(int col = 0; col < 8; col++)
         {           
            sq[col][row] = new Square(col, row, width);  //creates 64 squares
     

            if ( (row % 2) == (col % 2) )
            {
               sq[col][row].color = new Color(181, 164, 254);
            }
            else
            {
               sq[col][row].color = new Color(19, 1, 95);
            }

           
	          if (row == 0 && (col == 0 || col == 7))
 	          {
 	             pc[topplayer][topcount] = new Rook(col * width, 
 	                                                row * width, 
 	                                                width, 
                                                  players[topplayer].color,
                                                  players[topplayer],
                                                  topcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[topplayer][topcount];
               topcount++;
            }

            if (row == 0 && (col == 1 || col == 6))
 	          {
 	             pc[topplayer][topcount] = new Knight(col * width,
 	                                                  row * width,
 	                                                  width,
                                                    players[topplayer].color,
                                                    players[topplayer],
                                                    topcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[topplayer][topcount];
               topcount++;
            }
            
            if (row == 0 && (col == 2 || col == 5))
 	          {
 	             pc[topplayer][topcount] = new Bishop(col * width,
 	                                                  row * width,
 	                                                  width,
                                                    players[topplayer].color,
                                                    players[topplayer],
                                                    topcount, set);

               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[topplayer][topcount];
               topcount++;
            }
            
            if (row == 0 && col == 3)
 	          {
 	             if(players[topplayer].playerNumber == 1)
 	             {
    	            pc[topplayer][topcount] = new Queen(col * width,
    	                                                row * width,
    	                                                width,
                                                      players[topplayer].color,
                                                      players[topplayer],
                                                      topcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[topplayer][topcount];
                  topcount++;
               }
               else
               {
    	            pc[topplayer][topcount] = new King(col * width,
    	                                               row * width,
    	                                               width,
                                                     players[topplayer].color,
                                                     players[topplayer],
                                                     topcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[topplayer][topcount];
                  topcount++;
               }
            }
            
            if (row == 0 && col == 4)
 	          {
 	             if(players[topplayer].playerNumber == 1)
 	             {
   	              pc[topplayer][topcount] = new King(col * width,
    	                                               row * width,
    	                                               width,
                                                     players[topplayer].color,
                                                     players[topplayer],
                                                     topcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[topplayer][topcount];
                  topcount++;
               }
               else
               {
    	            pc[topplayer][topcount] = new Queen(col * width,
    	                                                row * width,
    	                                                width, 
                                                      players[topplayer].color,
                                                      players[topplayer],
                                                      topcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[topplayer][topcount];
                  topcount++;
               }
            }
            
            if (row == 1)
            {
               pc[topplayer][topcount] = new Pawn(col * width,
                                                  row * width,
                                                  width,
                                                  players[topplayer].color,
                                                  players[topplayer],
                                                  topcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[topplayer][topcount];
               topcount++;
            } 


            if (row == 7 && (col == 0 || col == 7))
 	          {
 	             pc[btmplayer][btmcount] = new Rook(col * width,
 	                                                row * width,
 	                                                width,
                                                  players[btmplayer].color,
                                                  players[btmplayer],
                                                  btmcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[btmplayer][btmcount];
               btmcount++;
            }
            
            if (row == 7 && (col == 1 || col == 6))
 	          {
 	             pc[btmplayer][btmcount] = new Knight(col * width,
 	                                                  row * width,
 	                                                  width,
                                                    players[btmplayer].color,
                                                    players[btmplayer],
                                                    btmcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[btmplayer][btmcount];
               btmcount++;
            }
            
            if (row == 7 && (col == 2 || col == 5))
 	          {
 	             pc[btmplayer][btmcount] = new Bishop(col * width,
 	                                                  row * width,
 	                                                  width,
                                                    players[btmplayer].color,
                                                    players[btmplayer],
                                                    btmcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[btmplayer][btmcount];
               btmcount++;
            }
            
            if (row == 7 && col == 3)
 	          {
 	             if(players[btmplayer].playerNumber == 0)
 	             { 	          
   	              pc[btmplayer][btmcount] = new Queen(col * width,
    	                                                row * width,
    	                                                width,
                                                      players[btmplayer].color,
                                                      players[btmplayer],
                                                      btmcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[btmplayer][btmcount];
                  btmcount++;
               }
               else
               { 	          
    	            pc[btmplayer][btmcount] = new King(col * width,
    	                                               row * width,
    	                                               width,
                                                     players[btmplayer].color,
                                                     players[btmplayer],
                                                     btmcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[btmplayer][btmcount];
                  btmcount++;
               }
            }
            
            if (row == 7 && col == 4)
 	          {
 	             if(players[btmplayer].playerNumber == 0)
 	             {
   	              pc[btmplayer][btmcount] = new King(col * width,
   	                                                 row * width,
   	                                                 width,
                                                     players[btmplayer].color,
                                                     players[btmplayer],
                                                     btmcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[btmplayer][btmcount];
                  btmcount++;
               }
               else
               {
   	              pc[btmplayer][btmcount] = new Queen(col * width,
   	                                                  row * width,
   	                                                  width, 
                                                      players[btmplayer].color,
                                                      players[btmplayer],
                                                      btmcount, set);
               
                  sq[col][row].hasPiece = true;
                  sq[col][row].contents = pc[btmplayer][btmcount];
                  btmcount++;
               }
            }
            
            if (row == 6)
            {
               pc[btmplayer][btmcount] = new Pawn(col * width,
                                                  row * width,
                                                  width,
                                                  players[btmplayer].color,
                                                  players[btmplayer],
                                                  btmcount, set);
               
               sq[col][row].hasPiece = true;
               sq[col][row].contents = pc[btmplayer][btmcount];
               btmcount++;
            }             
         }//end row
      }//end col      
    // repaint();
      players[0].setPieceArray(pc[0]);
      players[1].setPieceArray(pc[1]);
      //starts the threads
      players[0].start();
      players[1].start();
      
      currentPlayer = 0;
      try{wait(3000);}
        catch(Exception e){};
      repaint();

   }//end constructor 
   
   /**
    *  This method initializes the players and netplayers in accordance with 
    *  the command line argurments
    *  @param args   Command line arguments
   */
   public void initPlayer(String args[])
   {  
       //client 
       if(args.length > 1)
       {
          try
          {
             btmplayer = 1;
             topplayer = 0;
             players[0] = new NetPlayer(0, 1, "Light", args[0], boardRef);
             players[1] = new Player(1, -1, args[1], boardRef);
             thisPlayer = 1;
             players[0].setOpponent(players[1]);
             players[1].setOpponent(players[0]);
          }
          catch (UnknownHostException e)
          {
             System.err.println ("Can't connect to server " + args[0] + e);
          }
          catch (IOException e)
          {
             System.err.println ("Could not connect to port 4444 on server: "
                                 + args[0] + ": " + e);
          }
       }
       //server
       else
       {
          try
          {
             btmplayer = 0;
             topplayer = 1;
             players[0] = new Player (0, -1, args[0], boardRef);
             players[1] = new NetPlayer (1, 1, "Dark", boardRef);
             thisPlayer = 0;
             players[0].setOpponent(players[1]);
             players[1].setOpponent(players[0]);
          }
          catch (IOException e)
          {
             System.err.println ("Could not connect to port 4444: " + e);
          }
       }
       status1.setText("It is now " + players[currentPlayer].name + 
                        "'s turn.");   
   }
       
      
   
   /** 
    *  This method overrides the JPanel paint method.
    *  Calls each square's drawSquare method
    *  Calls drawPiece on the draggedPiece if there is one. 
    *  Implements Double Buffering
    *  public
    *  @param g    uses Graphics passed in 
   */
   public void paint(Graphics g)
   {

      if (notReady)
         return;
      //  setting image to the size of the current  board and sets
      //  offscreen to same spec. of the image             
      image = createImage(width*8, width*8);
      offscreen = image.getGraphics();

      //tells to paint to offscreen
      super.paint(offscreen);

      for ( int row = 0;  row < 8;  row++ ) 
      {
         for ( int col = 0;  col < 8;  col++)   
         {
            sq[col][row].drawSquare(offscreen,boardRef);
         }//end col
      }//end row

      if (!draggedPiece.isNull())
      {
         draggedPiece.drawPiece(offscreen,boardRef);
      }
      //paints the image 
      g.drawImage(image, 0, 0, this);
      
      

   }//end paint
   
   /**
     *  This class is used to reset the board when and
     *  instance of it is invoked.  It extends AbstractAction.
     *  public 
     *   
    */ 
   class resetBoard extends AbstractAction
   {
      public resetBoard()
      {
      }
      
      public void actionPerformed(ActionEvent e)
      {
         boardRef.sq = new Square[8][8]; //space for 64 references to squares
         boardRef.pc = new Piece[2][12];
         boardRef.players = new Player[2];
         boardRef.draggedPiece = new Pawn ();
         boardRef.currentPlayer = 0;
//         boardRef.setUpBoard();

         boardRef.repaint();
      }
   }
   /**
    *  This class is used to exit the program.  It extends AbstractAction
    *  public
    */
   class closeBoard extends AbstractAction
   {
      /**
       *  constructor
       */
      public closeBoard()
      {
      }
      
      /**
       *  This method exits the program.
       *  @param e     Instance of ActionEven
       */
      public void actionPerformed(ActionEvent e)
      {
         System.exit(0);
      }
   }
   /**
    *  This class allows for the choice of changing checker
    *  colors from the normal colors to Clemson theme
    *  
    */

   class checkerClemson extends AbstractAction
   {
      /**
       *  Constructor
       */
      public void checkerClemson()
      {
      }
      /**
       *  This method changes the color theme of the checker
       *  and square 
       *  @param e    Instance of ActionEvent
       */
      public void actionPerformed(ActionEvent e)
      {

         players[0].color = new Color(255,100,0);
      
         players[1].color = new Color(69,0,82);
         //changes the squares     
         for(int row = 0; row < 8; row++)
         {
            for(int col = 0; col < 8; col++)
            {           
           
               if ( (row % 2) == (col % 2) )
               {
                   sq[col][row].color = new Color(255, 250, 244);
               }
               else
               {
                  sq[col][row].color = new Color(123, 87, 101);
               }            
            }
         }
            status1.setText("It is now " + players[currentPlayer].name + 
                         "'s turn.");

         repaint();
      }
   }

   /**
    *  This class allows for the choice of standard checker colors rather than 
    *  Clemson University Colors.  This class extends AbstractAction
    *  
    */
   class checkerNormal extends AbstractAction
   {
      /**
       *  Constructor
       */
      public void checkerNormal()
      {

      }
      
      /**
       *  This method changes the color of the pieces and squares
       *  @param e      Instance of ActionEvent
       */
      public void actionPerformed(ActionEvent e){
         //checker
         players[0].color = new Color(255,0,0);
         players[1].color = new Color(0,0,0);
 
        //square
        for(int row = 0; row < 8; row++)
        {
            for(int col = 0; col < 8; col++)
            {           
            
               if ( (row % 2) == (col % 2) )
               {
                   sq[col][row].color = new Color(181, 164, 254);
               }
               else
               {
                  sq[col][row].color = new Color(19, 1, 95);
               }            
           }
        }
            status1.setText("It is now " + players[currentPlayer].name + 
                         "'s turn.");

        repaint();
      }
   }
   
   /**
    *  This method sets us the menu bar with File and Option
    *  File allows for exiting of the game.
    *  Option allows a choice of normal theme or Clemson theme
    *  @param f      JFrame
    */             
   public void setupMenu(JFrame f)
   {
      
      JMenuItem close, newGame, normColor, clemColor;
      JMenu file, options, color; 
      Action reset, exit, normalColor, clemsonColor;
      JMenuBar jmb = new JMenuBar();

      f.setJMenuBar(jmb);
      file = new JMenu("File");
      file.setMnemonic(KeyEvent.VK_F);
      jmb.add(file);

      options = new JMenu("Options");
      options.setMnemonic(KeyEvent.VK_O);
      jmb.add(options);

      reset = new resetBoard();
      exit = new closeBoard();
                           //      newGame = new JMenuItem();
      close = new JMenuItem();
      normalColor = new checkerNormal();
      clemsonColor = new checkerClemson();

                            /*      newGame.setAction(reset);
                                    newGame.setText("New Game");
                                    newGame.setMnemonic(KeyEvent.VK_N);*/

      close.setAction(exit);
      close.setText("Exit");
      close.setMnemonic(KeyEvent.VK_X);

                 //newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
                 //ActionEvent.ALT_MASK));
      close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
      ActionEvent.ALT_MASK));

      file.addActionListener(this);
                 //      newGame.addActionListener(this);
      close.addActionListener(this);

                 //      file.add(newGame);
      file.addSeparator();
      file.add(close); 

      color = new JMenu("Colors");
      normColor = new JMenuItem();
      clemColor = new JMenuItem();

      normColor.setAction(normalColor);
      clemColor.setAction(clemsonColor);

      normColor.setText("Normal Scheme");
      clemColor.setText("Clemson Scheme");

      clemColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
                               ActionEvent.ALT_MASK));

      options.addActionListener(this);
      color.addActionListener(this);
      normColor.addActionListener(this);
      clemColor.addActionListener(this);

      color.setMnemonic(KeyEvent.VK_C);
      color.add(normColor);
      color.add(clemColor);
      options.add(color);
      
      
   }
   /**
     *  Creates a JLabel to display information about Player and turns.
     *  @param f
    */
   public void setupLabel(JFrame f)
   {
      JPanel panel = new JPanel(new GridLayout(2,2, 0, 0));
      
      status1 = new JLabel("Player 1's turn");
      panel.add(status1);
      panel.setVisible(true);
      f.add(status1, BorderLayout.SOUTH);
   }
   
   /**
     *  This method overrides an interface method
     *  @param e
    */      
   public void actionPerformed(ActionEvent e) 
   {
   }
               
   /**
     *  This nested class is to allow the use of the MouseAdapter class
     *  Extends MouseAdapter
     *  public
    */ 
   public class DemoAdapter extends MouseAdapter 
   {
  
     /** 
      *  This method is used to identify the location of the mouse click
      *  in relation to the board in general if there is a Piece at this
      *  location it becomes the draggedPiece
      *  public
      *  @param e   Instance of MouceEvent
     */
      public void mousePressed(MouseEvent e)
      {
         if (notReady)
            return;
      
         if (currentPlayer != thisPlayer)
            return;

         if (playerIsMidJump)
            return;

         int row = 0;
         int col = 0;
         col = e.getX() / width;
         row = e.getY() / width;

         if (row > 7 || col > 7)
         {
            return;
         }
         
         if (sq[col][row].contents == null)
         {
            return;
         }

         if (!sq[col][row].contents.isNull())         
         {
            if (sq[col][row].contents.player.playerNumber == currentPlayer)
            {
               draggedPiece = sq[col][row].contents;
               draggedPiece.player = sq[col][row].contents.player;
            }
         }
         
         if (!draggedPiece.isNull())
         {
            draggedPiece.setXpos(e.getX()-(width / 2));
            draggedPiece.setYpos(e.getY()-(width / 2));
            repaint();
         }
      }
      
      /**
       *  This method sets the draggedPiece to follow the mouse and calls
       *  repaint(). 
       *  public
       *  @param e   Instance of MouseEvent
      */        
      public void mouseDragged(MouseEvent e) 
      {
         int row = 0;
         int col = 0; 
         col = e.getX() / width;
         row = e.getY() / width;
         if (!draggedPiece.isNull())
         {
            draggedPiece.setXpos(e.getX()-(width / 2));
            draggedPiece.setYpos(e.getY()-(width / 2));
            repaint();
         }
      }
      
      /**
       *  This method sets the draggedPiece to follow the mouse and calls
       *  repaint() only if the player is forced to multi-jump. 
       *  public
       *  @param e   Instance of MouseEvent
      */          
      public void mouseMoved(MouseEvent e) 
      {
         if (playerIsMidJump)
         {
            int row = 0;
            int col = 0; 
            col = e.getX() / width;
            row = e.getY() / width;
            if (!draggedPiece.isNull())
            {
               draggedPiece.setXpos(e.getX()-(width / 2));
               draggedPiece.setYpos(e.getY()-(width / 2));
               repaint();
            }
         }
      }
      
      /**
       *  This method determines where the mouse is released.  If the Checker 
       *  is released outside of the board it returns it to the original 
       *  square. It coordinates calls to the methods playerMustJump, moveIsValid
       *  and executeMove.  
       *  public
       *  @param e  Instance of MouseEvent
      */
      public void mouseReleased(MouseEvent e) 
      {
         int row = e.getY() / width; 
         int col = e.getX() / width;
         if (draggedPiece.isNull())
         {
            repaint();
            return;
         }

         //checks if outside the board
         if (col > 7 || row > 7 || col < 0 || row < 0)  
         {
            resetPiece();
         }
         else
         {
            handleMove(col, row);
         }
         if (!playerIsMidJump)
         {
            draggedPiece = new Pawn ();
         }
         repaint();
      }
   }


   /**
    *  This method is used to handle the move of a piece
    *  @param col     Column of move
    *  @param row     Row of move
    */
   public void handleMove(int col, int row)
   {
      boolean validMove, mustJump, doesJump;
      validMove = draggedPiece.isMoveValid(col, row, boardRef);
//      mustJump = players[currentPlayer].playerMustJump(boardRef);
//      doesJump = (Math.abs(col-draggedPiece.getCol()) == 2);

      // if this is a valid move
      if (validMove /* && (!mustJump || (mustJump && doesJump)) */)
      {
/*         if (Math.abs(col-draggedPiece.getCol()) == 2)
         {
            playerIsMidJump = true;
         } */

         if(players[currentPlayer].specialMove.intern() == "Promotion")
         {
            String[] choices = {"Bishop", "Knight", "Rook", "Queen"};
            int result = JOptionPane.showOptionDialog(null, "Choose a piece to promote to.", "Promotion", JOptionPane.YES_NO_OPTION, 
                           JOptionPane.PLAIN_MESSAGE, null, choices, "Bishop");
   
            switch (result){
               case 0:
                  draggedPiece = new Bishop(draggedPiece);
                  players[currentPlayer].setPromotion("B");
                  break;
               case 1:
                  draggedPiece = new Knight(draggedPiece);
                  players[currentPlayer].setPromotion("K");
                  break;
               case 2:
                  draggedPiece = new Rook(draggedPiece);
                  players[currentPlayer].setPromotion("R");
                  break;
               case 3:
                  draggedPiece = new Queen(draggedPiece);
                  players[currentPlayer].setPromotion("Q");
                  break;
            }
         }
         
//         System.out.println("Local Player is about to send the move.");
         players[currentPlayer].opponent.sendMove(draggedPiece.coord.x, 
                                                  draggedPiece.coord.y, 
                                                  col, row,
                                                  players[currentPlayer].
                                                  specialMove);

         draggedPiece.executeMove(col, row, boardRef, players[currentPlayer]);

//         crownCheck(col, row);
               
//         mustJump = players[currentPlayer].
//               playerMustJump(boardRef, draggedPiece.getPieceNumber());

/*         if (!(mustJump && playerIsMidJump))
         { */
            playerIsMidJump = false;
            currentPlayer = players[currentPlayer].endTurn();
            setLabel();
/*         } */

      }//end if
      else
      {
         //Puts piece back to original pos
         resetPiece();
      }//end else   
   }

   /**
    *  This method returns the piece back to its original postion 
    *  
    */ 
   public void resetPiece()
   {
      draggedPiece.setXpos(draggedPiece.getCol() * width);
      draggedPiece.setYpos(draggedPiece.getRow() * width);
   }
   /**
    *  This method checks if a piece should be crowned.
    *  @param col    Column
    *  @param row    Row
    */
/*   public void crownCheck (int col, int row)
   {
      if (row == 7 || row == 0)
      {
         if (!(sq[col][row].contents.isKing()))
         {
            crownedKing(col, row);
         }
      }
   } */

   /**
    *  This method creates a new king and crowns it.
    *  @param col    Column
    *  @param row    Row
    */
   public void crownedKing(int col, int row)
   {
      sq[col][row].contents = new King(sq[col][row].contents);
      pc[sq[col][row].contents.player.getPlayerNumber()]
        [sq[col][row].contents.getPieceNumber()] = sq[col][row].contents;
      sq[col][row].contents.player.pieces
        [sq[col][row].contents.getPieceNumber()] = sq[col][row].contents;
      playerIsMidJump = false;
   }
   
   /**
    *  This method sets the label for the status bar.
    */
   public void setLabel()
   {
      //sets Game over label
/*      if (!players[currentPlayer].mayMove(boardRef))
      {
         status1.setText("Game over: " + players[currentPlayer].
                         name + " loses, " + players[currentPlayer].
                         opponent.name + " wins!");
         gameOver = true;
      }
      
      //sets label for whose turn
      else
      { */
         status1.setText("It is now " + players[currentPlayer].
                         name + "'s turn.");
                                     
//         System.out.println("The turn has changed, checking for check.");

         boolean inCheck, hasMoves;
         
         inCheck = players[currentPlayer].isInCheck();
         hasMoves = players[currentPlayer].hasAnyMoves();
         
         if (inCheck)
         {
            status1.setText(status1.getText() + "  Check.");
         }
         
         if (!hasMoves)
         {
            if (inCheck)
            {
               status1.setText(status1.getText() + "  Game Over: Checkmate.");
               players[currentPlayer].endTurn();
            }
            else
            {
               status1.setText(status1.getText() + "  Game Over: Stalemate.");
               players[currentPlayer].endTurn();
            }
         }
//    } 
   }

   /**
     *  This method is called in the event that the board is resized
     *  @param e    ComponentEvent
    */
     
   public void componentResized(ComponentEvent e)
   {
      if (notReady)
         return;
      
      width = (this.getSize().height < this.getSize().width) ?
               this.getSize().height / 8 : this.getSize().width / 8; 
                 
      for (int col = 0; col < 8; col++)
      {
         for (int row = 0; row < 8; row++)
         {
            sq[col][row].setWidth(width);
            sq[col][row].setXpos(col * width);
            sq[col][row].setYpos(row * width);
            if(sq[col][row].hasPiece)
            {
               sq[col][row].contents.setWidth(width);
               sq[col][row].contents.setXpos(sq[col][row].contents.
                  getCol() * width);
               sq[col][row].contents.setYpos(sq[col][row].contents.
                  getRow() * width);
            }
         }
      }
      repaint();
   }
   /**
     *  This method overrides an interface method
     *  @param e     ComponentEvent
    */ 
   public void componentHidden(ComponentEvent e)
   {
   }
   
   /**
     *  This method overrides an interface method
     *  @param e     ComponentEvent
    */ 
   public void componentMoved(ComponentEvent e)
   {
   }

   /**
     *  This method overrides an interface method
     *  @param e      ComponentEvent
    */      
   public void componentShown(ComponentEvent e)
   {
   }
   /**
     *  This metond sets the gameOver
     *  public
    */
   public void endGame()
   {
      gameOver = true;
   }
   
   public void promptSet()
   {
      String[] choices = {"Realistic", "Abstract", "Bunny Ears"};
      int result = JOptionPane.showOptionDialog(null, "Choose a Chess set", "New Game", JOptionPane.YES_NO_OPTION, 
                     JOptionPane.PLAIN_MESSAGE, null, choices, "Realistic");
   
      switch (result){
         case 0:
            set = new String("realistic/");
            break;
         case 1:
            set = new String("freehand/");
            break;
         case 2:
            set = new String("bunnyears/");
            break;
      }
   }
            
   /**
    *   This method is the driver of the Board.  It creates a
    *   new Board and calls the method setUpBoard.
    *   public static
    *   @param args    command argruments
   */
   public static void main(String [] args)
   {
      //checks to make sure there are some command line arguments
      if (!(args.length > 0))
      {
         System.err.println("Please enter your name if a server or");
         System.err.println("a server name and your name if a client.");
         return;         
      }

     
      Board b = new Board();
      b.promptSet(); 
      b.boardRef = b;


      b.setUpBoard(args);            

      notReady = false;
      b.repaint();
   }//end main
}//end Board class
