1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 00:33: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 <cstring>
#include <fstream>
#include <map>
#include <iostream>
#include <sstream>
@ -77,33 +76,8 @@ namespace {
// 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 ");
}
@ -192,17 +166,19 @@ void Position::from_fen(const string& fen, bool isChess960) {
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))
@ -214,23 +190,23 @@ void Position::from_fen(const string& fen, bool isChess960) {
}
// 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));
@ -241,7 +217,7 @@ void Position::from_fen(const string& fen, bool isChess960) {
}
// 5. Halfmove clock
if (ss >> hmc)
if (ss >> std::skipws >> hmc)
st->rule50 = hmc;
// 6. Fullmove number
@ -337,10 +313,12 @@ const string Position::to_fen() const {
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);
@ -352,16 +330,13 @@ const string Position::to_fen() const {
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 ");
@ -413,7 +388,7 @@ void Position::print(Move move) const {
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;