1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 08:43:09 +00:00

Retire PieceLetters struct

Use a much simpler std::string instead.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-06-10 15:50:39 +02:00
parent bc4f3155ae
commit 735cac5d53

View file

@ -20,7 +20,6 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <map>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -77,33 +76,8 @@ namespace {
// Bonus for having the side to move (modified by Joona Kiiski) // Bonus for having the side to move (modified by Joona Kiiski)
const Score TempoValue = make_score(48, 22); const Score TempoValue = make_score(48, 22);
struct PieceLetters : public std::map<char, Piece> { // To convert a Piece to and from a FEN char
const string PieceToChar(".PNBRQK pnbrqk ");
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;
} }
@ -192,17 +166,19 @@ void Position::from_fen(const string& fen, bool isChess960) {
char token; char token;
int hmc, fmn; int hmc, fmn;
std::istringstream ss(fen); size_t p;
Square sq = SQ_A8; Square sq = SQ_A8;
std::istringstream ss(fen);
clear(); clear();
ss >> std::noskipws;
// 1. Piece placement field // 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++; sq++;
} }
else if (isdigit(token)) else if (isdigit(token))
@ -214,23 +190,23 @@ void Position::from_fen(const string& fen, bool isChess960) {
} }
// 2. Active color // 2. Active color
if (!ss.get(token) || (token != 'w' && token != 'b')) if (!(ss >> token) || (token != 'w' && token != 'b'))
goto incorrect_fen; goto incorrect_fen;
sideToMove = (token == 'w' ? WHITE : BLACK); sideToMove = (token == 'w' ? WHITE : BLACK);
if (!ss.get(token) || token != ' ') if (!(ss >> token) || !isspace(token))
goto incorrect_fen; goto incorrect_fen;
// 3. Castling availability // 3. Castling availability
while (ss.get(token) && token != ' ') while ((ss >> token) && !isspace(token))
if (!set_castling_rights(token)) if (!set_castling_rights(token))
goto incorrect_fen; goto incorrect_fen;
// 4. En passant square // 4. En passant square
char col, row; char col, row;
if ( (ss.get(col) && (col >= 'a' && col <= 'h')) if ( ((ss >> col) && (col >= 'a' && col <= 'h'))
&& (ss.get(row) && (row == '3' || row == '6'))) && ((ss >> row) && (row == '3' || row == '6')))
{ {
st->epSquare = make_square(file_from_char(col), rank_from_char(row)); st->epSquare = make_square(file_from_char(col), rank_from_char(row));
@ -241,7 +217,7 @@ void Position::from_fen(const string& fen, bool isChess960) {
} }
// 5. Halfmove clock // 5. Halfmove clock
if (ss >> hmc) if (ss >> std::skipws >> hmc)
st->rule50 = hmc; st->rule50 = hmc;
// 6. Fullmove number // 6. Fullmove number
@ -337,10 +313,12 @@ const string Position::to_fen() const {
string fen; string fen;
Square sq; Square sq;
char emptyCnt = '0'; char emptyCnt;
for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/') for (Rank rank = RANK_8; rank >= RANK_1; rank--, fen += '/')
{ {
emptyCnt = '0';
for (File file = FILE_A; file <= FILE_H; file++) for (File file = FILE_A; file <= FILE_H; file++)
{ {
sq = make_square(file, rank); sq = make_square(file, rank);
@ -352,16 +330,13 @@ const string Position::to_fen() const {
fen += emptyCnt; fen += emptyCnt;
emptyCnt = '0'; emptyCnt = '0';
} }
fen += pieceLetters.from_piece(piece_on(sq)); fen += PieceToChar[piece_on(sq)];
} else } else
emptyCnt++; emptyCnt++;
} }
if (emptyCnt != '0') if (emptyCnt != '0')
{
fen += emptyCnt; fen += emptyCnt;
emptyCnt = '0';
}
} }
fen += (sideToMove == WHITE ? " w " : " b "); fen += (sideToMove == WHITE ? " w " : " b ");
@ -413,7 +388,7 @@ void Position::print(Move move) const {
piece = PIECE_NONE_DARK_SQ; piece = PIECE_NONE_DARK_SQ;
char c = (color_of_piece_on(sq) == BLACK ? '=' : ' '); 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; cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl;