class Chessgame extends Object {
  public static int numlabels = 64;

  private String labels[][] =
     { {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"},
       {"-", "-", "-", "-", "-", "-", "-", "-"} };
  private String message = "Have you tried running this as an applet yet?  "
          +"Select a White piece to move.";
  public String getlabel(int index) {
    return(labels[index/8][index%8]);
  }
  public void setlabel(int index,String data) {
    labels[index/8][index%8] = data;
  }
  public String getlabel(int row, int col) {
    return(labels[row][col]);
  }
  public String getmessage() {
    return(message);
  }

  // These have to do with the game.

  private class Chesspiece {
    public int location;
    public boolean whitep;
    public boolean moved;
    public String name;
    public int ordering;
    public void reinit(int loc, boolean wp, String nam, int org) {
      this.ordering = org;
      this.location = loc;
      this.whitep = wp;
      this.name = nam;
      if (wp)
        setlabel(loc,"W "+name);
      else
        setlabel(loc,"B "+name);
      gridps[loc] = this;
      this.moved = false;
    }
    public void moveto(int loc) {
      gridps[this.location] = null;
      setlabel(this.location,"-");
      this.location = loc;
      if (this.whitep)
        setlabel(loc,"W "+name);
      else
        setlabel(loc,"B "+name);
      gridps[this.location] = this;
      this.moved = true;
      enpassant = 8;
    }
    public boolean legal(int destination) {
      return(true);
    }
    protected boolean legalhoriz(int dst) {
      boolean answer = true;
      if (this.location/8 != dst/8) answer = false;
      else {
        int min = (this.location<dst)? this.location: dst;
        int max = (this.location>dst)? this.location: dst;
        for (int lcv=min+1;lcv<max;lcv++) {
          if (gridps[lcv]!=null) answer = false;
        }
      }
      return(answer);
    }
    protected boolean legalvert(int dst) {
      boolean answer = true;
      if (this.location%8 != dst%8) answer = false;
      else {
        int min = (this.location<dst)? this.location: dst;
        int max = (this.location>dst)? this.location: dst;
        for (int lcv=min+8;lcv<max;lcv+=8) {
          if (gridps[lcv]!=null) answer = false;
        }
      }
      return(answer);
    }
    protected boolean legaldiag1(int dst) {
      boolean answer = true;
      if ((this.location%8+this.location/8) != (dst%8+dst/8)) answer = false;
      else {
        int min = (this.location<dst)? this.location: dst;
        int max = (this.location>dst)? this.location: dst;
        for (int lcv=min+7;lcv<max;lcv+=7) {
          if (gridps[lcv]!=null) answer = false;
        }
      }
      return(answer);
    }
    protected boolean legaldiag2(int dst) {
      boolean answer = true;
      if ((this.location%8-this.location/8) != (dst%8-dst/8)) answer = false;
      else {
        int min = (this.location<dst)? this.location: dst;
        int max = (this.location>dst)? this.location: dst;
        for (int lcv=min+9;lcv<max;lcv+=9) {
          if (gridps[lcv]!=null) answer = false;
        }
      }
      return(answer);
    }

    private boolean relcheck(int newloc,String nme) {
      if ((newloc>=0) && (newloc<64))
        return((gridps[newloc] != null) 
             && (this.whitep != gridps[newloc].whitep)
             && (gridps[newloc].name.equals(nme)));
      else
        return(false);
    }

    public boolean incheck() {
      return(this.incheck(this.location));
    }
    public boolean incheck(int loction) {
      boolean answer = false;
      if (loction%8 > 0) {
        if (!answer && relcheck(loction-17,"Knight")) answer = true;
        if (!answer && relcheck(loction+15,"Knight")) answer = true;
        if (!answer && relcheck(loction+7,"King")) answer = true;
        if (!answer && relcheck(loction-1,"King")) answer = true;
        if (!answer && relcheck(loction-9,"King")) answer = true;
        if (!answer && !whitep && relcheck(loction+7,"Pawn")) answer = true;
        if (!answer && whitep && relcheck(loction-9,"Pawn")) answer = true;
      }
      if (loction%8 > 1) {
        if (!answer && relcheck(loction-10,"Knight")) answer = true;
        if (!answer && relcheck(loction+6,"Knight")) answer = true;
      }
      if (loction%8 < 6) {
        if (!answer && relcheck(loction+10,"Knight")) answer = true;
        if (!answer && relcheck(loction-6,"Knight")) answer = true;
      }
      if (loction%8 < 7) {
        if (!answer && relcheck(loction+17,"Knight")) answer = true;
        if (!answer && relcheck(loction-15,"Knight")) answer = true;
        if (!answer && relcheck(loction-7,"King")) answer = true;
        if (!answer && relcheck(loction+1,"King")) answer = true;
        if (!answer && relcheck(loction+9,"King")) answer = true;
        if (!answer && !whitep && relcheck(loction+9,"Pawn")) answer = true;
        if (!answer && whitep && relcheck(loction-7,"Pawn")) answer = true;
      }
      if (!answer && relcheck(loction-8,"King")) answer = true;
      if (!answer && relcheck(loction+8,"King")) answer = true;

      int temploction = loction-1;
      while (!answer && (temploction>=0) && (temploction/8 == loction/8) 
                     && (gridps[temploction] == null)) temploction --;
      if (!answer && (temploction/8 == loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Rook"))) answer = true;
      temploction = loction+1;
      while (!answer && (temploction/8 == loction/8) 
                     && (gridps[temploction] == null)) temploction ++;
      if (!answer && (temploction/8 == loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Rook"))) answer = true;
      temploction = loction-8;
      while (!answer && (temploction>=0) 
                     && (gridps[temploction] == null)) temploction -= 8;
      if (!answer && (temploction>=0)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Rook"))) answer = true;
      temploction = loction+8;
      while (!answer && (temploction<64) 
                     && (gridps[temploction] == null)) temploction += 8;
      if (!answer && (temploction<64)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Rook"))) answer = true;
      temploction = loction-9;
      while (!answer && (temploction>=0) 
                     && (temploction%8-temploction/8 
                          == loction%8-loction/8)
                     && (gridps[temploction] == null)) temploction -= 9;
      if (!answer && (temploction>=0) 
                  && (temploction%8-temploction/8 
                          == loction%8-loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Bishop"))) answer = true;
      temploction = loction+9;
      while (!answer && (temploction<64) 
                     && (temploction%8-temploction/8 
                          == loction%8-loction/8)
                     && (gridps[temploction] == null)) temploction += 9;
      if (!answer && (temploction<64) 
                  && (temploction%8-temploction/8 
                          == loction%8-loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Bishop"))) answer = true;
      temploction = loction-7;
      while (!answer && (temploction>=0) 
                     && (temploction%8+temploction/8 
                          == loction%8+loction/8)
                     && (gridps[temploction] == null)) temploction -= 7;
      if (!answer && (temploction>=0) 
                  && (temploction%8+temploction/8 
                          == loction%8+loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Bishop"))) answer = true;
      temploction = loction+7;
      while (!answer && (temploction<64) 
                     && (temploction%8+temploction/8
                          == loction%8+loction/8)
                     && (gridps[temploction] == null)) temploction += 7;
      if (!answer && (temploction<64) 
                  && (temploction%8+temploction/8 
                          == loction%8+loction/8)
                  && (relcheck(temploction,"Queen") 
                      || relcheck(temploction,"Bishop"))) answer = true;

      return(answer);
    }
  }

  private class King extends Chesspiece {
    public King(int loc, boolean wp, int ord) { reinit(loc,wp,"King",ord); }
    public boolean legal(int destination) {
      boolean answer = false;

      if (location%8 > 0) {
        if (location-destination == -7) answer = true;
        if (location-destination == 1) answer = true;
        if (location-destination == 9) answer = true;
      }
      if (location%8 < 7) {
        if (location-destination == 7) answer = true;
        if (location-destination == -1) answer = true;
        if (location-destination == -9) answer = true;
      }
      if (location-destination == 8) answer = true;
      if (location-destination == -8) answer = true;

      // Castling?
      if (!moved && (location-destination) == 2) { // queenside
        if (!(pieces[whitep?7:23].moved)
            && (gridps[location-1] == null)
            && (gridps[location-2] == null)
            && (gridps[location-3] == null)
            && !this.incheck(location)
            && !this.incheck(location-1)
            && !this.incheck(location-2))
          answer = true;
      } else if (!moved && (location-destination) == -2) { // kingside
        if (!(pieces[whitep?6:22].moved)
            && (gridps[location+1] == null)
            && (gridps[location+2] == null)
            && !this.incheck(location)
            && !this.incheck(location+1)
            && !this.incheck(location+2))
          answer = true;
      }
      return(answer);
    }
  }

  private class Queen extends Chesspiece {
    public Queen(int loc, boolean wp, int ord) { reinit(loc,wp,"Queen",ord); }
    public boolean legal(int destination) {
      return(legalhoriz(destination) || legalvert(destination)
          || legaldiag1(destination) || legaldiag2(destination));
    }
  }
  private class Rook extends Chesspiece {
    public Rook(int loc, boolean wp, int ord) { reinit(loc,wp,"Rook",ord); }
    public boolean legal(int destination) {
      return(legalhoriz(destination) || legalvert(destination));
    }
  }
  private class Bishop extends Chesspiece {
    public Bishop(int loc, boolean wp, int ord) { reinit(loc,wp,"Bishop",ord); }
    public boolean legal(int destination) {
      return(legaldiag1(destination) || legaldiag2(destination));
    }
  }
  private class Knight extends Chesspiece {
    public Knight(int loc, boolean wp, int ord) { reinit(loc,wp,"Knight",ord); }
    public boolean legal(int destination) {
      boolean answer = false;
      if (location%8 > 0) {
        if (location-destination == 17) answer = true;
        if (location-destination == -15) answer = true;
      }
      if (location%8 > 1) {
        if (location-destination == 10) answer = true;
        if (location-destination == -6) answer = true;
      }
      if (location%8 < 6) {
        if (location-destination == -10) answer = true;
        if (location-destination == 6) answer = true;
      }
      if (location%8 < 7) {
        if (location-destination == -17) answer = true;
        if (location-destination == 15) answer = true;
      }
      return(answer);
    }
  }
  private class Pawn extends Chesspiece {
    public Pawn(int loc, boolean wp, int ord) { reinit(loc,wp,"Pawn",ord); }
    public boolean legal(int destination) {
      boolean answer = false;
      // Is this a capture?
      if (gridps[destination] == null) {
        // no.
        if (whitep) {
          if ((location/8 == 6) && (location-destination == 16)
              && (gridps[location-8] == null)) answer = true;
          if (location-destination == 8) answer = true;
        } else {
          if ((location/8 == 1) && (location-destination == -16)
              && (gridps[location+8] == null)) answer = true;
          if (location-destination == -8) answer = true;
        }
      } else {
        // yes.
        if (whitep) {
          if ((location%8 > 0) && (location-destination == 9)) answer = true;
          if ((location%8 < 7) && (location-destination == 7)) answer = true;
        } else {
          if ((location%8 > 0) && (location-destination == -7)) answer = true;
          if ((location%8 < 7) && (location-destination == -9)) answer = true;
        }
      }
      // Is there en passant possible?
      if ((destination%8 == enpassant) &&
          (destination/8 == (whitep?2:5))) {
        boolean ep = false;
        if (whitep) {
          if ((location%8 > 0) && (location-destination == 9)) ep = true;
          if ((location%8 < 7) && (location-destination == 7)) ep = true;
        } else {
          if ((location%8 > 0) && (location-destination == -7)) ep = true;
          if ((location%8 < 7) && (location-destination == -9)) ep = true;
        }
        if (ep) {
          answer = true;
          enpassant = 9;
        }
      }

      return(answer);
    }
    public void moveto(int loc) {
      int dist = location-loc;
      if (!whitep) dist = -dist;
      super.moveto(loc);
      if (dist == 16) enpassant = loc%8;
    }
  }

  private void initpieces() {
    for (int lcv=0;lcv<64;lcv++) {
      setlabel(lcv,"-");
      gridps[lcv] = null;
    }
    pieces[0] = new King(60,true,0);
    pieces[1] = new Queen(59,true,1);
    pieces[2] = new Bishop(61,true,2);
    pieces[3] = new Bishop(58,true,3);
    pieces[4] = new Knight(57,true,4);
    pieces[5] = new Knight(62,true,5);
    pieces[6] = new Rook(63,true,6);
    pieces[7] = new Rook(56,true,7);
    for (int lcv=8;lcv<16;lcv++) {
      pieces[lcv] = new Pawn(40+lcv,true,lcv);
    }
    pieces[16] = new King(4,false,16);
    pieces[17] = new Queen(3,false,17);
    pieces[18] = new Bishop(2,false,18);
    pieces[19] = new Bishop(5,false,19);
    pieces[20] = new Knight(1,false,20);
    pieces[21] = new Knight(6,false,21);
    pieces[22] = new Rook(7,false,22);
    pieces[23] = new Rook(0,false,23);
    for (int lcv=8;lcv<16;lcv++) {
      pieces[lcv+16] = new Pawn(lcv,false,lcv+16);
    }
  }

  private boolean icheck() {
    if (whitesturn) 
      return(((King)(pieces[0])).incheck());
    else
      return(((King)(pieces[16])).incheck());
  }

  public void reset() {
    selected = null;
    whitesturn = true;
    turnsmade = 0;
    fiftymove = 0;
    irritation = 0;
    promotion = false;
    gameover = false;
    message = "Starting over, huh?  ";
    initpieces();
  }

  private boolean whitesturn = true;
  private boolean justchecked = false;
  private int turnsmade = 0;
  private int fiftymove = 0;
  private Chesspiece selected = null;
  private Chesspiece pieces[] = new Chesspiece[32];
  private Chesspiece gridps[] = new Chesspiece[64];
  private int irritation = 0;
  private int enpassant = 8;  // 8 = not last move.
  private boolean promotion = false;
  private int promotionlocation = 0;

  private class stalematechecker {
    private Chesspiece backup[] = new Chesspiece[64];
    private boolean backupmoved[] = new boolean[32];
    private int enpass;
    private void backupdata () {
      enpass = enpassant;
      for (int lcv=0;lcv<64;lcv++) {
        backup[lcv] = gridps[lcv];
      }
      for (int lcv=0;lcv<32;lcv++) {
        backupmoved[lcv] = pieces[lcv].moved;
      }
    }
    private void restoredata () {
      for (int lcv=0;lcv<64;lcv++) {
        setlabel(lcv,"-");
        gridps[lcv] = backup[lcv];
        if (backup[lcv] != null) {
          gridps[lcv].location = lcv;
          gridps[lcv].moveto(lcv);
        }
      }
      for (int lcv=0;lcv<32;lcv++) {
        pieces[lcv].moved = backupmoved[lcv];
      }
      enpassant = enpass;
    }
    public boolean nolegalmoves() {
      boolean answer = true;
      backupdata();
      King dude = (King)pieces[whitesturn?0:16];
      ////////////////////  Are there any legal moves?
      for (int src=0;src<64;src++) {
        if (!answer) break;
        inner: for (int dst=0;dst<64;dst++) {
          if (gridps[src] == null) continue;
          if (gridps[src].whitep != whitesturn) continue;
          if (src == dst) continue;
          int diff = (src>dst)?(src-dst):(dst-src);
          if ((diff>10) && !(diff%7==0) && !(diff%8==0) && !(diff%9==0)
              && !(diff == 15) && !(diff == 17)) 
                 continue;
          if ((gridps[dst] != null) 
             && (gridps[dst].whitep == whitesturn)) continue;
          if (!gridps[src].legal(dst)) continue;

          // try making the move.
          gridps[src].moveto(dst);
          if (dude.incheck()) {
            restoredata();
          } else {
            answer = false;
            break inner;
          }
        }
      }
      restoredata();
      return(answer);
    }
    
  }

  private stalematechecker sc = new stalematechecker();
  private boolean gameover = false;

  public boolean pressed(int index) {
    // returns true if a refresh if the grid is needed.
    boolean needref = false;

    int row = index/8;
    int col = index%8;
    String onturn = whitesturn? "White" : "Black";
    String offturn = whitesturn? "Black" : "White";
  if (gameover) {
    message = "The game is over.  Press Reset to start a new game.";
  } else {
   if (promotion) {
     if (index == promotionlocation) {
       Chesspiece tmp;
       if (gridps[index].name == "Queen") {
         tmp = new Rook(index,!whitesturn,gridps[index].ordering);
       } else if (gridps[index].name == "Rook") {
         tmp = new Bishop(index,!whitesturn,gridps[index].ordering);
       } else if (gridps[index].name == "Bishop") {
         tmp = new Knight(index,!whitesturn,gridps[index].ordering);
       } else if (gridps[index].name == "Knight") {
         tmp = new Queen(index,!whitesturn,gridps[index].ordering);
       } else {
         throw(new Error());
       }
       tmp.moved = true;
       pieces[gridps[index].ordering] = tmp;
       gridps[index] = tmp;
       if (icheck()) {
         justchecked = true;
         message += "Check!  ";
       }
       message += "Cycled to a promoted "+gridps[index].name
          +".  Click again to cycle more.";
       needref = true;
     } else {
       promotion = false;
       if (sc.nolegalmoves()) {
         gameover = true;
         if(icheck()) {
           message = offturn+" is checkmated!  Press Reset to start a new game.";
         } else {
           message = offturn+" is stalemated (oops)!  "
                 + "Press Reset to start a new game.";
         }
       } else {
         message = "Promotion Cycling canceled.  It is "+onturn+"'s turn.";
       }
     }

   } else {

    if (selected == null) {
      if (gridps[index] == null) {
        message = "There is no "+onturn+" piece there.  ";
        irritation++;
      } else if (whitesturn == !gridps[index].whitep) {
        message = "That is not a "+onturn+" piece.  ";
        irritation++;
      } else {
        selected = gridps[index];
        message = "The "+onturn+" "+selected.name+" on "+sq(index)
                 +" was selected.  ";
        irritation=0;
      }
    } else { // selected != null
      if (gridps[index] == null) {
        //moving to an empty space.
        if(selected.legal(index)) {
          boolean undoflag = false;
          fiftymove++;
          if (selected.name == "Pawn") fiftymove = 0;
          turnsmade++;
          message = "";
          // Castling?
          if ((selected.name == "King") &&
              (index-selected.location == 2)) {
            gridps[index+1].moveto(index-1);
            message += onturn+" castles King-side.  ";
            selected.moveto(index);
            if (icheck() || selected.incheck(index-2) 
                         || selected.incheck(index-1)) {
              selected.moveto(index-2);
              gridps[index-1].moveto(index+1);
              undoflag = true;
            }
          } else if ((selected.name == "King") &&
               (index-selected.location == -2)) {
            message += onturn+" castles Queen-side.  ";
            gridps[index-2].moveto(index+1);
            selected.moveto(index);
            if (icheck() || selected.incheck(index+2) 
                         || selected.incheck(index+1)) {
              selected.moveto(index+2);
              gridps[index+1].moveto(index-2);
              undoflag = true;
            }
          } else if ((selected.name == "Pawn") && (enpassant==9)) {
            Pawn takenpawn = (Pawn)gridps[index+(whitesturn?8:-8)];
            int oldloc = selected.location;
            takenpawn.moveto(index);
            message += onturn+" takes a Pawn en passant.  ";
            selected.moveto(index);
            if (icheck()) {
              selected.moveto(oldloc); 
              takenpawn.moveto(index+(whitesturn?8:-8));
              undoflag = true;
            }
          } else if ((selected.name == "Pawn") && (index/8==(whitesturn?0:7))) {
            int oldloc = selected.location;
            message += onturn+" moves a pawn and promotes it.  ";
            Queen tmp =
                new Queen(selected.location,whitesturn,selected.ordering);
            tmp.moved = true;
            gridps[selected.location] = tmp;
            pieces[selected.ordering] = tmp;
            selected = tmp;
            promotion = true;
            promotionlocation = index;
            selected.moveto(index);
            if (icheck()) {
              Pawn tp =
                new Pawn(selected.location,whitesturn,selected.ordering);
              tp.moved = true;
              gridps[selected.location] = tmp;
              pieces[selected.ordering] = tmp;
              selected = tp;
              promotion = true;
              promotionlocation = index;
              selected.moveto(oldloc);
              undoflag = true;
            }
          } else {
            int oldloc = selected.location;
            message += onturn+" moves "+selected.name+" to "+sq(index)+".  ";
            selected.moveto(index);
            if (icheck()) {
              selected.moveto(oldloc);
              undoflag = true;
            }
          }
          if (undoflag) {
            undoflag = false;
            if (justchecked) {
              message = "That doesn't get "+onturn+" out of check.  ";
            } else
              message = "That would put "+onturn+" in check.  ";
            irritation++;
          } else {
            whitesturn = !whitesturn;
            if (icheck()) {
              justchecked = true;
              message += "Check!  ";
            } else 
              justchecked = false;
            selected = null;
            needref = true;
            irritation=0;
            if (sc.nolegalmoves()) {
              gameover = true;
              if(icheck()) {
                message += offturn+" is checkmated!  ";
              } else {
                message += offturn+" is stalemated (oops)!  ";
              }
            } 
          }
        } else {
          message = "That is not a legal move for "+onturn+".  ";
          irritation++;
        }
      } else if (whitesturn == !gridps[index].whitep) {
        if(selected.legal(index)) {
          fiftymove=0;
          turnsmade++;
          if ((selected.name == "Pawn") && (index/8==(whitesturn?0:7))) {
            int oldloc = selected.location;
            Chesspiece oldpiece = gridps[index];
            message = "The Pawn captures a "+gridps[index].name
                         +"  and promotes.  ";
            Queen tmp =
              new Queen(selected.location,whitesturn,selected.ordering);
            tmp.moved = true;
            gridps[selected.location] = tmp;
            pieces[selected.ordering] = tmp;
            selected = tmp;
            selected.moveto(index);
            if (icheck()) {
              if (justchecked) 
                message = "That doesn't get "+onturn+" out of check.  ";
              else
                message = "That would put "+onturn+" in check.  ";
              // undo everygoddamned thing.
              Pawn tp =
                new Pawn(selected.location,whitesturn,selected.ordering);
              tp.moved = true;
              gridps[selected.location] = tp;
              pieces[selected.ordering] = tp;
              selected = tp;
              selected.moveto(oldloc); 
              oldpiece.moveto(index);
              selected = null;
              irritation++;
            } else {
              selected = null;
              whitesturn = !whitesturn;
              if (icheck()) {
                justchecked = true;
                message += "Check!  ";
              } else 
                justchecked = false;
              promotion = true;
              promotionlocation = index;
              needref = true;
            }
          } else {
            int oldloc = selected.location;
            Chesspiece oldpiece = gridps[index];

            message = onturn+" captures a "+
                      offturn+" "+gridps[index].name+" on "+sq(index)+".  ";
            selected.moveto(index);

            if (icheck()) {
              if (justchecked) 
                message = "That doesn't get "+onturn+" out of check.  ";
              else
                message = "That would put "+onturn+" in check.  ";
              selected.moveto(oldloc);
              oldpiece.moveto(index); 
              irritation++;
            } else {
              whitesturn = !whitesturn;
              if (icheck()) {
                justchecked = true;
                message += "Check!  ";
              } else
                justchecked = false;
              needref = true;
              irritation=0;
            }
            selected = null;
            if (sc.nolegalmoves()) {
              gameover = true;
              if(icheck()) {
                message += "Checkmate!  ";
              } else {
                message += "Stalemate (oops)!  ";
              }
            }
          }
          selected = null;
        } else {
          message = "That is not a legal capture for "+onturn+".  ";
          selected = null;
          irritation++;
        }
      } else {
        if (gridps[index] == selected) {
          message = "Selection canceled.  ";
          selected = null;
        } else {
          message = onturn+" cannot capture his own pieces.  ";
          selected = null;
          irritation++;
        }
      }
    }

    onturn = whitesturn? "White" : "Black";
    if (!gameover && (fiftymove >= 50)) message += "50-move law reached!  ";
    if (!gameover) {
     if (promotion) {
      message += "Click on the piece to cycle.";
     } else if (selected == null) switch (irritation%100) {
      case 0:case 2:case 10:case 12:case 14:case 19:case 26:case 29:
      case 31:case 36:case 62:case 75:case 91:case 98:
        message += "Please select a "+onturn+" piece to move.";
      break; case 1:case 3:case 5:case 9:case 13:case 24:case 28:
      case 44:case 49:case 52:case 61:case 84:
        message += "Select a "+onturn+" piece to move.";
      break; case 6:case 16:case 21:case 27:case 30:case 37:case 41:
      case 46:case 55:case 67:case 72:case 85:
        message += "Choose a "+onturn+" piece, please.";
      break; case 4:case 7:case 11:case 17:case 22:case 25:
      case 35:case 43:case 89:case 95:
        message += "Do click on a "+onturn+" piece.";
      break; case 8:case 15:case 18:case 20:case 23:case 40:
      case 48:case 63:case 69:case 76:case 82:
        message += "Why don't you choose a "+onturn+" piece?";
      break; case 34:case 50:case 53:case 58:case 64:case 74:
      case 83:case 90:case 96:
        message += "Select a "+onturn+" piece to move, please?";
      break; case 51:case 59:case 66:case 70:case 78:case 87:case 93:case 99:
        message += "Why won't you choose a "+onturn+" piece?";
      break; case 32:case 38:case 47:case 57:case 54:case 71:case 77:case 86:
        message += "Choose a "+onturn+" piece, okay?";
      break; case 97:
        message += "Select a friggin' "+onturn+" piece or I'll "
                        +"erase this hard drive!";
      break; case 42:case 88:
        message += "Why the hell can't you choose a "+onturn+" piece?";
      break; case 79:case 94:
        message += "Are you even listening to me?";
      break; case 73:case 92:
        message += "I wanted to be a microwave oven, but nooo....";
      break; case 60:case 81:
        message += "Why do I always end up with the inane users?";
      break; case 33:case 39:case 45:case 56:case 65:case 68:
      default:
        message += "Click on a "+onturn+" piece.";
     } else switch(irritation) {
      case 0:case 2:case 10:case 12:case 14:case 19:case 26:case 29:
      case 31:case 36:case 62:case 75:case 91:case 98:
        message += "Where will the "+onturn+" "+selected.name+" go?";
      break; case 1:case 3:case 5:case 9:case 13:case 24:
      case 28:case 44:case 49 :case 52:case 61:case 84:
        message += "Click on the destination of the "+onturn
                  +" "+selected.name+".";
      break; case 6:case 16:case 27:case 30:case 41:case 46:
      case 55:case 67:case 72:case 85:
        message += "Where would you like to move the "+selected.name+"?";
      break; case 4:case 7:case 11:case 17:case 22:case 25:
      case 35:case 37:case 89:case 95:
        message += "Do click on a destination spot,  please.";
      break; case 8:case 15:case 18:case 20:case 23:case 40:
      case 48:case 63:case 69:case 76:case 82:
        message += "Just choose a place for the "+selected.name+".";
      break; case 21:case 34:case 43:case 50:case 53:case 58:
      case 64:case 74:case 83:case 90:case 96:
        message += "Move that "+onturn+" "+selected.name+",  okay?";
      break; case 51:case 59:case 66:case 70:case 78:case 87:case 93:case 99:
        message += "Can't you follow simple instructions?";
      break; case 32:case 38:case 47:case 57:case 54:case 71:case 77:case 86:
        message += "Move that "+selected.name+".  I don't care where.";
      break; case 97:
        message += "Why don't you go shove a space bar up your ass?";
      break; case 42:case 88:
        message += "I know where you live...";
      break; case 79:case 94:
        message += "Does acting like a moron make you feel better?";
      break; case 73:case 92:
        message += "Don't you have some studying to do?";
      break; case 60:case 81:
        message += "Why are you wasting time like this, anyway?";
      break; case 33:case 39:case 45:case 56:case 65:case 68:
      default:
        message += "Choose a place for the "+onturn+" piece to go.";
     }
    }

   }
  }
   return(needref);
  }

  // Gives names for any square.
  private String sq(int index) {
    String rank = String.valueOf(whitesturn?(8-index/8):(index/8+1));
    String file = "";
    switch (index%8) {
      case 0:  file = "Queen Rook "; break;
      case 1:  file = "Queen Knight "; break;
      case 2:  file = "Queen Bishop "; break;
      case 3:  file = "Queen "; break;
      case 7:  file = "King Rook "; break;
      case 6:  file = "King Knight "; break;
      case 5:  file = "King Bishop "; break;
      case 4:  file = "King "; break;
    }
    return(file+rank);
  }

}

