public static final String toFEN(Position pos){
  StringBuilder ret=new StringBuilder();
  for (int r=7; r >= 0; r--) {
    int numEmpty=0;
    for (int c=0; c < 8; c++) {
      int p=pos.getPiece(Position.getSquare(c,r));
      if (p == Piece.EMPTY) {
        numEmpty++;
      }
 else {
        if (numEmpty > 0) {
          ret.append(numEmpty);
          numEmpty=0;
        }
switch (p) {
case Piece.WKING:
          ret.append('K');
        break;
case Piece.WQUEEN:
      ret.append('Q');
    break;
case Piece.WROOK:
  ret.append('R');
break;
case Piece.WBISHOP:
ret.append('B');
break;
case Piece.WKNIGHT:
ret.append('N');
break;
case Piece.WPAWN:
ret.append('P');
break;
case Piece.BKING:
ret.append('k');
break;
case Piece.BQUEEN:
ret.append('q');
break;
case Piece.BROOK:
ret.append('r');
break;
case Piece.BBISHOP:
ret.append('b');
break;
case Piece.BKNIGHT:
ret.append('n');
break;
case Piece.BPAWN:
ret.append('p');
break;
default :
throw new RuntimeException();
}
}
}
if (numEmpty > 0) {
ret.append(numEmpty);
}
if (r > 0) {
ret.append('/');
}
}
ret.append(pos.whiteMove ? " w " : " b ");
boolean anyCastle=false;
if (pos.h1Castle()) {
ret.append('K');
anyCastle=true;
}
if (pos.a1Castle()) {
ret.append('Q');
anyCastle=true;
}
if (pos.h8Castle()) {
ret.append('k');
anyCastle=true;
}
if (pos.a8Castle()) {
ret.append('q');
anyCastle=true;
}
if (!anyCastle) {
ret.append('-');
}
{
ret.append(' ');
if (pos.getEpSquare() >= 0) {
int x=Position.getX(pos.getEpSquare());
int y=Position.getY(pos.getEpSquare());
ret.append((char)(x + 'a'));
ret.append((char)(y + '1'));
}
 else {
ret.append('-');
}
}
ret.append(' ');
ret.append(pos.halfMoveClock);
ret.append(' ');
ret.append(pos.fullMoveCounter);
return ret.toString();
}

public static final String toFEN ( Position pos ) { StringBuilder ret = new StringBuilder ( ) ; for ( int r = 7 ; r >= 0 ; r -- ) { int numEmpty = 0 ; for ( int c = 0 ; c < 8 ; c ++ ) { int p = pos . getPiece ( Position . getSquare ( c , r ) ) ; if ( p == Piece . EMPTY ) { numEmpty ++ ; } else { if ( numEmpty > 0 ) { ret . append ( numEmpty ) ; numEmpty = 0 ; } switch ( p ) { case Piece . WKING : ret . append ( 'K' ) ; break ; case Piece . WQUEEN : ret . append ( 'Q' ) ; break ; case Piece . WROOK : ret . append ( 'R' ) ; break ; case Piece . WBISHOP : ret . append ( 'B' ) ; break ; case Piece . WKNIGHT : ret . append ( 'N' ) ; break ; case Piece . WPAWN : ret . append ( 'P' ) ; break ; case Piece . BKING : ret . append ( 'k' ) ; break ; case Piece . BQUEEN : ret . append ( 'q' ) ; break ; case Piece . BROOK : ret . append ( 'r' ) ; break ; case Piece . BBISHOP : ret . append ( 'b' ) ; break ; case Piece . BKNIGHT : ret . append ( 'n' ) ; break ; case Piece . BPAWN : ret . append ( 'p' ) ; break ; default : throw new RuntimeException ( ) ; } } } if ( numEmpty > 0 ) { ret . append ( numEmpty ) ; } if ( r > 0 ) { ret . append ( '/' ) ; } } ret . append ( pos . whiteMove ? " w " : " b " ) ; boolean anyCastle = false ; if ( pos . h1Castle ( ) ) { ret . append ( 'K' ) ; anyCastle = true ; } if ( pos . a1Castle ( ) ) { ret . append ( 'Q' ) ; anyCastle = true ; } if ( pos . h8Castle ( ) ) { ret . append ( 'k' ) ; anyCastle = true ; } if ( pos . a8Castle ( ) ) { ret . append ( 'q' ) ; anyCastle = true ; } if ( ! anyCastle ) { ret . append ( '-' ) ; } { ret . append ( ' ' ) ; if ( pos . getEpSquare ( ) >= 0 ) { int x = Position . getX ( pos . getEpSquare ( ) ) ; int y = Position . getY ( pos . getEpSquare ( ) ) ; ret . append ( ( char ) ( x + 'a' ) ) ; ret . append ( ( char ) ( y + '1' ) ) ; } else { ret . append ( '-' ) ; } } ret . append ( ' ' ) ; ret . append ( pos . halfMoveClock ) ; ret . append ( ' ' ) ; ret . append ( pos . fullMoveCounter ) ; return ret . toString ( ) ; }
return a fen string corresponding to a chess position object .
