#include <cassert>
#include <cstring>
#include <fstream>
-#include <map>
#include <iostream>
#include <sstream>
// Bonus for having the side to move (modified by Joona Kiiski)
const Score TempoValue = make_score(48, 22);
- struct PieceLetters : public std::map<char, Piece> {
-
- PieceLetters() {
-
- operator[]('K') = WK; operator[]('k') = BK;
- operator[]('Q') = WQ; operator[]('q') = BQ;
- operator[]('R') = WR; operator[]('r') = BR;
- operator[]('B') = WB; operator[]('b') = BB;
- operator[]('N') = WN; operator[]('n') = BN;
- operator[]('P') = WP; operator[]('p') = BP;
- operator[](' ') = PIECE_NONE;
- operator[]('.') = PIECE_NONE_DARK_SQ;
- }
-
- char from_piece(Piece p) const {
-
- std::map<char, Piece>::const_iterator it;
- for (it = begin(); it != end(); ++it)
- if (it->second == p)
- return it->first;
-
- assert(false);
- return 0;
- }
- };
-
- PieceLetters pieceLetters;
+ // To convert a Piece to and from a FEN char
+ const string PieceToChar(".PNBRQK pnbrqk ");
}
char token;
int hmc, fmn;
- std::istringstream ss(fen);
+ size_t p;
Square sq = SQ_A8;
+ std::istringstream ss(fen);
clear();
+ ss >> std::noskipws;
// 1. Piece placement field
- while (ss.get(token) && token != ' ')
+ while ((ss >> token) && !isspace(token))
{
- if (pieceLetters.find(token) != pieceLetters.end())
+ if ((p = PieceToChar.find(token)) != string::npos)
{
- put_piece(pieceLetters[token], sq);
+ put_piece(Piece(p), sq);
sq++;
}
else if (isdigit(token))
}
// 2. Active color
- if (!ss.get(token) || (token != 'w' && token != 'b'))
+ if (!(ss >> token) || (token != 'w' && token != 'b'))
goto incorrect_fen;
sideToMove = (token == 'w' ? WHITE : BLACK);
- if (!ss.get(token) || token != ' ')
+ if (!(ss >> token) || !isspace(token))
goto incorrect_fen;
// 3. Castling availability
- while (ss.get(token) && token != ' ')
+ while ((ss >> token) && !isspace(token))
if (!set_castling_rights(token))
goto incorrect_fen;
// 4. En passant square
char col, row;
- if ( (ss.get(col) && (col >= 'a' && col <= 'h'))
- && (ss.get(row) && (row == '3' || row == '6')))
+ if ( ((ss >> col) && (col >= 'a' && col <= 'h'))
+ && ((ss >> row) && (row == '3' || row == '6')))
{
st->epSquare = make_square(file_from_char(col), rank_from_char(row));
}
// 5. Halfmove clock
- if (ss >> hmc)
+ if (ss >> std::skipws >> hmc)
st->rule50 = hmc;
// 6. Fullmove number
string fen;
Square sq;
- char emptyCnt = '0';
+ char emptyCnt;
for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
{
+ emptyCnt = '0';
+
for (File file = FILE_A; file <= FILE_H; file++)
{
sq = make_square(file, rank);
fen += emptyCnt;
emptyCnt = '0';
}
- fen += pieceLetters.from_piece(piece_on(sq));
+ fen += PieceToChar[piece_on(sq)];
} else
emptyCnt++;
}
if (emptyCnt != '0')
- {
fen += emptyCnt;
- emptyCnt = '0';
- }
}
fen += (sideToMove == WHITE ? " w " : " b ");
piece = PIECE_NONE_DARK_SQ;
char c = (color_of_piece_on(sq) == BLACK ? '=' : ' ');
- cout << c << pieceLetters.from_piece(piece) << c << '|';
+ cout << c << PieceToChar[piece] << c << '|';
}
}
cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl;