| 881 | } |
| 882 | |
| 883 | public static Board getBoard(String fen) { |
| 884 | Board board = new Board(); |
| 885 | String[] boardFen = fen.split(" "); |
| 886 | String[] rows = boardFen[0].split("/"); |
| 887 | for (int i = 0; i < 8; i++) { |
| 888 | int index = 0; |
| 889 | for (int j = 0; j < rows[i].length(); j++) { |
| 890 | final char current = rows[i].charAt(j); |
| 891 | if (Character.isDigit(current)) { |
| 892 | index += current - '0'; |
| 893 | } else { |
| 894 | final Color color = Character.isUpperCase(current) ? Color.WHITE : Color.BLACK; |
| 895 | final int row = 7 - i; |
| 896 | switch (Character.toLowerCase(current)) { |
| 897 | case 'p': |
| 898 | board.placePawn(row, index, color); |
| 899 | break; |
| 900 | case 'n': |
| 901 | board.placeKnight(row, index, color); |
| 902 | break; |
| 903 | case 'b': |
| 904 | board.placeBishop(row, index, color); |
| 905 | break; |
| 906 | case 'r': |
| 907 | board.placeRook(row, index, color); |
| 908 | break; |
| 909 | case 'q': |
| 910 | board.placeQueen(row, index, color); |
| 911 | break; |
| 912 | case 'k': |
| 913 | board.placeKing(row, index, color); |
| 914 | break; |
| 915 | } |
| 916 | index++; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | board.playerToMove = boardFen[1].equals("w") ? Color.WHITE : Color.BLACK; |
| 921 | if (board.playerToMove == Color.BLACK) { |
| 922 | board.zobristHash ^= zobristSwitchPlayer; |
| 923 | } |
| 924 | board.canCastle[0][1] = boardFen[2].contains("K"); |
| 925 | board.canCastle[0][0] = boardFen[2].contains("Q"); |
| 926 | board.canCastle[1][1] = boardFen[2].contains("k"); |
| 927 | board.canCastle[1][0] = boardFen[2].contains("q"); |
| 928 | for (int i = 0; i < 2; i++) { |
| 929 | for (int j = 0; j < 2; j++) { |
| 930 | if (!board.canCastle[i][j]) { |
| 931 | board.zobristHash ^= zobristCastle[i * 2 + j]; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | if (!boardFen[3].equals("-")) { |
| 936 | int rowDiff = boardFen[3].charAt(1) == '6' ? -1 : 1; |
| 937 | final int col = boardFen[3].charAt(0) - 'a'; |
| 938 | final int row = rowDiff == -1 ? 4 : 3; |
| 939 | final Piece pawn = board.getPiece(row, col); |
| 940 | board.moveList.add(Move.get(Piece.get(pawn.color, Cell.get(rowDiff == -1 ? 6 : 1, col), PieceType.PAWN), pawn.position, false)); |