public static String posibleB(int i) {
   String list = "", oldPiece;
   int r = i / 8, c = i % 8;
   for (int j = -1; j <= 1; j += 2) {
     for (int k = -1; k <= 1; k += 2) {
       int temp = 1;
       try {
         while (" ".equals(chessBoard.get(r + temp * j, c + temp * k))) {
           oldPiece = chessBoard.get(r + temp * j, c + temp * k);
           chessBoard.set(r, c, " ");
           chessBoard.set(r + temp * j, c + temp * k, "B");
           if (kingSafe()) {
             list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece;
           }
           chessBoard.set(r, c, "B");
           chessBoard.set(r + temp * j, c + temp * k, oldPiece);
           temp++;
         }
         if (Character.isLowerCase(chessBoard.get(r + temp * j, c + temp * k).charAt(0))) {
           oldPiece = chessBoard.get(r + temp * j, c + temp * k);
           chessBoard.set(r, c, " ");
           chessBoard.set(r + temp * j, c + temp * k, "B");
           if (kingSafe()) {
             list = list + r + c + (r + temp * j) + (c + temp * k) + oldPiece;
           }
           chessBoard.set(r, c, "B");
           chessBoard.set(r + temp * j, c + temp * k, oldPiece);
         }
       } catch (Exception e) {
       }
     }
   }
   return list;
 }
  public static String possibleKing(int i) {
    String list = "", oldPiece;
    int r = i / 8, c = i % 8;
    for (int j = 0; j < 9; j++) {
      if (j != 4) {
        try {
          if (Character.isLowerCase(chessBoard.get(r - 1 + j / 3, c - 1 + j % 3).charAt(0))
              || " ".equals(chessBoard.get(r - 1 + j / 3, c - 1 + j % 3))) {
            oldPiece = chessBoard.get(r - 1 + j / 3, c - 1 + j % 3);
            chessBoard.set(r, c, " ");
            chessBoard.set(r - 1 + j / 3, c - 1 + j % 3, "A");

            if (kingSafe()) {
              list = list + r + c + (r - 1 + j / 3) + (c - 1 + j % 3) + oldPiece;
            }
            chessBoard.set(r, c, "A");
            chessBoard.set(r - 1 + j / 3, c - 1 + j % 3, oldPiece);
          }
        } catch (Exception e) {
        }
      }
    }

    // Y is castling 7472. Z is castling 7476
    // TODO without the explicit check for "A", opponent kings spawn out of
    // nowhere. Must be bug in move rating
    if (!kingCMoved) {
      if (chessBoard.get(7, 4).equals("A")
          && !rook56Moved
          && (chessBoard.get(7, 0).equals("R"))
          && (chessBoard.get(7, 1).equals(" "))
          && (chessBoard.get(7, 2).equals(" "))
          && (chessBoard.get(7, 3).equals(" "))) {
        castleLeft();
        if (kingSafe()) {
          list = list + "7472Y";
        }
        uncastleLeft();
      }
      if (chessBoard.get(7, 4).equals("A")
          && !rook63Moved
          && (chessBoard.get(7, 7).equals("R"))
          && (chessBoard.get(7, 6).equals(" "))
          && (chessBoard.get(7, 5).equals(" "))) {
        castleRight();
        if (kingSafe()) {
          list = list + "7476Z";
        }
        uncastleRight();
      }
    }
    return list;
  }
 static int kingPositionC() {
   int kingPositionC = 0;
   while (!"A".equals(chessBoard.get(kingPositionC / 8, kingPositionC % 8))) {
     kingPositionC++;
   }
   return kingPositionC;
 }
 public static String posibleMoves() {
   String list = "";
   for (int i = 0; i < 64; i++) {
     switch (chessBoard.get(i / 8, i % 8)) {
       case "P":
         list += posibleP(i);
         break;
       case "R":
         list += posibleR(i);
         break;
       case "K":
         list += possibleKnight(i);
         break;
       case "B":
         list += posibleB(i);
         break;
       case "Q":
         list += posibleQ(i);
         break;
       case "A":
         list += possibleKing(i);
         break;
     }
   }
   return list; // x1,y1,x2,y2,captured piece
 }
  public static void flipBoard() {
    String temp;
    for (int i = 0; i < 32; i++) {
      int r = i / 8, c = i % 8;
      temp = switchCase(chessBoard.get(r, c));
      chessBoard.set(r, c, switchCase(chessBoard.get(7 - r, 7 - c)));
      chessBoard.set(7 - r, 7 - c, temp);
    }

    boolean movedTemp = kingCMoved;
    kingCMoved = kingLMoved;
    kingLMoved = movedTemp;

    boolean rook56Temp = rook56Moved;
    rook56Moved = rook7Moved;
    rook7Moved = rook56Temp;

    boolean rook63Temp = rook63Moved;
    rook63Moved = rook0Moved;
    rook0Moved = rook63Temp;
  }
 public static void undoMove(String move) {
   if (move.charAt(4) == 'E') { // enpassant left
     unEnpassantLeft(move);
   } else if (move.charAt(4) == 'N') { // enpassant right
     unEnpassantRight(move);
   } else if (move.charAt(4) == 'Z') {
     uncastleRight();
   } else if (move.charAt(4) == 'Y') {
     uncastleLeft();
   } else if (move.charAt(4) == 'P') {
     // if pawn promotion
     chessBoard.set(1, move.charAt(0), "P");
     chessBoard.set(0, move.charAt(1), String.valueOf(move.charAt(2)));
   } else {
     chessBoard.set(
         move.charAt(0), move.charAt(1), chessBoard.get(move.charAt(2), move.charAt(3)));
     chessBoard.set(move.charAt(2), move.charAt(3), String.valueOf(move.charAt(4)));
   }
 }
 public static String possibleKnight(int i) {
   String list = "", oldPiece;
   int r = i / 8, c = i % 8;
   for (int j = -1; j <= 1; j += 2) {
     for (int k = -1; k <= 1; k += 2) {
       try {
         if (Character.isLowerCase(chessBoard.get(r + j, c + k * 2).charAt(0))
             || " ".equals(chessBoard.get(r + j, c + k * 2))) {
           oldPiece = chessBoard.get(r + j, c + k * 2);
           chessBoard.set(r, c, " ");
           chessBoard.set(r + j, c + k * 2, "K");
           if (kingSafe()) {
             list = list + r + c + (r + j) + (c + k * 2) + oldPiece;
           }
           chessBoard.set(r, c, "K");
           chessBoard.set(r + j, c + k * 2, oldPiece);
         }
       } catch (Exception e) {
       }
       try {
         if (Character.isLowerCase(chessBoard.get(r + j * 2, c + k).charAt(0))
             || " ".equals(chessBoard.get(r + j * 2, c + k))) {
           oldPiece = chessBoard.get(r + j * 2, c + k);
           chessBoard.set(r, c, " ");
           chessBoard.set(r + j * 2, c + k, "K");
           if (kingSafe()) {
             list = list + r + c + (r + j * 2) + (c + k) + oldPiece;
           }
           chessBoard.set(r, c, "K");
           chessBoard.set(r + j * 2, c + k, oldPiece);
         }
       } catch (Exception e) {
       }
     }
   }
   return list;
 }
  public static boolean squareSafe(int position) {
    // bishop/queen
    for (int i = -1; i <= 1; i += 2) {
      for (int j = -1; j <= 1; j += 2) {
        int temp = 1;
        try {
          while (" ".equals(chessBoard.get(position / 8 + temp * i, position % 8 + temp * j))) {
            temp++;
          }
          if ("b".equals(chessBoard.get(position / 8 + temp * i, position % 8 + temp * j))
              || "q".equals(chessBoard.get(position / 8 + temp * i, position % 8 + temp * j))) {
            return false;
          }
        } catch (Exception e) {
        }
      }
    }
    // rook/queen
    for (int i = -1; i <= 1; i += 2) {
      int temp = 1;
      try {
        while (" ".equals(chessBoard.get(position / 8, position % 8 + temp * i))) {
          temp++;
        }
        if ("r".equals(chessBoard.get(position / 8, position % 8 + temp * i))
            || "q".equals(chessBoard.get(position / 8, position % 8 + temp * i))) {
          return false;
        }
      } catch (Exception e) {
      }
      temp = 1;
      try {
        while (" ".equals(chessBoard.get(position / 8 + temp * i, position % 8))) {
          temp++;
        }
        if ("r".equals(chessBoard.get(position / 8 + temp * i, position % 8))
            || "q".equals(chessBoard.get(position / 8 + temp * i, position % 8))) {
          return false;
        }
      } catch (Exception e) {
      }
      temp = 1;
    }
    // knight
    for (int i = -1; i <= 1; i += 2) {
      for (int j = -1; j <= 1; j += 2) {
        try {
          if ("k".equals(chessBoard.get(position / 8 + i, position % 8 + j * 2))) {
            return false;
          }
        } catch (Exception e) {
        }
        try {

          if ("k".equals(chessBoard.get(position / 8 + i * 2, position % 8 + j))) {
            return false;
          }
        } catch (Exception e) {
        }
      }
    }
    // pawn
    if (position >= 16) {
      try {
        if ("p".equals(chessBoard.get(position / 8 - 1, position % 8 - 1))) {
          return false;
        }
      } catch (Exception e) {
      }
      try {
        if ("p".equals(chessBoard.get(position / 8 - 1, position % 8 + 1))) {
          return false;
        }
      } catch (Exception e) {
      }
      // king
      for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
          if (i != 0 || j != 0) {
            try {
              if ("a".equals(chessBoard.get(position / 8 + i, position % 8 + j))) {
                return false;
              }
            } catch (Exception e) {
            }
          }
        }
      }
    }
    return true;
  }
 public static String posibleP(int i) {
   String list = "", oldPiece;
   int r = i / 8, c = i % 8;
   for (int j = -1; j <= 1; j += 2) {
     try { // capture
       if (Character.isLowerCase(chessBoard.get(r - 1, c + j).charAt(0)) && i >= 16) {
         oldPiece = chessBoard.get(r - 1, c + j);
         chessBoard.set(r, c, " ");
         chessBoard.set(r - 1, c + j, "P");
         if (kingSafe()) {
           list = list + r + c + (r - 1) + (c + j) + oldPiece;
         }
         chessBoard.set(r, c, "P");
         chessBoard.set(r - 1, c + j, oldPiece);
       }
     } catch (Exception e) {
     }
     try { // promotion && capture
       if (Character.isLowerCase(chessBoard.get(r - 1, c + j).charAt(0)) && i < 16 && i >= 8) {
         String[] temp = {"Q", "R", "B", "K"};
         for (int k = 0; k < 4; k++) {
           oldPiece = chessBoard.get(r - 1, c + j);
           chessBoard.set(r, c, " ");
           chessBoard.set(r - 1, c + j, temp[k]);
           if (kingSafe()) {
             // column1,column2,captured-piece,new-piece,P
             list = list + c + (c + j) + oldPiece + temp[k] + "P";
           }
           chessBoard.set(r, c, "P");
           chessBoard.set(r - 1, c + j, oldPiece);
         }
       }
     } catch (Exception e) {
     }
   }
   try { // move one up
     if (" ".equals(chessBoard.get(r - 1, c)) && i >= 16) {
       chessBoard.set(r, c, " ");
       chessBoard.set(r - 1, c, "P");
       if (kingSafe()) {
         list = list + r + c + (r - 1) + c + " ";
       }
       chessBoard.set(r, c, "P");
       chessBoard.set(r - 1, c, " ");
     }
   } catch (Exception e) {
   }
   try { // promotion && no capture
     if (" ".equals(chessBoard.get(r - 1, c)) && i < 16) {
       String[] temp = {"Q", "R", "B", "K"};
       for (int k = 0; k < 4; k++) {
         chessBoard.set(r, c, " ");
         chessBoard.set(r - 1, c, temp[k]);
         if (kingSafe()) {
           // column1,column2,captured-piece,new-piece,P
           list = list + c + c + " " + temp[k] + "P";
         }
         chessBoard.set(r, c, "P");
         chessBoard.set(r - 1, c, " ");
       }
     }
   } catch (Exception e) {
   }
   try { // move two up
     if (" ".equals(chessBoard.get(r - 1, c))
         && " ".equals(chessBoard.get(r - 2, c))
         && i >= 48
         && i < 56) {
       chessBoard.set(r, c, " ");
       chessBoard.set(r - 2, c, "P");
       if (kingSafe()) {
         list = list + r + c + (r - 2) + c + " "; // double pawn move
       }
       chessBoard.set(r, c, "P");
       chessBoard.set(r - 2, c, " ");
     }
   } catch (Exception e) {
   }
   try { // left en passant simple: not really: trial
     if ("p".equals(chessBoard.get(r, c - 1))
         && pawnTwoUp(opponentMoves.get(opponentMoves.size() - 1))) {
       chessBoard.set(r - 1, c - 1, "P");
       chessBoard.set(r, c, " ");
       chessBoard.set(r, c - 1, " ");
       if (kingSafe()) {
         list = list + r + c + (r - 1) + (c - 1) + "E";
       }
       chessBoard.set(r - 1, c - 1, " ");
       chessBoard.set(r, c, "P");
       chessBoard.set(r, c - 1, "p");
     }
   } catch (Exception e) {
   }
   try { // left en passant simple: not really: trial
     if ("p".equals(chessBoard.get(r, c + 1))
         && pawnTwoUp(opponentMoves.get(opponentMoves.size() - 1))) {
       chessBoard.set(r - 1, c + 1, "P");
       chessBoard.set(r, c, " ");
       chessBoard.set(r, c + 1, " ");
       if (kingSafe()) {
         list = list + r + c + (r - 1) + (c + 1) + "N";
       }
       chessBoard.set(r - 1, c + 1, " ");
       chessBoard.set(r, c, "P");
       chessBoard.set(r, c + 1, "p");
     }
   } catch (Exception e) {
   }
   return list;
 }