public static void MirrorBoard(Board board) { int[] t_PiecesArray = new int[64]; int t_side = board.Side ^ 1; // Changes side int t_castlePerm = 0; int t_enPas = (int)Square.NO_SQ; // First we reverse castlepermissions. if ((board.CastlePerm & (int)Castling.WKCA) != 0) { t_castlePerm |= (int)Castling.BKCA; } if ((board.CastlePerm & (int)Castling.WQCA) != 0) { t_castlePerm |= (int)Castling.BQCA; } if ((board.CastlePerm & (int)Castling.BKCA) != 0) { t_castlePerm |= (int)Castling.WKCA; } if ((board.CastlePerm & (int)Castling.BQCA) != 0) { t_castlePerm |= (int)Castling.WQCA; } // En pas if (board.EnPas != (int)Square.NO_SQ) { t_enPas = Conversion.getSq64ToSq120(Data.mirror64Table[Conversion.getSq120ToSq64(board.EnPas)]); } for (int i = 0; i < 64; i++) { var mirror120Sq = Conversion.getSq64ToSq120(Data.mirror64Table[i]); t_PiecesArray[i] = board[mirror120Sq]; } // Have all info so now we reset and put information in. BoardOperations.ResetBoard(board); for (int i = 0; i < 64; i++) { int reversedPiece = (int)Data.SWAP_PIECE[t_PiecesArray[i]]; board[Conversion.getSq64ToSq120(i)] = reversedPiece; } board.Side = t_side; board.EnPas = t_enPas; board.CastlePerm = t_castlePerm; board.PosKey = Hashkeys.GeneratePosKey(board); BoardOperations.UpdateListsMaterial(board); Debug.Assert(BoardOperations.CheckBoard(board)); }
/// <summary> /// Parses the input FEN string onto the board. /// </summary> /// <param name="fen"> The FEN-string to parse. </param> /// <param name="board"> The board to update. </param> /// <returns></returns> public static int ParseFen(string fen, Board board) { int rank = (int)Rank.RANK_8; int file = (int)File.FILE_A; int fenIndex = 0; int i = 0; char fenChar = ' '; BoardOperations.ResetBoard(board); while (rank >= (int)Rank.RANK_1 && fenIndex <= fen.Length) { fenChar = fen[fenIndex]; var count = 1; var piece = 0; switch (fenChar) { case 'p': piece = (int)Piece.bP; break; case 'r': piece = (int)Piece.bR; break; case 'n': piece = (int)Piece.bN; break; case 'b': piece = (int)Piece.bB; break; case 'k': piece = (int)Piece.bK; break; case 'q': piece = (int)Piece.bQ; break; case 'P': piece = (int)Piece.wP; break; case 'R': piece = (int)Piece.wR; break; case 'N': piece = (int)Piece.wN; break; case 'B': piece = (int)Piece.wB; break; case 'K': piece = (int)Piece.wK; break; case 'Q': piece = (int)Piece.wQ; break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': piece = (int)Piece.EMPTY; count = fenChar - '0'; // holds the number of empty squares. break; case '/': case ' ': rank--; file = (int)File.FILE_A; fenIndex++; continue; default: Console.Write("FEN Error default \n"); return(-1); } for (i = 0; i < count; i++) { var sq64 = (int)rank * 8 + (int)file; var sq120 = Conversion.getSq64ToSq120(sq64); if (piece != (int)Piece.EMPTY) { board[sq120] = piece; } file++; } fenIndex++; } fenChar = fen[fenIndex]; board.Side = (fenChar == 'w') ? (int)Colour.WHITE : (int)Colour.BLACK; fenIndex += 2; // We're now at castling permissions. for (i = 0; i < 4; i++) { fenChar = fen[fenIndex]; if (fenChar == ' ') { break; } switch (fenChar) { case 'K': board.CastlePerm |= (int)Castling.WKCA; break; case 'Q': board.CastlePerm |= (int)Castling.WQCA; break; case 'k': board.CastlePerm |= (int)Castling.BKCA; break; case 'q': board.CastlePerm |= (int)Castling.BQCA; break; default: break; } fenIndex++; } fenIndex++; fenChar = fen[fenIndex]; // We're not at the ENPASSANT SQ. if (board.CastlePerm >= 0 && board.CastlePerm <= 15) { if (fenChar != '-') // if there is an enpassant sq. { file = fenChar - 'a'; rank = fen[fenIndex + 1] - '1'; board.EnPas = (int)Conversion.FR2SQ(file, rank); } BoardOperations.UpdateListsMaterial(board); board.PosKey = Hashkeys.GeneratePosKey(board); return(0); } else { Console.WriteLine("FEN CASTLEPERM ERROR"); return(-1); } }